画混淆矩阵,并根据矩阵计算准确率、每个类别的精确率和召回率

TP(True Positives):实际为正例并且预测为正例
FP(False Postives):实际为反例但是预测为正例
TN(True Negatives):实际为反例并且预测为反例
FN(False Negatives):实际为正例但是预测为反例

精确率=TP/(TP+FP)
召回率=TP/(TP+FN)

# coding=utf-8
import matplotlib.pyplot as plt
import numpy as np

confusion = np.array(([200,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                      [0,200,0,0,0,0,0,0,0,0,0,0,0,0,0],
                      [0,0,200,0,0,0,0,0,0,0,0,0,0,0,0],
                      [0,0,0,199,0,0,0,1,0,0,0,0,0,0,0],
                      [0,0,0,0,200,0,0,0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,200,0,0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,200,0,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,200,0,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,0,200,0,0,0,0,0,0],
                      [0,0,0,0,0,0,0,1,0,199,0,0,0,0,0],
                      [0,0,0,0,0,0,0,0,0,0,200,0,0,0,0],
                      [0,1,0,0,0,0,0,0,0,0,0,199,0,0,0],
                      [0,0,0,0,0,2,0,0,0,0,0,0,197,0,1],
                      [0,0,0,0,0,0,0,0,0,0,0,0,0,200,0],
                      [0,0,0,0,0,0,0,0,0,0,0,0,0,0,200]
                      ))
classes=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#画出混淆矩阵
def confusion_matrix(confMatrix):
    # 热度图,后面是指定的颜色块,可设置其他的不同颜色
    plt.imshow(confMatrix, cmap=plt.cm.Blues)
    # ticks 坐标轴的坐标点
    # label 坐标轴标签说明
    indices = range(len(confMatrix))
    # 第一个是迭代对象,表示坐标的显示顺序,第二个参数是坐标轴显示列表
    # plt.xticks(indices, [0, 1, 2])
    # plt.yticks(indices, [0, 1, 2])
    plt.xticks(indices, classes,rotation=45)
    plt.yticks(indices, classes)

    plt.colorbar()

    plt.xlabel('预测值')
    plt.ylabel('真实值')
    plt.title('混淆矩阵')

    # plt.rcParams两行是用于解决标签不能显示汉字的问题
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False

    # 显示数据
    for first_index in range(len(confMatrix)):  # 第几行
        for second_index in range(len(confMatrix[first_index])):  # 第几列
            if first_index==second_index:
                plt.text(first_index, second_index, confMatrix[first_index][second_index],va='center',ha='center',color='white')
            else:
                plt.text(first_index, second_index, confMatrix[first_index][second_index], va='center', ha='center')
    # 在matlab里面可以对矩阵直接imagesc(confusion)
    # 显示
    plt.show()


#计算准确率
def calculate_all_prediction(confMatrix):
    '''
    计算总精度,对角线上所有值除以总数
    :return:
    '''
    total_sum=confMatrix.sum()
    correct_sum=(np.diag(confMatrix)).sum()
    prediction=round(100*float(correct_sum)/float(total_sum),2)
    print('准确率:'+str(prediction)+'%')

def calculae_lable_prediction(confMatrix):
    '''
    计算每一个类别的预测精度:该类被预测正确的数除以该类的总数
    '''
    l=len(confMatrix)
    for i in range(l):
        label_total_sum = confMatrix.sum(axis=1)[i]
        label_correct_sum=confMatrix[i][i]
        prediction = round(100 * float(label_correct_sum) / float(label_total_sum), 2)
        print('精确率:'+classes[i]+":"+str(prediction)+'%')

def calculate_label_recall(confMatrix):
    l = len(confMatrix)
    for i in range(l):
        label_total_sum = confMatrix.sum(axis=0)[i]
        label_correct_sum = confMatrix[i][i]
        prediction = round(100 * float(label_correct_sum) / float(label_total_sum), 2)
        print('召回率:'+classes[i] + ":" + str(prediction) + '%')


confusion_matrix(confusion)
calculae_lable_prediction(confusion)
calculate_label_recall(confusion)

结果
在这里插入图片描述

E:\Anaconda\python.exe "E:/Classfication Of Network/z.other/混淆矩阵.py"
精确率:1:100.0%
精确率:2:100.0%
精确率:3:100.0%
精确率:4:99.5%
精确率:5:100.0%
精确率:6:100.0%
精确率:7:100.0%
精确率:8:100.0%
精确率:9:100.0%
精确率:10:99.5%
精确率:11:100.0%
精确率:12:99.5%
精确率:13:98.5%
精确率:14:100.0%
精确率:15:100.0%
召回率:1:100.0%
召回率:2:99.5%
召回率:3:100.0%
召回率:4:100.0%
召回率:5:100.0%
召回率:6:99.01%
召回率:7:100.0%
召回率:8:99.01%
召回率:9:100.0%
召回率:10:100.0%
召回率:11:100.0%
召回率:12:100.0%
召回率:13:100.0%
召回率:14:100.0%
召回率:15:99.5%
  • 18
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在MATLAB中,可以根据混淆矩阵计算每个类别的分类精度。混淆矩阵是一个二维矩阵,用于表示分类器在每个类别上的分类结果。假设混淆矩阵为C,其中C(i, j)表示被正确分类为第i类的样本中,有多少个被错误地分类为第j类。 要计算每个类别的分类精度,可以按照以下步骤进行: 1. 首先,计算混淆矩阵C的行和列的和,分别作为每个类别的总样本数和被正确分类的样本数。 2. 然后,对于每个类别i,将C(i, i)(即被正确分类为第i类的样本数)除以该类别的总样本数,得到该类别的分类精度。 下面是用MATLAB实现的代码示例: ```matlab % 假设混淆矩阵为 C C = [10 2 1; 3 12 0; 2 1 8]; % 计算每个类别的总样本数和被正确分类的样本数 total_samples = sum(C, 2); % 按行求和 correct_samples = diag(C); % 矩阵的对角线元素 % 计算每个类别的分类精度 classification_accuracy = correct_samples ./ total_samples; % 打印每个类别的分类精度 for i = 1:length(classification_accuracy) fprintf('Class %d: %.2f\n', i, classification_accuracy(i)); end ``` 以上代码中,存储了一个3x3的混淆矩阵C。通过计算每行的和,可以得到每个类别的总样本数;而通过取混淆矩阵的对角线元素,可以得到每个类别被正确分类的样本数。然后,通过将被正确分类的样本数除以总样本数,即可得到每个类别的分类精度。最后,通过循环打印出每个类别的分类精度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值