栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

混淆矩阵简单例子(混淆矩阵作用)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

混淆矩阵简单例子(混淆矩阵作用)


import matplotlib
import matplotlib.pyplot as plt
# sphinx_gallery_thumbnail_number = 2
import pandas as pd
import numpy as np
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

d="EMD"   ##i 可选项IQ、EMD、VMD
f=8    ##j 可选项4、6、8
k=0.5    ##k 可选项1、0.5、0.1
datapath="./data/{}_test/sample{}/resnet{}/".format(d,k,f)
datasave="./hunxiao_pdf/{}/sample{}/".format(d,k)
data=pd.read_csv(datapath+"label.csv")
pre=data['识别标签']
true=data['原始标签']
pre=np.array(pre)
true=np.array(true)
#生成12个计数变量a0~到a11
a=np.zeros((12,12),dtype=int)

##从csv文件中获得混淆矩阵数据
for i in range(26):
    start=int(i*13000)
    end=int((i+1)*13000)
    pre1=pre[start:end]
    true1=true[start:end]
    for j in range(len(pre1)):
        for z in range(12):

            if pre1[j]==z:
                a[i][z]=int(a[i][z])+1
print(a)
a=np.array(a)

plt.rc('font',family='Times New Roman')
# 热度图,后面是指定的颜色块,可设置其他的不同颜色
font = {'family': 'Times New Roman',
         'weight': 'normal',
         'size': 8,
         }
font1 = {'family': 'Times New Roman',
         'weight': 'normal',
         'size': 10,
         }
###设置每个子图大小
fig, ax = plt.subplots(figsize=(7,7))
# plt.figure(figsize=(5, 5))
##画布颜色
# plt.imshow(a,cmap=plt.cm.Blues)
im = ax.imshow(a,cmap=plt.cm.Blues,)

##设置坐标轴刻度

classes=['BPSK','QPSK','8PSK','OQPSK','2FSK','4FSK','8FSK','16QAM','32QAM','64QAM','4PAM','8PAM']
# We want to show all ticks...

###设置x,y轴长度
ax.set_xticks(np.arange(len(classes)))
ax.set_yticks(np.arange(len(classes)))
# ... and label them with the respective list entries
ax.set_xticklabels(classes,fontdict=font)
ax.set_yticklabels(classes,fontdict=font)

plt.xlabel('Predicted label', font1)
plt.ylabel('True lable', font1)

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
         rotation_mode="anchor")

# Loop over data dimensions and create text annotations.
for i in range(len(classes)):
    for j in range(len(classes)):
        text = ax.text(j, i, a[i, j],
                       ha="center", va="center", color="brown",)

# ax.set_title("data of local classes (in tons/year)")
fig.tight_layout()
plt.savefig(datasave+'resnet{}.pdf'.format(f), bbox_inches='tight')
plt.show()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/772716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号