IndexError:使用scikit-learn绘制ROC曲线时,数组的索引太多了?

joh*_*doe 5 python numpy matplotlib scikit-learn

我想修改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()
Run Code Online (Sandbox Code Playgroud)

这是输出:

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

然后从上一个问题我尝试了这个:

false_positive_rate, recall, thresholds = roc_curve(y_test, prediction)
Run Code Online (Sandbox Code Playgroud)

得到了这个追溯:

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

然后我也尝试了这个:

false_positive_rate, recall, thresholds = roc_curve(y_test, prediction[0].values)
Run Code Online (Sandbox Code Playgroud)

这是追溯:

AttributeError: 'numpy.int64' object has no attribute 'values'
Run Code Online (Sandbox Code Playgroud)

任何想法如何正确绘制这个指标?提前致谢!

这是预测变量的形状:

print prediction.shape
(650,)
Run Code Online (Sandbox Code Playgroud)

这是形状 testing_matrix: (650, 9596)

JAB*_*JAB 1

该变量prediction需要是 a 1d array(与 形状相同 y_test)。您可以通过检查 shape 属性来进行检查,例如y_test.shape。我认为

prediction[0].values 
Run Code Online (Sandbox Code Playgroud)

回报

AttributeError: 'numpy.int64' object has no attribute 'values'
Run Code Online (Sandbox Code Playgroud)

因为你试图调用.values预测元素。

更新:

ValueError: Data is not binary and pos_label is not specified
Run Code Online (Sandbox Code Playgroud)

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

from sklearn.preprocessing import LabelEncoder
class_labels = LabelEncoder()
prediction_le = class_lables.fit_transform(prediction)
Run Code Online (Sandbox Code Playgroud)

pediction_le返回类重新编码aint

更新2:

您的预测器仅返回一类,因此您无法绘制 ROC 曲线