python绘制多分类的ROC曲线
时间: 2023-08-30 20:09:25 浏览: 75
ROC曲线绘制
绘制多分类的ROC曲线可以使用micro-averaging和macro-averaging两种方法。
1. micro-averaging方法:
micro-averaging方法将所有的TP、FP、FN的数量累加,再计算ROC曲线。对于多分类问题,我们可以将每个类别的TP、FP、FN数量累加起来,得到总的TP、FP、FN数量,然后计算ROC曲线。
以下是使用micro-averaging方法绘制多分类的ROC曲线的示例代码:
```python
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
import numpy as np
# y_true是真实标签,y_pred是预测标签概率
y_true = np.array([0, 1, 2, 0, 1, 2])
y_pred = np.array([[0.9, 0.05, 0.05],
[0.05, 0.9, 0.05],
[0.05, 0.05, 0.9],
[0.8, 0.1, 0.1],
[0.1, 0.8, 0.1],
[0.1, 0.1, 0.8]])
# 计算ROC曲线和AUC
fpr = dict()
tpr = dict()
roc_auc = dict()
n_classes = 3
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_true == i, y_pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# micro-averaging方法
fpr["micro"], tpr["micro"], _ = roc_curve(y_true.ravel(), y_pred.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
# Plot ROC curves
plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
label='micro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["micro"]),
linewidth=2)
colors = ['aqua', 'darkorange', 'cornflowerblue']
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=2,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=2)
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 for multi-class')
plt.legend(loc="lower right")
plt.show()
```
2. macro-averaging方法:
macro-averaging方法将每个类别的ROC曲线计算出来,然后对所有的ROC曲线取平均。对于多分类问题,我们可以将每个类别的TP、FP、FN数量分别计算出ROC曲线,然后对所有的ROC曲线取平均。
以下是使用macro-averaging方法绘制多分类的ROC曲线的示例代码:
```python
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
import numpy as np
# y_true是真实标签,y_pred是预测标签概率
y_true = np.array([0, 1, 2, 0, 1, 2])
y_pred = np.array([[0.9, 0.05, 0.05],
[0.05, 0.9, 0.05],
[0.05, 0.05, 0.9],
[0.8, 0.1, 0.1],
[0.1, 0.8, 0.1],
[0.1, 0.1, 0.8]])
# 计算ROC曲线和AUC
fpr = dict()
tpr = dict()
roc_auc = dict()
n_classes = 3
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_true == i, y_pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# macro-averaging方法
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])
mean_tpr /= n_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
# Plot ROC curves
plt.figure()
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
linewidth=2)
colors = ['aqua', 'darkorange', 'cornflowerblue']
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=2,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=2)
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 for multi-class')
plt.legend(loc="lower right")
plt.show()
```
阅读全文