python绘制混淆矩阵_python – 如何绘制混淆矩阵?

参见英文答案 > How to plot confusion matrix with string axis rather than integer in python                                    4个

我正在使用scikit-learn将文本文档(22000)分类为100个类.我使用scikit-learn的混淆矩阵方法来计算混淆矩阵.

model1 = LogisticRegression()

model1 = model1.fit(matrix, labels)

pred = model1.predict(test_matrix)

cm=metrics.confusion_matrix(test_labels,pred)

print(cm)

plt.imshow(cm, cmap='binary')

这就是我的混淆矩阵的样子:

[[3962 325 0 ..., 0 0 0]

[ 250 2765 0 ..., 0 0 0]

[ 2 8 17 ..., 0 0 0]

...,

[ 1 6 0 ..., 5 0 0]

[ 1 1 0 ..., 0 0 0]

[ 9 0 0 ..., 0 0 9]]

但是,我没有收到明确或清晰的情节.有一个更好的方法吗?

解决方法:

您可以使用plt.matshow()而不是plt.imshow(),或者您可以使用seaborn模块的热图(see documentation)来绘制混淆矩阵

import seaborn as sn

import pandas as pd

import matplotlib.pyplot as plt

array = [[33,2,0,0,0,0,0,0,0,1,3],

[3,31,0,0,0,0,0,0,0,0,0],

[0,4,41,0,0,0,0,0,0,0,1],

[0,1,0,30,0,6,0,0,0,0,1],

[0,0,0,0,38,10,0,0,0,0,0],

[0,0,0,3,1,39,0,0,0,0,4],

[0,2,2,0,4,1,31,0,0,0,2],

[0,1,0,0,0,0,0,36,0,2,0],

[0,0,0,0,0,0,1,5,37,5,1],

[3,0,0,0,0,0,0,0,0,39,0],

[0,0,0,0,0,0,0,0,0,0,38]]

df_cm = pd.DataFrame(array, index = [i for i in "ABCDEFGHIJK"],

columns = [i for i in "ABCDEFGHIJK"])

plt.figure(figsize = (10,7))

sn.heatmap(df_cm, annot=True)

标签:text-classification,python,scikit-learn,matrix,matplotlib

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值