首页 > 解决方案 > 如何获得“评分”训练模型(预测)

问题描述

在函数 GridSearchCV 中有数据 (X, y) 用于搜索和训练。训练在自定义标准 T_scorer 上进行。是否可以在 T_scorer 函数中使用经过训练的模型?我需要“T_scorer”预测数据“X1”。也就是说,模型在每次迭代时都在数据 (X,y) 上进行训练,并在 (X1,y1) 上进行预测,再次 (X1, y1) 根本不参与训练并且“GridSearchCV”这些数据看不到。

理想情况下,我们应该在数据(X,y)上进行训练,并且在“评分”时,应该传输基于预测(X1,y1)的结果)

def T_scorer(y_true, y_pred, clf, **kwargs):
    r = np.sum((y_pred == 0) & (y_pred == y_true))

    y_pred1 = clf.predict(X1)  #It doesn't work

    confmat = confusion_matrix(y, y_pred)
    print(confmat)
    print(r)
    return r

_scorer = make_scorer(T_scorer)

clf = RandomForestClassifier()
grid_searcher = GridSearchCV(clf, parameter_grid, cv=StratifiedKFold(shuffle =True,random_state=42),verbose=20, scoring=_scorer)
grid_searcher.fit(X, y)
clf_best = grid_searcher.best_estimator_
print('Best params = ', clf_best.get_params())

标签: pythonscikit-learngrid-search

解决方案


make_scorer()仅当您具有签名功能时才应使用(y_true, y_pred)。当您make_scorer()在函数上使用时,返回的签名是:

func(估计器,X,y)

然后在 GridSearchCV 中使用。因此make_scorer,您可以将函数指定为:

# I am assuming this is the data you want to use
X1 = X[:1000]
y1 = y[:1000]

def T_scorer(clf, X, y):
    # Here X and y passed on to the function from GridSearchCV will not be used
    y_pred1 = clf.predict(X1)
    r = np.sum((y_pred1 == 0) & (y1 == y_pred1))
    confmat = confusion_matrix(y1, y_pred1)
    print(confmat)
    return r

# Now dont use make_scorer, pass directly
grid_searcher = GridSearchCV(clf,..., verbose=20, scoring=T_scorer)

推荐阅读