【问题标题】:IndexError: too many indices for array while plotting ROC curve with scikit-learn?IndexError:使用 scikit-learn 绘制 ROC 曲线时数组索引过多?
【发布时间】:2015-04-08 00:27:27
【问题描述】:

我想绘制 scikit-lern 实现的 ROC 曲线,所以我尝试了以下方法:

from sklearn.metrics import roc_curve, auc
false_positive_rate, recall, thresholds = roc_curve(y_test, prediction[:, 1])
roc_auc = auc(false_positive_rate, recall)
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate, recall, 'b', label='AUC = %0.2f' % roc_auc)
plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1], 'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.ylabel('Recall')
plt.xlabel('Fall-out')
plt.show()

这是输出:

Traceback (most recent call last):
  File "/Users/user/script.py", line 62, in <module>
    false_positive_rate, recall, thresholds = roc_curve(y_test, prediction[:, 1])
IndexError: too many indices for array

然后从previous question 我尝试了这个:

false_positive_rate, recall, thresholds = roc_curve(y_test, prediction)

并得到了这个回溯:

/usr/local/lib/python2.7/site-packages/sklearn/metrics/metrics.py:705: DeprecationWarning: elementwise comparison failed; this will raise the error in the future.
  not (np.all(classes == [0, 1]) or
/usr/local/lib/python2.7/site-packages/sklearn/metrics/metrics.py:706: DeprecationWarning: elementwise comparison failed; this will raise the error in the future.
  np.all(classes == [-1, 1]) or
Traceback (most recent call last):
  File "/Users/user/PycharmProjects/TESIS_CODE/clasificacion_simple_v1.py", line 62, in <module>
    false_positive_rate, recall, thresholds = roc_curve(y_test, prediction)
  File "/usr/local/lib/python2.7/site-packages/sklearn/metrics/metrics.py", line 890, in roc_curve
    y_true, y_score, pos_label=pos_label, sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/site-packages/sklearn/metrics/metrics.py", line 710, in _binary_clf_curve
    raise ValueError("Data is not binary and pos_label is not specified")
ValueError: Data is not binary and pos_label is not specified

然后我也尝试了这个:

false_positive_rate, recall, thresholds = roc_curve(y_test, prediction[0].values)

这是回溯:

AttributeError: 'numpy.int64' object has no attribute 'values'

知道如何正确绘制此指标吗?提前致谢!

这是预测变量的形状:

print prediction.shape
(650,)

这是testing_matrix: (650, 9596)的形状

【问题讨论】:

  • 您能否检查预测变量的type 并更新问题以显示它是如何创建的?

标签: python numpy matplotlib scikit-learn


【解决方案1】:

变量prediction 必须是1d array(与y_test 的形状相同)。您可以通过检查 shape 属性进行检查,例如y_test.shape。我觉得

prediction[0].values 

返回

AttributeError: 'numpy.int64' object has no attribute 'values'

因为您试图在预测元素上调用.values

更新:

ValueError: Data is not binary and pos_label is not specified

我以前没有注意到这一点。如果您的类不是二进制的,则必须在 inroc_curve 时指定 pos_label 参数,以便绘制一个类与其他类。为此,您需要您的类标签为整数。您可以使用:

from sklearn.preprocessing import LabelEncoder
class_labels = LabelEncoder()
prediction_le = class_lables.fit_transform(prediction)

pediction_le 返回类重新编码 int

更新 2:

你的预测器只返回一个类,所以你不能绘制 ROC 曲线

【讨论】:

  • 抱歉看不到 - .shapey_test 生成什么?
  • 预测的不同值是什么?
  • 你需要至少两个预测类来绘制 roc_curve
  • “我如何优化我的模型”是一个不同的问题,更适合交叉验证。我的回答解决了你原来的问题。如果您同意,请接受。
  • 我认为代码还可以。 testing_matrix的形状是什么? m 是否大于 n
猜你喜欢
  • 2016-08-24
  • 2015-06-23
  • 2014-09-27
  • 1970-01-01
  • 2020-04-11
  • 2018-01-20
  • 2021-03-03
  • 2019-02-27
  • 2018-07-30
相关资源
最近更新 更多