【问题标题】:Creating a threshold-coded ROC plot in Python在 Python 中创建阈值编码的 ROC 图
【发布时间】:2014-04-26 10:16:21
【问题描述】:

R 的ROCR package 提供了用于绘制 ROC 曲线的选项,这些选项将沿着曲线对阈值进行颜色编码和标记:

我能用 Python 最接近的是

from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(qualityTrain.PoorCare, qualityTrain.Pred1)
plt.plot(fpr, tpr, label='ROC curve', color='b')
plt.axes().set_aspect('equal')
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])

给了

是否有提供与 R 标记(使用print.cutoffs.at)和颜色代码(使用colorize)阈值能力相当的功能的包?大概这个信息在thresholds,由sklearn.metrics.roc_curve返回,但是我不知道怎么用它来给图上色和标注。

【问题讨论】:

    标签: python r matplotlib scikit-learn roc


    【解决方案1】:
    import sklearn # for the roc curve
    import matplotlib.pyplot as plt
    
    def plot_roc(labels, predictions, positive_label, thresholds_every=10, title=''):
      # fp: false positive rates. tp: true positive rates
      fp, tp, thresholds = sklearn.metrics.roc_curve(labels, predictions, pos_label=positive_label)
      roc_auc = sklearn.metrics.auc(fp, tp)
    
      figure(figsize=(16, 16))
      plt.plot(fp, tp, label='ROC curve (area = %0.2f)' % roc_auc, linewidth=2, color='darkorange')
      plt.plot([0, 1], [0, 1], color='navy', linestyle='--', linewidth=2)
      plt.xlabel('False positives rate')
      plt.ylabel('True positives rate')
      plt.xlim([-0.03, 1.0])
      plt.ylim([0.0, 1.03])
      plt.title(title)
      plt.legend(loc="lower right")
      plt.grid(True)
    
      # plot some thresholds
      thresholdsLength = len(thresholds)
      colorMap=plt.get_cmap('jet', thresholdsLength)
      for i in range(0, thresholdsLength, thresholds_every):
        threshold_value_with_max_four_decimals = str(thresholds[i])[:5]
        plt.text(fp[i] - 0.03, tp[i] + 0.005, threshold_value_with_max_four_decimals, fontdict={'size': 15}, color=colorMap(i/thresholdsLength));
      plt.show()
    

    用法:

    labels = [1, 1, 2, 2, 2, 3]
    predictions = [0.7, 0.99, 0.9, 0.3, 0.7, 0.01] # predictions/accuracy for class 1
    plot_roc(labels, predictions, positive_label=1, thresholds_every=1, title="ROC Curve - Class 1")
    

    结果: plot result

    【讨论】:

      【解决方案2】:

      看看这个要点:

      https://gist.github.com/podshumok/c1d1c9394335d86255b8

      roc_data = sklearn.metrics.roc_curve(...)
      plot_roc(*roc_data, label_every=5)
      

      【讨论】:

        猜你喜欢
        • 2013-05-14
        • 2020-02-13
        • 2013-04-27
        • 2020-01-25
        • 1970-01-01
        • 2022-09-29
        • 1970-01-01
        • 2022-06-12
        • 2021-02-10
        相关资源
        最近更新 更多