用于 Python 中二元分类的 ROC 曲线



我正在使用RandomForestClassifier绘制二元分类的ROC曲线

我有两个 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-learn示例的示例代码:

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()

最新更新