machine-learning - SKlearn 中嵌套交叉验证的分类报告(平均值/个体值)

标签 machine-learning scikit-learn classification cross-validation

是否可以通过一些解决方法从 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

现在,只需使用 make_scorer 使用我们的新评分函数调用 cross_val_score:

# 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/

相关文章:

machine-learning - 决策树(例如 C4.5)是否被视为非参数学习?

python - 为什么这不起作用? CountVectorizer 中的停用词

python-3.x - 如何在 scikit-learn 中访问管道 GridSearchCV 内估计器的属性?

machine-learning - 在 caffe 中测试回归网络

python - 线性回归梯度

optimization - 生成匹配对进行统计分析

python - 将模型拟合到 Python 中的所有变量(Scikit Learn)

java - Weka 预测同一类别

javascript - 如何计算Google Earth Engine中分类图像(Landsat)中每个类别的像素总和?

machine-learning - 在RapidMiner中使用测试数据集