python中二元分类的ROC曲线 [英] ROC curve for binary classification in python

查看:28
本文介绍了python中二元分类的ROC曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 RandomForestClassifier

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

I have two numpy arrays one contains predicted values and one contains true values as follows:

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(曲线下面积)?

How do I port ROC curve and obtain AUC (Area Under Curve) for this binary classification result in ipython?

推荐答案

您需要概率来创建 ROC 曲线.

You need probabilities to create ROC curve.

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 示例的示例代码:

Example code from scikit-learn examples:

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

这篇关于python中二元分类的ROC曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆