SKlearn中具有嵌套交叉验证的分类报告

ute*_*ngr 8 classification machine-learning scikit-learn 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)
Run Code Online (Sandbox Code Playgroud)

我想在评分值旁边看到分类报告. http://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html

San*_*Dey 12

我们可以定义我们自己的评分函数,如下所示:

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
Run Code Online (Sandbox Code Playgroud)

现在,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 
Run Code Online (Sandbox Code Playgroud)

它将打印分类报告作为文本同时返回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.
Run Code Online (Sandbox Code Playgroud)


ute*_*ngr 11

它只是Sandipan答案的补充,因为我无法编辑它.如果我们想要计算完整的交叉验证运行的平均分类报告而不是单个折叠,我们可以使用以下代码:

# Variables for average classification report
originalclass = []
predictedclass = []

#Make our customer score
def classification_report_with_accuracy_score(y_true, y_pred):
    originalclass.extend(y_true)
    predictedclass.extend(y_pred)
    return accuracy_score(y_true, y_pred) # return accuracy score

inner_cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=i)
outer_cv = StratifiedKFold(n_splits=5, 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, scoring=make_scorer(classification_report_with_accuracy_score))

# Average values in classification report for all folds in a K-fold Cross-validation  
print(classification_report(originalclass, predictedclass)) 
Run Code Online (Sandbox Code Playgroud)

现在Sandipan答案中的示例结果如下所示:

            precision    recall  f1-score   support

          0       1.00      1.00      1.00        50
          1       0.96      0.94      0.95        50
          2       0.94      0.96      0.95        50

avg / total       0.97      0.97      0.97       150
Run Code Online (Sandbox Code Playgroud)