如何获得 "scoring" 训练模型(预测)
How to get a "scoring" trained model(predict)
GridSearchCV函数中搜索训练的数据(X,y)。
训练根据自定义标准 T_scorer 进行。
是否可以在 T_scorer 函数中使用经过训练的模型?
我需要 "T_scorer" 预测数据 "X1"。
也就是说,模型在每次迭代时都在数据 (X,y) 上进行训练,并在 (X1,y1) 上进行预测
同样,(X1, y1) 根本不参与训练,"GridSearchCV" 看不到这些数据。
理想情况下,我们应该在数据 (X,y) 上进行训练,并且在 "scoring" 中应该传输基于预测 (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())
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)
GridSearchCV函数中搜索训练的数据(X,y)。 训练根据自定义标准 T_scorer 进行。 是否可以在 T_scorer 函数中使用经过训练的模型? 我需要 "T_scorer" 预测数据 "X1"。 也就是说,模型在每次迭代时都在数据 (X,y) 上进行训练,并在 (X1,y1) 上进行预测 同样,(X1, y1) 根本不参与训练,"GridSearchCV" 看不到这些数据。
理想情况下,我们应该在数据 (X,y) 上进行训练,并且在 "scoring" 中应该传输基于预测 (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())
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)