sklearn打印出混淆矩阵信息
时间: 2023-05-29 17:01:47 浏览: 53
可以使用sklearn中的confusion_matrix()函数来打印出混淆矩阵信息。
以下是一个示例:
```
from sklearn.metrics import confusion_matrix
# 假设y_true和y_pred为真实标签和预测标签
# 这里仅作示例,数据应当根据实际情况自行输入
y_true = [0, 1, 0, 1, 1, 0, 1, 0]
y_pred = [0, 1, 1, 0, 1, 0, 0, 1]
# 计算混淆矩阵
cm = confusion_matrix(y_true, y_pred)
print(cm)
```
输出结果:
```
[[2 2]
[2 2]]
```
解释:这个混淆矩阵说明了我们分类器的性能情况。在总共8个样本中,有2个正样本被正确分类,2个正样本被错误分类成负样本,2个负样本被错误分类成正样本,2个负样本被正确分类。
相关问题
利用python中的matplotlib打印混淆矩阵实例
使用Python中的Matplotlib库可以很方便地打印混淆矩阵。首先,我们需要导入必要的库和数据。
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
# 定义真实标签和预测结果
y_true = [1, 0, 0, 1, 1, 0, 2, 1, 2, 0]
y_pred = [1, 0, 1, 1, 0, 0, 2, 1, 2, 2]
# 计算混淆矩阵
cm = confusion_matrix(y_true, y_pred)
```
然后,我们可以使用Matplotlib库中的imshow函数来可视化混淆矩阵。
```python
# 可视化混淆矩阵
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Greens)
plt.title('Confusion Matrix')
plt.colorbar()
# 设置坐标轴标签
classes = ['Class 0', 'Class 1', 'Class 2']
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
# 设置颜色标签
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
plt.text(j, i, cm[i, j], ha="center", va="center", color="white" if cm[i, j] > thresh else "black")
# 设置坐标轴标签
plt.tight_layout()
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
# 显示混淆矩阵
plt.show()
```
运行上述代码后,将会显示一个带有颜色标注的混淆矩阵,横坐标表示预测结果,纵坐标表示真实标签。每个单元格中的数字表示对应真实标签和预测结果的样本数量。颜色越深表示数量越多,颜色越浅表示数量越少。通过这个混淆矩阵,我们可以直观地了解分类模型的预测情况。
resnet34进行训练后如何打印混淆矩阵
可以使用sklearn.metrics库中的confusion_matrix函数来打印混淆矩阵。具体代码如下:
```python
from sklearn.metrics import confusion_matrix
# 假设y_true和y_pred分别为真实标签和预测标签
cm = confusion_matrix(y_true, y_pred)
print(cm)
```
其中,y_true和y_pred都是一维数组,分别表示真实标签和预测标签。打印出来的混淆矩阵是一个二维数组,行表示真实标签,列表示预测标签。