如何用整个数字绘制热图混淆矩阵

How to plot Heatmap confussion matrix with entire numbers

我正在绘制这样的混淆矩阵:

from sklearn.linear_model import LogisticRegression
#Initalize the classifier
clf = LogisticRegression(random_state=0)
#Fitting the training data
clf.fit(X_train, y_train)
#Predicting on test
y_pred=clf.predict(X_test)

from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))

print(f'Accuracy = {accuracy_score(y_test, y_pred):.2f}\nRecall = {recall_score(y_test, y_pred):.2f}\n')
cm = confusion_matrix(y_test, y_pred)
cm_plot = sns.heatmap(cm, annot=True, cmap='Blues');
cm_plot.set_xlabel('Predicted Values')
cm_plot.set_ylabel('Actual Values')
cm_plot.set_title('Confusion Matrix (with SMOTE)', size=16)

我得到了这个结果:

但是当实数混淆矩阵值是这样的时候,它显示的数字像 3.3e+02:

[[327 103]
 [ 51 346]]

如何在热图中绘制实数?

看来您正在使用 Seaborn 绘制热图。您可以使用 seaborn.heatmap's fmt 参数格式化数字。做 cm_plot = sns.heatmap(cm, annot=True, cmap='Blues', fmt='d') 应该有效。