【问题标题】:Classification report with Nested Cross Validation in SKlearn (Average/Individual values)SKlearn中具有嵌套交叉验证的分类报告(平均值/个体值)
【发布时间】:2017-07-22 13:49:12
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    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.
    

    【讨论】:

    • 谢谢。它给了我个人折叠的分类报告,但我会从中取平均值。
    • 我会添加平均分类报告的代码以防其他人使用它。
    • 探索内部 cv 循环的报告真的有帮助吗?我认为从数学的角度来看,内部循环由于随机分裂而受到随机性的影响,因此不应该用于判断某组超参数是否比其他超参数做得更好
    【解决方案2】:

    这只是对 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)) 
    

    现在 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
    

    【讨论】:

      【解决方案3】:

      为什么不选择最简单的路径!我会去的-

      输入:

      results = []
      names = []
      for name, model in models:
          print(name)
          for score in ["roc_auc", "f1", "precision", "recall", "accuracy"]:
              cvs = cross_val_score(model, train, target, scoring=score, cv=10).mean()
              print(score + " : "+ str(cvs))
             
          print('\n')
          
      return names, results
      

      输出:

      【讨论】:

        猜你喜欢
        • 2020-08-03
        • 2014-06-13
        • 2021-02-10
        • 2018-09-12
        • 2017-11-16
        • 2021-02-17
        • 1970-01-01
        • 1970-01-01
        • 2014-11-24
        相关资源
        最近更新 更多