python - 如何获得“评分”训练模型(预测)
问题描述
在函数 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())
解决方案
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)
推荐阅读
- c++ - 如何在程序崩溃时关闭程序,没有任何警告屏幕等... C++ Windows
- javascript - 单击图标时如何保存选定的日期选择器值?
- angular - 在所有挂起的订阅发出值之前,不会在路由导航上触发 ngOnDestroy
- vb.net - 用两个变量编辑 20 个列表框
- system.reactive - 寻找响应式方法来实现昂贵操作的触发器
- python - How to replace ™ in a string?
- node.js - 如何将控制器的返回数据用作 EJS 数据?
- json - Jolt generates null values as transformation output and can't be removed
- java - Android Firebase how to loop through EVERY key of the list
- gradle - Gradle - War task: how include into the .war file a specific file from a specific path