python - 我们应该为每个类(class)绘制 roc 曲线吗?

标签 python machine-learning scikit-learn

我正在做一个二元分类..我有一个不平衡的数据,我已经使用 svm 权重来试图缓解这种情况...... 如您所见,我已经计算并绘制了每个类(class)的 roc 曲线,并且得到了以下图表: enter image description here 看起来这两个类有点像一个..我不确定我做的是否正确,因为这是我第一次绘制自己的 roc 曲线......我正在使用Scikit 学习绘图......单独绘制每个类是否正确......分类器是否无法对蓝色类进行分类?

这是我用来获取情节的代码:

y_pred = clf.predict_proba(X_test)[:,0] # for calculating the probability of the first class
y_pred2 = clf.predict_proba(X_test)[:,1] # for calculating the probability of the second class
fpr, tpr, thresholds = metrics.roc_curve(y_test, y_pred)
auc=metrics.auc(fpr, tpr)
print "auc for the first class",auc

fpr2, tpr2, thresholds2 = metrics.roc_curve(y_test, y_pred2)
auc2=metrics.auc(fpr2, tpr2)
print "auc for the second class",auc2

# ploting the roc curve
plt.plot(fpr,tpr)
plt.plot(fpr2,tpr2)

plt.xlim([0.0,1.0])
plt.ylim([0.0,1.0])
plt.title('Roc curve')
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend(loc="lower right")
plt.show()

我知道有更好的方法来写字典,但我只是想先看看曲线

最佳答案

参见 Wikipedia输入您所有的 ROC 曲线需要:)

predict_proba 返回每个类别的类别概率。第一列包含第一类的概率,第二列包含第二类的概率。请注意,这两条曲线是彼此的旋转版本。这是因为类概率加起来为 1。

roc_curve 的文档声明第二个参数必须包含

Target scores, can either be probability estimates of the positive class or confidence values.

这意味着您必须传递对应于类别 1 的概率。这很可能是第二列。

您得到蓝色曲线是因为您通过了错误类别(第一列)的概率。只有绿色曲线是正确的。

为每个类计算 ROC 曲线没有意义,因为 ROC 曲线描述了分类器区分两个类的能力。每个分类器只有一条曲线。

具体问题是编码错误。

predict_proba 返回类别概率(如果肯定是类别则为 1,如果绝对不是类别则为 0,通常介于两者之间)。

metrics.roc_curve(y_test, y_pred) 现在将类别标签与概率进行比较,就像将梨与苹果汁进行比较一样。

您应该使用 predict 而不是 predict_proba 来预测类标签而不是概率。这些可以与计算 ROC 曲线的真实类标签进行比较。顺便说一下,这也删除了绘制第二条曲线的选项 - 您只能为分类器获得一条曲线,而不是为每个类获得一条曲线。

关于python - 我们应该为每个类(class)绘制 roc 曲线吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34894587/

相关文章:

python - 通过 OpenFaaS 在 Kubernetes 中部署 FastAPI 微服务

python - Pandas DataFrame 的 Keras LSTM 形状

python - 打开用于机器学习的 ROOT NTuple 的最快、最节省内存的方法是什么?

tensorflow - Keras 模型在本地运行良好,但无法在 Flask API 上运行

python - 评估模型给出的准确度不等于 sklearn 分类报告准确度

python - [sklearn][standardscaler] 我可以反转模型输出的 standardscaler 吗?

python - 为什么我的 Python 函数没有被执行?

python - 在 Selenium 中通过带有特定文本的标签名称选择元素

python - Pandas:相互评估两个数据帧

python - 具有多对一设置的 RNN - 使用哪个输出