首页 > 其他分享 >【796】Confusion Matrix 混淆矩阵相关

【796】Confusion Matrix 混淆矩阵相关

时间:2022-12-21 15:11:34浏览次数:61  
标签:796 plt matrix Confusion import np C2 confusion Matrix

参考:sklearn.metrics.confusion_matrix

参考:Confusion matrix

参考:Matplotlib setting title bold while using "Times New Roman"

参考:seaborn.heatmap

参考:Choosing Colormaps in Matplotlib(colorbar关键字)


1. 创建 Confusion Matrix (方法一)

import numpy as np
import seaborn as sns
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

# 两个数组存储在npy文件中了,下面为读取
y_true = list(np.load('y_true.npy'))
y_pred = list(np.load('y_pred.npy'))

# 计算混淆矩阵数量
C2 = confusion_matrix(y_true, y_pred, labels=[0, 1, 2])
# 计算百分比
C2_normal = np.round(C2/np.sum(C2, axis=1).reshape(-1, 1), 2)

sns.set()
sns.heatmap(C2_normal, cmap="YlGnBu", annot=True)

# 设置全局字体
plt.rcParams['font.sans-serif'] = 'Times New Roman'
# 设置x轴y轴的显示内容,并加粗
plt.xlabel("Predicted label", fontweight='bold')
plt.ylabel('True label', fontweight='bold')
# 设置x轴y轴的显示刻度
plt.xticks([0, 1, 2], [-1, 0, 1])
plt.yticks([0, 1, 2], [-1, 0, 1])

# 图像显示
#plt.show()

# 图像存储,PDF为矢量图
plt.savefig("Confusion_matrix.pdf")

效果:

通过 Adobe Acrobat 调整标签位置

参考:How to Plot Confusion Matrix Heatmap in Python

参考:Python中生成并绘制混淆矩阵(confusion matrix)


2. 创建 Confusion Matrix (方法二)

import seaborn as sns
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

# 两个数组存储在npy文件中了,下面为读取
y_true = list(np.load('y_true.npy'))
y_pred = list(np.load('y_pred.npy'))

# 计算混淆矩阵数量
C2 = confusion_matrix(y_true, y_pred, labels=[0, 1, 2])
# 计算百分比
C2_normal = np.round(C2/np.sum(C2, axis=1).reshape(-1, 1), 2)

sns.set()
f, ax = plt.subplots()
sns.heatmap(C2_normal, cmap="YlGnBu", annot=True, ax=ax) # heatmap

#ax.set_title('Normalized confusion maxtrix') # title
ax.set_xlabel('Predicted label')
ax.set_ylabel('True label')
ax.xaxis.set_ticklabels([-1, 0, 1])
ax.yaxis.set_ticklabels([-1, 0, 1])

效果:

参考:python画混淆矩阵(confusion matrix)


1. 设置 matplotlib.pyplot 全局字体

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'Times New Roman'

参考:matplotlib字体设置看这一篇就够了


2. 

 

标签:796,plt,matrix,Confusion,import,np,C2,confusion,Matrix
From: https://www.cnblogs.com/alex-bn-lee/p/16996313.html

相关文章

  • UVA 442 Matrix Chain Multiplication
    原题Vjudge题目大意模拟矩阵链乘的计算,如果出现错误就输出error,否则输出总共的乘法次数对于一个矩阵\(A(m\timesn),B(n\timesp)\)乘法次数为\(m\timesn\timesp......
  • WebGL之Matrix4库
    1.Matrix4是由<<WebGL编程指南>>作者写的提供WebGL的4*4矩阵操作的方法库,简化我们编写的代码。源代码共享地址,点击链接:Matrix4源代码。参考:https://www.cnblogs.com/w-wa......
  • 1796.second-largest-digit-in-a-string 字符串中第二大的数字
    问题描述1796.字符串中第二大的数字解题思路遍历就好了代码classSolution{public:intsecondHighest(strings){intfirst=-1;intseco......
  • Leetcode 1796 寻找字符串中第二大的数字
    Leetcode1796寻找字符串中第二大的数字1、两次遍历(省略)2、一次遍历(max和second变量,代码省略)3、范围0-9,桶排序(代码如下)classSolution{public:intsecondHig......
  • SPOJ GCDMAT - GCD OF MATRIX
    简要题意给出三个整数\(T,n,m\),\(T\)组询问,每组询问给出四个整数\(i_1,j_1,i_2,j_2\)(数据保证\(i_1,j_1\leqn\\i_2,j_2\leqm\)),计算:\[\sum_{i=i_1}^{i_2}\sum_{j=......
  • 「遍历」字符串中第二大的数字(力扣第1796题)
    本题为12月3日力扣每日一题题目来源:力扣第1796题题目tag:遍历题面题目描述给你一个混合字符串s,请你返回s中第二大的数字,如果不存在第二大的数字,请你返回-1。混合......
  • 1796. 字符串中第二大的数字
    1796.字符串中第二大的数字classSolution{publicintsecondHighest(Strings){intmax1=-1;intmax2=-1;char[]ch=s.toChar......
  • test cpu performance with matrix multiplication
    一需求:测试cpu计算性能二方法:1.使用一定规模方阵执行乘法运算,不需要保存结果。2.根据CPU核数开启线程执行乘法运算3.事先将线程执行任务放入线程对应的任务容器,然后开启线......
  • leetcode-796-easy
    RotateStringGiventwostringssandgoal,returntrueifandonlyifscanbecomegoalaftersomenumberofshiftsons.Ashiftonsconsistsofmovingthe......
  • P7962 [NOIP2021] 方差
    [NOIP2021]方差时隔一年。我又回来做这个题了。。。我们通过观察是可以发现这里的操作实际上就是交换相邻差分,但是差分\(c_1\)不可被交换。然后如果要求方差最小的话......