在GridSearchCV函数中有搜索和训练的数据(X,y)。 训练发生在自定义标准T_scorer上。 是否可以在T_scorer函数中使用经过训练的模型? 我需要" T_scorer"预测数据" X1"。 也就是说,模型在每次迭代时对数据(X,y)进行训练,并在(X1,y1)上预测 再次(X1,y1)根本不参加培训和#34; 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())
答案 0 :(得分:2)
make_scorer()
的功能时,才应使用 (y_true, y_pred)
。在函数上使用make_scorer()
时,返回的签名为:
func(estimator,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)