写一段画混淆矩阵的代码
时间: 2023-02-26 12:07:12 浏览: 58
以下是一段用 Python 画混淆矩阵的代码示例:
```
import matplotlib.pyplot as plt
import numpy as np
# 假设您有如下混淆矩阵
cm = np.array([[100, 20], [30, 40]])
# 绘制混淆矩阵
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
# 添加标题
plt.title('Confusion Matrix')
# 添加数字标签
threshold = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > threshold else "black")
# 添加 X 轴标签
plt.xlabel('Predicted label')
# 添加 Y 轴标签
plt.ylabel('True label')
# 添加图例
plt.colorbar()
# 去掉坐标轴的刻度
plt.xticks(np.arange(2), [0, 1])
plt.yticks(np.arange(2), [0, 1])
# 显示图像
plt.show()
```
相关问题
python画混淆矩阵
在Python中,你可以使用scikit-learn库来计算和绘制混淆矩阵。下面是一个示例代码,展示了如何使用scikit-learn来计算和绘制混淆矩阵:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
# 定义真实标签和预测标签
y_true = np.array([0, 1, 0, 1, 1, 0, 0, 1])
y_pred = np.array([0, 1, 1, 1, 0, 0, 1, 1])
# 计算混淆矩阵
cm = confusion_matrix(y_true, y_pred)
# 绘制混淆矩阵
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.title("Confusion Matrix")
plt.colorbar()
tick_marks = np.arange(2)
plt.xticks(tick_marks, ['Class 0', 'Class 1'])
plt.yticks(tick_marks, ['Class 0', 'Class 1'])
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
# 在每个格子中显示数值
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
plt.text(j, i, format(cm[i, j], 'd'),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.show()
```
这段代码首先导入了必要的库,然后定义了真实标签和预测标签。接下来,使用`confusion_matrix`函数计算混淆矩阵,并将结果存储在变量`cm`中。最后,使用Matplotlib库绘制混淆矩阵,并在每个格子中显示对应的数值。
pytorch画混淆矩阵
在PyTorch中,可以使用matplotlib库来绘制混淆矩阵。下面是一个示例代码,展示了如何使用PyTorch和matplotlib来画混淆矩阵:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义混淆矩阵
def confusion_matrix(preds, labels, conf_matrix):
for p, t in zip(preds, labels):
conf_matrix\[p, t\] += 1
return conf_matrix
# 绘制混淆矩阵
def plot_matrix(matrix, labels):
plt.imshow(matrix, cmap=plt.cm.Blues)
for x in range(len(labels)):
for y in range(len(labels)):
info = matrix\[y, x\]
plt.text(x, y, info, verticalalignment='center', horizontalalignment='center')
plt.xticks(range(len(labels)), labels, rotation=45)
plt.yticks(range(len(labels)), labels)
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.title('Confusion Matrix')
plt.colorbar()
plt.show()
# 示例代码
preds = \[0, 1, 2, 1, 0, 2, 2\]
labels = \[0, 1, 2, 1, 0, 1, 2\]
conf_matrix = np.zeros((3, 3))
conf_matrix = confusion_matrix(preds, labels, conf_matrix)
plot_matrix(conf_matrix, \['Label 0', 'Label 1', 'Label 2'\])
```
这段代码首先定义了一个混淆矩阵函数`confusion_matrix`,用于计算混淆矩阵。然后定义了一个绘制混淆矩阵的函数`plot_matrix`,该函数使用matplotlib库来绘制混淆矩阵。最后,通过调用这两个函数,可以计算准确率并绘制混淆矩阵。
希望这个示例代码能够帮助你理解如何使用PyTorch和matplotlib来画混淆矩阵。如果有任何问题,请随时提问。
#### 引用[.reference_title]
- *1* *2* [混淆矩阵的绘制](https://blog.csdn.net/qq_45470799/article/details/123737859)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [pytorch绘制混淆矩阵](https://blog.csdn.net/qq_18617009/article/details/103345308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]