[英]How can I have a good visualization of the confusion matrix?
I'm working on a classification problem and I wanted to visualize confusion matrix.我正在研究一个分类问题,我想可视化混淆矩阵。 But i have a problem is that the number are not in the middle.但我有一个问题是数字不在中间。 Here you can see the code:在这里你可以看到代码:
lr = LogisticRegression()
lr.fit(X_train,Y_train)
y_test_pred = lr.predict(X_test)
print("The accuracy of the logistic regression : ",(accuracy_score(y_test_pred,Y_test)*100), "%")
confusion_matrix = pd.crosstab(Y_test, y_test_pred, rownames=['Actual'], colnames=['Predicted'])
cm = confusion_matrix.astype('float') / confusion_matrix.sum(axis=1)[:, np.newaxis]
# Visualization
plt.figure
plt.title('Logistic Regression')
sns.heatmap(cm, annot=True)
plt.show()
The output is something like that: screen output 是这样的:屏幕
How can I solve this problem?我怎么解决这个问题?
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
cm=confusion_matrix(y,y_pred)
plt.matshow(cm,cmap=plt.cm.Greens)
plt.colorbar()
plt.show()
plt.matshow() can help. plt.matshow() 可以提供帮助。
Add how to put the number in the middle.添加如何将数字放在中间。 Key words: horizontalalignment, verticalalignment can locate the position of annotation.关键词:horizontalalignment,verticalalignment 可以定位注释的position。
import matplotlib.pyplot as plt
plt.matshow(cm,cmap=plt.cm.Greens) # plot matrix
plt.colorbar()
for i in range(len(cm)):
for j in range(len(cm)):
plt.annotate(cm[i,j],xy=(j,i),horizontalalignment='center',verticalalignment='center',size=15,color='orange')
I ran into the same problem.我遇到了同样的问题。 In my case, I only uninstall the existing version of matplotlib and reinstall it with version 3.0.0.就我而言,我只卸载了现有版本的 matplotlib 并重新安装了 3.0.0 版本。 It works for me, (I know it's not a permanent solution. though).它对我有用,(我知道这不是一个永久的解决方案。不过)。
First:第一的:
pip uninstall matplotlib
And then:接着:
pip install matplotlib==3.0
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.