带数值的混淆矩阵代码
时间: 2023-07-09 11:04:41 浏览: 72
以下是一个示例代码,可以生成带数值的混淆矩阵:
```python
import numpy as np
from sklearn.metrics import confusion_matrix
# 生成随机预测结果和真实标签
y_pred = np.random.randint(0, 2, size=100)
y_true = np.random.randint(0, 2, size=100)
# 计算混淆矩阵
cm = confusion_matrix(y_true, y_pred)
# 打印带数值的混淆矩阵
print('Confusion Matrix:')
print('[TP, FP]')
print('[FN, TN]')
print(cm)
```
输出结果类似于以下内容:
```
Confusion Matrix:
[TP, FP]
[FN, TN]
[[25 26]
[22 27]]
```
其中,TP 表示真正例数量,FP 表示假正例数量,FN 表示假反例数量,TN 表示真反例数量。可以根据需要自行调整输出格式。
相关问题
带数值的混淆矩阵画图代码
以下是Python代码,使用Matplotlib库绘制带数值的混淆矩阵图:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义混淆矩阵
confusion_matrix = np.array([[25, 5, 0], [5, 20, 5], [0, 5, 25]])
# 定义标签
labels = ['Class 0', 'Class 1', 'Class 2']
# 绘制混淆矩阵图
fig, ax = plt.subplots()
im = ax.imshow(confusion_matrix, cmap='Blues')
# 显示数值
for i in range(len(labels)):
for j in range(len(labels)):
text = ax.text(j, i, confusion_matrix[i, j],
ha="center", va="center", color="w")
# 设置标题、轴标签、刻度标签
ax.set_title("Confusion Matrix")
ax.set_xticks(np.arange(len(labels)))
ax.set_yticks(np.arange(len(labels)))
ax.set_xticklabels(labels)
ax.set_yticklabels(labels)
ax.set_xlabel("Predicted label")
ax.set_ylabel("True label")
# 旋转刻度标签以避免重叠
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# 在混淆矩阵图旁添加颜色条
cbar = ax.figure.colorbar(im, ax=ax)
# 显示图形
plt.show()
```
运行以上代码,将得到带数值的混淆矩阵图。可以根据实际需要修改混淆矩阵、标签、标题和颜色映射等参数。
读取两个列表完成带数值的混淆矩阵画图代码
可以使用Python中的Matplotlib库来绘制混淆矩阵的图像。以下是一个示例代码,它将两个列表作为输入,分别表示实际类别和预测类别,然后绘制混淆矩阵的图像。
```python
import matplotlib.pyplot as plt
import numpy as np
# 定义实际类别和预测类别
actual = [0, 1, 2, 3, 4]
predicted = [0, 1, 2, 3, 4]
# 定义混淆矩阵的数值
confusion_matrix = np.array([[10, 2, 0, 0, 0],
[3, 15, 2, 0, 0],
[0, 2, 20, 1, 0],
[0, 0, 2, 18, 0],
[0, 0, 0, 1, 17]])
# 绘制混淆矩阵的图像
plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('Confusion Matrix')
plt.colorbar()
tick_marks = np.arange(len(actual))
plt.xticks(tick_marks, actual)
plt.yticks(tick_marks, predicted)
plt.xlabel('Actual')
plt.ylabel('Predicted')
# 将数值添加到混淆矩阵的每个单元格
thresh = confusion_matrix.max() / 2.
for i, j in itertools.product(range(confusion_matrix.shape[0]), range(confusion_matrix.shape[1])):
plt.text(j, i, format(confusion_matrix[i, j], 'd'),
horizontalalignment="center",
color="white" if confusion_matrix[i, j] > thresh else "black")
plt.tight_layout()
plt.show()
```
该代码将绘制以下混淆矩阵的图像:
```
0 1 2 3 4
-----------------------
0 | 10 2 0 0 0
1 | 3 15 2 0 0
2 | 0 2 20 1 0
3 | 0 0 2 18 0
4 | 0 0 0 1 17
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![m](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)