gpt4 book ai didi

numpy - python中二元分类的ROC曲线

转载 作者:行者123 更新时间:2023-11-30 08:27:57 24 4
gpt4 key购买 nike

我正在使用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()

关于numpy - python中二元分类的ROC曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43043271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com