truth=[[3,98],[2,71],[3,62]
y_true=[]for i inrange(len(truth)):
y_true.append(truth[i][0])
y_pred0=[[3,102],[71,2],[3,62],print("L1=",len(y_pred0))
y_pred0 =[[10]if x ==[]else x for x in y_pred0]
y_pred =[]for j inrange(len(y_pred0)):
y_pred.append(y_pred0[j][0])#print("y_prede=", y_pred)for j inrange(len(y_pred)):if y_pred[j]>6:
y_pred[j]=7#print("y_prede=", y_pred)
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
#plt.figure(figsize=(10, 24), dpi=300) #设置画布的大小和dpi,为了使图片更加清晰
C = confusion_matrix(y_true=y_true, y_pred=y_pred)# 可将'1'等替换成自己的类别,如'cat'。print("Confusion Matrix: ")print(C)
plt.matshow(C, cmap=plt.cm.Reds)# 根据最下面的图按自己需求更改颜色# plt.colorbar()for i inrange(len(C)):for j inrange(len(C)):
plt.annotate(C[j, i], xy=(i, j), horizontalalignment='center', verticalalignment='center',color="white"if C[j, i]>500else"black")
Emotion_kinds=8#plt.tight_layout() # 保证图不重叠
plt.ylabel('True label', fontdict={'family':'Times New Roman','size':15})# 设置字体大小。
plt.xlabel('Predicted label', fontdict={'family':'Times New Roman','size':15})
labels =['CS','Medical','Civil','ECE','biochemistry','MAE','Psychology','others']
plt.yticks(range(Emotion_kinds), labels,fontdict={'family':'Times New Roman','size':10})
plt.xticks(range(Emotion_kinds), labels, rotation=45)# X轴字体倾斜45°# plt.savefig('WebOfScience-bert-WOSv-2rm', format='pdf') #保存图像为png格式
plt.show()
import itertools
import numpy as np
import matplotlib.pyplot as plt
import torch
from sklearn.metrics import confusion_matrix