导入多分类数据把混淆矩阵和AUC曲线画出来python代码
时间: 2023-10-08 16:11:53 浏览: 92
好的,以下是导入多分类数据并画出混淆矩阵和AUC曲线的Python代码:
```python
# 导入必要的库
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, roc_curve, auc
from itertools import cycle
# 定义一个函数,用于展示混淆矩阵
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in np.ndindex(cm.shape):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
# 定义一个函数,用于展示AUC曲线
def plot_roc_curve(Y_test, Y_pred, n_classes):
# 计算每个类别的ROC曲线和AUC值
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(Y_test[:, i], Y_pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# 将所有ROC曲线画在一张图上
plt.figure()
lw = 2
colors = cycle(['aqua', 'darkorange', 'cornflowerblue', 'pink', 'red', 'yellow'])
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (AUC = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
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")
# 生成一个随机的多分类数据集
n_classes = 6
Y_test = np.random.randint(n_classes, size=200)
Y_pred = np.random.randint(n_classes, size=200)
# 计算混淆矩阵
cnf_matrix = confusion_matrix(Y_test, Y_pred)
# 画出混淆矩阵
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=np.arange(n_classes),
title='Confusion matrix, without normalization')
# 画出归一化的混淆矩阵
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=np.arange(n_classes), normalize=True,
title='Normalized confusion matrix')
# 画出AUC曲线
Y_test_one_hot = np.eye(n_classes)[Y_test]
Y_pred_one_hot = np.eye(n_classes)[Y_pred]
plot_roc_curve(Y_test_one_hot, Y_pred_one_hot, n_classes)
# 展示所有图像
plt.show()
```
以上代码先生成一个随机的多分类数据集,然后计算出混淆矩阵,并展示出来。接着,画出了归一化的混淆矩阵和AUC曲线。最后,通过`plt.show()`展示所有图像。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)