【问题标题】:Suppress scientific notation in sklearn.metrics.plot_confusion_matrix抑制 sklearn.metrics.plot_confusion_matrix 中的科学记数法
【发布时间】: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


【解决方案1】:

只需从您的调用参数声明中删除“.format”和 {} 括号:

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

另外,您可以使用'.5g' 来避免十进制0

拍下 from source

【讨论】:

    【解决方案2】:

    只需传递 values_format='' 示例:

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

    【讨论】:

      【解决方案3】:

      如果有人使用seaborn´s heatmap 来绘制混淆矩阵,但上述答案均无效。您应该在混淆矩阵seabornfmt='g' 中关闭科学记数法,如下所示:

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

      【讨论】:

      • 谢谢,这应该是最新的正确答案!
      猜你喜欢
      • 1970-01-01
      • 2013-07-18
      • 2017-03-13
      • 2021-06-17
      • 2021-09-30
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多