如何为 Seaborn Confusion Matrix 添加正确的标签 [英] How to add correct labels for Seaborn Confusion Matrix

查看:62
本文介绍了如何为 Seaborn Confusion Matrix 添加正确的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 seaborn 将我的数据绘制成一个混淆矩阵,但我遇到了一个问题.问题是它只在两个轴上显示从 0 到 11 的数字,因为我有 12 个不同的标签.

I have plotted my data into a confusion matrix using seaborn but I ran into a problem. The problem is that it is only showing numbers from 0 to 11, on both axes, because I have 12 different labels.

我的代码如下:

cf_matrix = confusion_matrix(y_test, y_pred)
fig, ax = plt.subplots(figsize=(15,10)) 
sns.heatmap(cf_matrix, linewidths=1, annot=True, ax=ax, fmt='g')

在这里你可以看到我的混淆矩阵:

Here you can see my confusion matrix:

我得到了我应该得到的混淆矩阵.唯一的问题是未显示的标签名称.我在互联网上搜索了很长时间,但没有运气.是否有任何可以附加标签的参数或如何做到这一点?

I am getting the confusion matrix as I should. The only problem is the names of the labels which are not shown. I have search quite a while over the internet and with no luck. Are there any parameters which can attach the labels or how can this be done?

有人可以帮我吗?

谢谢!

//拉斯姆斯

推荐答案

当您分解类别时,您应该保留级别,因此您可以将其与 pd.crosstab 结合使用,而不是confusion_matrix 进行绘图.以鸢尾花为例:

When you factorize your categories, you should have retained the levels, so you can use that in conjunction with pd.crosstab instead of confusion_matrix to plot. Using iris as example:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.metrics import classification_report, confusion_matrix

df = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
                 header=None,names=["s.wid","s.len","p.wid","p.len","species"])
X = df.iloc[:,:4]
y,levels = pd.factorize(df['species'])

在这部分,你得到了 [0,..1,..2] 中的标签 y 和级别作为 0,1,2 对应的原始标签:

At this part, you get the labels y in [0,..1,..2] and levels as the original labels to which 0,1,2 corresponds to:

Index(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype='object')

所以我们适合你所拥有的:

So we fit and do like what you have:

clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(X,y)
y_pred = clf.predict(X)
print(classification_report(y,y_pred,target_names=levels))

还有一个带有 0,1,2 的混淆矩阵:

And a confusion matrix with 0,1,2:

cf_matrix = confusion_matrix(y, y_pred)
sns.heatmap(cf_matrix, linewidths=1, annot=True, fmt='g')

我们回去使用关卡:

cf_matrix = pd.crosstab(levels[y],levels[y_pred])
fig, ax = plt.subplots(figsize=(5,5))
sns.heatmap(cf_matrix, linewidths=1, annot=True, ax=ax, fmt='g')

这篇关于如何为 Seaborn Confusion Matrix 添加正确的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆