两个200个标签的列表画混淆矩阵代码
时间: 2023-11-28 16:47:24 浏览: 29
以下是一个Python示例代码,可以计算两个200个标签的列表之间的混淆矩阵:
```python
import numpy as np
from sklearn.metrics import confusion_matrix
# 两个200个标签的列表
true_labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... , 199]
pred_labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... , 199]
# 计算混淆矩阵
confusion_mat = confusion_matrix(true_labels, pred_labels)
# 打印混淆矩阵
print(confusion_mat)
```
在这个示例代码中,`true_labels`和`pred_labels`是两个200个标签的列表,分别代表真实标签和预测标签。`confusion_matrix`函数可以计算混淆矩阵,返回一个二维数组。最后,使用`print`函数打印混淆矩阵。
相关问题
两个4个标签的200个样品的列表画混淆矩阵代码
下面是一个Python代码示例,用于绘制两个具有4个标签的200个样本的混淆矩阵:
``` python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
# 生成随机标签
y_true = np.random.randint(0, 4, 200)
y_pred = np.random.randint(0, 4, 200)
# 计算混淆矩阵
cm = confusion_matrix(y_true, y_pred)
# 绘制混淆矩阵
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.colorbar()
tick_marks = np.arange(4)
plt.xticks(tick_marks, ['Label 0', 'Label 1', 'Label 2', 'Label 3'], rotation=45)
plt.yticks(tick_marks, ['Label 0', 'Label 1', 'Label 2', 'Label 3'])
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.show()
```
这里使用了numpy和matplotlib库来生成随机标签和绘制混淆矩阵,并使用scikit-learn库中的confusion_matrix函数来计算混淆矩阵。代码中的tick_marks变量是用于设置标签的位置和方向的数组,可以根据需要进行修改。
读取两个列表完成带数值的混淆矩阵画图代码
以下是Python代码:
```python
import matplotlib.pyplot as plt
import numpy as np
# 两个列表,分别表示预测值和真实值
predicted = [1, 0, 1, 1, 0, 1, 1, 0, 1, 0]
actual = [1, 0, 0, 1, 1, 1, 0, 0, 1, 0]
# 计算混淆矩阵
confusion_matrix = np.zeros((2, 2))
for p, a in zip(predicted, actual):
confusion_matrix[p][a] += 1
# 画图
fig, ax = plt.subplots(figsize=(5, 5))
ax.matshow(confusion_matrix, cmap=plt.cm.Blues)
for i in range(confusion_matrix.shape[0]):
for j in range(confusion_matrix.shape[1]):
ax.text(x=j, y=i, s=int(confusion_matrix[i, j]), va='center', ha='center')
ax.set_xlabel('True label')
ax.set_ylabel('Predicted label')
plt.show()
```
解释一下代码的过程:
- 首先定义了两个列表 `predicted` 和 `actual`,分别表示模型的预测结果和真实结果。
- 接着使用 NumPy 库创建了一个 2x2 的全零数组 `confusion_matrix`,用于记录混淆矩阵。
- 遍历 `predicted` 和 `actual` 列表,根据预测结果和真实结果更新混淆矩阵。
- 最后使用 Matplotlib 库画出混淆矩阵的图像,其中 `ax.matshow()` 函数用于显示矩阵,`ax.text()` 函数用于在每个单元格中添加数值标签。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)