python seaborn+confusion_matrix 绘制混淆矩阵

发布于:2024-05-19 ⋅ 阅读:(130) ⋅ 点赞:(0)

原文链接:link

拓展使用:感觉我画的更好看~

import seaborn as sebrn
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as atlas
import random

# y_true = ["bat", "ball", "ball", "bat", "bat", "bat"]
# y_pred = ["bat", "bat", "ball", "ball", "bat", "bat"]
y_true = [random.randint(0,6) for i in range(100)]
y_pred = [random.randint(0,6) for i in range(100)]
# 'SU':0, 'AF':1, 'DI':2, 'HA':3, 'SA':4, 'AN':5, 'NE':6

conf_matrix = confusion_matrix(y_true, y_pred, labels=list(range(7)))
conf_matrix = [conf_matrix[i]*1.0/sum(conf_matrix[i]) for i in range(len(conf_matrix))]

# Using Seaborn heatmap to create the plot
fx = sebrn.heatmap(conf_matrix, annot=True, cmap="turbo")

# labels the title and x, y axis of plot
fx.set_title("Plotting Confusion Matrix using Seaborn\n\n")
fx.set_xlabel("Predicted Values")
fx.set_ylabel("Actual Values ")

# labels the boxes
fx.xaxis.set_ticklabels(['suprised', 'afraid', 'disgust', 'happy', 'sad', 'angry', 'neutral'])
fx.yaxis.set_ticklabels(['suprised', 'afraid', 'disgust', 'happy', 'sad', 'angry', 'neutral'])

atlas.show()

在这里插入图片描述

  • random.randint、random、randrange产生随机整数、小数、随机周期整数

网站公告

今日签到

点亮在社区的每一天
去签到