Numpy python中用于二进制分类的ROC曲线

Numpy python中用于二进制分类的ROC曲线,numpy,machine-learning,scikit-learn,ipython,Numpy,Machine Learning,Scikit Learn,Ipython,我正准备使用randomfrestedclassifier 我有两个numpy数组,一个包含预测值,另一个包含真值,如下所示: In [84]: test Out[84]: array([0, 1, 0, ..., 0, 1, 0]) In [85]: pred Out[85]: array([0, 1, 0, ..., 1, 0, 0]) 如何在ipython中输入ROC曲线并获得此二元分类结果的AUC(曲线下面积)?您需要概率来创建ROC曲线 In [84]: test Out[84]:

我正准备使用
randomfrestedclassifier

我有两个numpy数组,一个包含预测值,另一个包含真值,如下所示:

In [84]: test
Out[84]: array([0, 1, 0, ..., 0, 1, 0])

In [85]: pred
Out[85]: array([0, 1, 0, ..., 1, 0, 0])

如何在ipython中输入ROC曲线并获得此二元分类结果的AUC(曲线下面积)?

您需要概率来创建ROC曲线

In [84]: test
Out[84]: array([0, 1, 0, ..., 0, 1, 0])

In [85]: pred
Out[85]: array([0.1, 1, 0.3, ..., 0.6, 0.85, 0.2])
scikit学习示例中的示例代码:

import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(2):
    fpr[i], tpr[i], _ = roc_curve(test, pred)
    roc_auc[i] = auc(fpr[i], tpr[i])

print roc_auc_score(test, pred)
plt.figure()
plt.plot(fpr[1], tpr[1])
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.show()

检查
test
pred
的形状[0]的长度是否不等于0。如果是,请使用
anyarray.reformate(-1)
。你可以使用
模型获得概率。预测概率(testdata)[:,1]
我在
plt.plot(fpr[2],tpr[2])上得到了一个关键错误
我把它改为
1
。。。其他一切都起作用了<示例中的code>fpr[2]是因为有3个类。对于二元分类,只需计算fpr,tpr,roc曲线(y检验,y评分)并绘制
x=fpr,y=tpr