【发布时间】:2020-05-31 02:25:47
【问题描述】:
我试图很好地绘制一个混淆矩阵,所以我关注了scikit-learn's newer version 0.22's in built plot confusion matrix function。但是,我的混淆矩阵值的一个值是 153,但它在混淆矩阵图中显示为 1.5e+02:
在scikit-learn's documentation之后,我发现了这个名为values_format的参数,但我不知道如何操作这个参数,以便它可以抑制科学计数法。我的代码如下。
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import plot_confusion_matrix
# import some data to play with
X = pd.read_csv("datasets/X.csv")
y = pd.read_csv("datasets/y.csv")
class_names = ['Not Fraud (positive)', 'Fraud (negative)']
# Split the data into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
# Run classifier, using a model that is too regularized (C too low) to see
# the impact on the results
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
np.set_printoptions(precision=2)
# Plot non-normalized confusion matrix
titles_options = [("Confusion matrix, without normalization", None),
("Normalized confusion matrix", 'true')]
for title, normalize in titles_options:
disp = plot_confusion_matrix(logreg, X_test, y_test,
display_labels=class_names,
cmap=plt.cm.Greens,
normalize=normalize, values_format = '{:.5f}'.format)
disp.ax_.set_title(title)
print(title)
print(disp.confusion_matrix)
plt.show()
【问题讨论】:
-
使用
plt.grid(False)关闭网格线。 -
试试
values_format='d'之类的,看看是否适合您的目的。 -
values_format=''只是留下了显示数字的标准方式。'.2g'是这个绘图函数的默认值,它将153显示为1.5e+02,默认情况下很奇怪。有关Python docs 格式的更多信息。
标签: python matplotlib scikit-learn