是否可以通过一些解决方法从cross_val_score获取分类报告?我正在使用嵌套的交叉验证,在这里可以获得模型的各种评分,但是,我希望看到外部循环的分类报告。有什么建议吗?
# Choose cross-validation techniques for the inner and outer loops,
# independently of the dataset.
# E.g "LabelKFold", "LeaveOneOut", "LeaveOneLabelOut", etc.
inner_cv = KFold(n_splits=4, shuffle=True, random_state=i)
outer_cv = KFold(n_splits=4, shuffle=True, random_state=i)
# Non_nested parameter search and scoring
clf = GridSearchCV(estimator=svr, param_grid=p_grid, cv=inner_cv)
# Nested CV with parameter optimization
nested_score = cross_val_score(clf, X=X_iris, y=y_iris, cv=outer_cv)
我想在分数值旁边看到分类报告。
http://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html
最佳答案
我们可以定义自己的评分函数,如下所示:
from sklearn.metrics import classification_report, accuracy_score, make_scorer
def classification_report_with_accuracy_score(y_true, y_pred):
print classification_report(y_true, y_pred) # print classification report
return accuracy_score(y_true, y_pred) # return accuracy score
现在,只需使用
cross_val_score
使用我们新的评分功能调用make_scorer
:# Nested CV with parameter optimization
nested_score = cross_val_score(clf, X=X_iris, y=y_iris, cv=outer_cv, \
scoring=make_scorer(classification_report_with_accuracy_score))
print nested_score
它将以文本形式打印分类报告,同时以数字形式返回
nested_score
。使用此新的评分功能运行时的http://scikit-learn.org/stable/auto_examples/model_selection/plot_nested_cross_validation_iris.html示例,输出的最后几行如下:
# precision recall f1-score support
#0 1.00 1.00 1.00 14
#1 1.00 1.00 1.00 14
#2 1.00 1.00 1.00 9
#avg / total 1.00 1.00 1.00 37
#[ 0.94736842 1. 0.97297297 1. ]
#Average difference of 0.007742 with std. dev. of 0.007688.
关于machine-learning - SKlearn中具有嵌套交叉验证的分类报告(平均值/单个值),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42562146/