pyhton分类时画混淆矩阵

1.求取预测的矩阵

import numpy as np
from sklearn.metric import confusion_matrix
from keras import model
from keras.models import predict
c=np.zeros(shape=(9,9)) #建立一个空矩阵,一共分为9类
for i in range (10):
     #取模型进行预测
     a = eval("model_"+"{}".format(i+1)).predict(eval("normalization_test_"+"{}".format(i)),batch_size=1) 
    true_label=np.argmax(eval("y"+"{}".format(i)+"_test"),axis=1)
    pre_label=np.argmax(a,axis=1)
    lmr_matrix=confusion_matrix(true_label,pre_label)
    c=c+lmr_matrix

2 分类的名称

classes=['wave','drink from a bottle','answer phone','clap','tight lace','sit down','stand up','read watch','bow']

3.画图

def paintConfusion_float(lmr_matrix,classes):
    plt.figure(figsize=(15,10))
    plt.imshow(lmr_matrix,interpolation='nearest',cmap=plt.cm.Blues)
    #plt.title('confusion matrix')
    #plt.colorbar()
    tick_marks=np.arange(len(classes))
    plt.xticks(tick_marks,classes,rotation=90,size=18)
    plt.yticks(tick_marks,classes,size=18)
    #plt.xlabel('Pre label',size=20)
    #plt.ylabel('True label',size=20)
    lmr_matrix=lmr_matrix.astype('float')/lmr_matrix.sum(axis=1)[:,np.newaxis]
    fmt='.2f' 
    thresh=lmr_matrix.max()/2.
    for i,j in itertools.product(range(lmr_matrix.shape[0]),range(lmr_matrix.shape[1])):
        plt.text(j, i, format(lmr_matrix[i, j], fmt),
                     horizontalalignment="center",
                     color="black" if lmr_matrix[i, j] > thresh else "red",size=22)
    plt.tight_layout()
    #plt.grid()
    plt.savefig("./sace_image.jpg")
    plt.show()
paintConfusion_float(c,classes)

得到的混淆矩阵

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python中,可以使用混淆矩阵来评估分类器的性能,下面是一个简单的例子: ```python from sklearn.metrics import confusion_matrix # 定义真实的标签和预测的标签 y_true = [0, 1, 2, 0, 1, 2, 0, 1, 2] y_pred = [0, 0, 2, 0, 2, 1, 0, 1, 2] # 计算混淆矩阵 cm = confusion_matrix(y_true, y_pred) print(cm) ``` 输出结果为: ``` array([[3, 0, 0], [1, 2, 1], [1, 0, 2]]) ``` 其中,`array([[3, 0, 0], [1, 2, 1], [1, 0, 2]])` 表示混淆矩阵,其中行表示真实标签,列表示预测标签。例如,第一行第一列(3)表示真实标签为0,预测标签也为0的样本数目有3个。 如果你想更加直观地展示混淆矩阵,可以使用 `matplotlib` 库,如下所示: ```python import matplotlib.pyplot as plt import numpy as np cm = np.array([[3, 0, 0], [1, 2, 1], [1, 0, 2]]) fig, ax = plt.subplots() im = ax.imshow(cm, cmap='Blues') # 显示颜色条 cbar = ax.figure.colorbar(im, ax=ax) # 显示所有的刻度 ax.set(xticks=np.arange(cm.shape[1]), yticks=np.arange(cm.shape[0]), xticklabels=['0', '1', '2'], yticklabels=['0', '1', '2'], title='Confusion matrix', ylabel='True label', xlabel='Predicted label') # 在矩阵中添加文本标签 thresh = cm.max() / 2. for i in range(cm.shape[0]): for j in range(cm.shape[1]): ax.text(j, i, format(cm[i, j], 'd'), ha="center", va="center", color="white" if cm[i, j] > thresh else "black") fig.tight_layout() plt.show() ``` 输出结果为: ![混淆矩阵](https://cdn.jsdelivr.net/gh/ethan-sui/PictureBed/img/20210701180513.png)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值