【发布时间】:2021-10-27 18:33:48
【问题描述】:
我想使用 Scikit-Learn 的 GridSearchCV 运行一堆实验,然后打印出每个实验的召回率、精度和 f1。
这篇文章(https://scikit-learn.org/stable/auto_examples/model_selection/plot_grid_search_digits.html)建议我需要多次运行.fit和.predict。
...
scores = ['precision', 'recall']
...
for score in scores:
...
clf = GridSearchCV(
SVC(), tuned_parameters, scoring='%s_macro' % score
)
clf.fit(X_train, y_train) # running for each scoring metric
...
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print("%0.3f (+/-%0.03f) for %r"
% (mean, std * 2, params))
...
y_true, y_pred = y_test, clf.predict(X_test) # running for each scoring metric
print(classification_report(y_true, y_pred))
我想只运行一次.fit 并记录所有召回率、精度和 f1 指标。例如,类似于以下内容:
clf = GridSearchCV(
SVC(), tuned_parameters, scoring=['recall', 'precision', 'f1'] # I don't think this syntax is even possible
)
clf.fit(X_train, y_train)
for metric in clf.something_that_i_cannot_find:
### does something like this exist?
print(metric['precision']
print(metric['recall'])
print(metric['f1'])
###:end does something like this exist?
甚至可能:
...
for run in clf.something_that_i_cannot_find:
### does something like this exist?
print(classification_report(run.y_true, run.y_pred))
###:end does something like this exist?
这篇文章 (Scoring in Gridsearch CV) 建议 GridSearchCV 可以识别多个评分者,但我仍然不知道如何访问所有实验的每个分数。
GridSearchCV 不支持我正在寻找的内容吗?文章中使用的方法(即多次运行.fit 和.predict)是完成类似于我要求的事情的最简单方法吗?
感谢您的宝贵时间????
【问题讨论】:
-
您将不得不手动执行此操作,这将使用 scikit learn 中的折叠和循环参数需要大量代码,我建议设置随机状态并运行网格搜索 3 次。
-
感谢您的建议。我会采取这种方法。如果您想输入您的评论作为答案,我会接受它以关闭此循环。
标签: python scikit-learn metrics grid-search gridsearchcv