简体   繁体   English

在 sklearn.metrics.plot_confusion_matrix 中抑制科学记数法

[英]Suppress scientific notation in sklearn.metrics.plot_confusion_matrix

I was trying to plot a confusion matrix nicely, so I followed scikit-learn's newer version 0.22's in built plot confusion matrix function .我试图很好地绘制一个混淆矩阵,所以我遵循了scikit-learn 的新版本 0.22 的内置绘图混淆矩阵函数 However, one value of my confusion matrix value is 153, but it appears as 1.5e+02 in the confusion matrix plot:但是,我的混淆矩阵值的一个值是 153,但它在混淆矩阵图中显示为 1.5e+02: 在此处输入图片说明

Following the scikit-learn's documentation , I spotted this parameter called values_format , but I do not know how to manipulate this parameter so that it can suppress the scientific notation.按照scikit-learn 的文档,我发现了这个名为values_format参数,但我不知道如何操作这个参数,以便它可以抑制科学记数法。 My code is as follows.我的代码如下。

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()

Just remove ".format" and the {} brackets from your call parameter declaration:只需从调用参数声明中删除“.format”和 {} 括号:

disp = plot_confusion_matrix(logreg, X_test, y_test,
                                 display_labels=class_names,
                                 cmap=plt.cm.Greens,
                                 normalize=normalize, values_format = '.5f')

In addition, you can use '.5g' to avoid decimal 0's此外,您可以使用'.5g'来避免十进制 0

Taken from source 取自来源

只需传递 values_format='' 示例:

plot_confusion_matrix(clf, X_test, Y_test, values_format = '')

In case anyone using seaborn ´s heatmap to plot the confusion matrix, and none of the answer above worked.如果有人使用seabornheatmap来绘制混淆矩阵,并且上述答案均seaborn You should turn off scientific notation in confusion matrix seaborn with fmt='g' , like so:您应该使用fmt='g'关闭混淆矩阵seaborn科学记数法,如下所示:

sns.heatmap(conf_matrix,annot=True, fmt='g')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM