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. However, one value of my confusion matrix value is 153, but it appears as 1.5e+02 in the confusion matrix plot:
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. My code is as follows.
JavaScript
x
37
37
1
from sklearn import svm, datasets
2
from sklearn.model_selection import train_test_split
3
from sklearn.metrics import plot_confusion_matrix
4
5
# import some data to play with
6
7
X = pd.read_csv("datasets/X.csv")
8
y = pd.read_csv("datasets/y.csv")
9
10
class_names = ['Not Fraud (positive)', 'Fraud (negative)']
11
12
# Split the data into a training set and a test set
13
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
14
15
# Run classifier, using a model that is too regularized (C too low) to see
16
# the impact on the results
17
logreg = LogisticRegression()
18
logreg.fit(X_train, y_train)
19
20
21
np.set_printoptions(precision=2)
22
23
# Plot non-normalized confusion matrix
24
titles_options = [("Confusion matrix, without normalization", None),
25
("Normalized confusion matrix", 'true')]
26
for title, normalize in titles_options:
27
disp = plot_confusion_matrix(logreg, X_test, y_test,
28
display_labels=class_names,
29
cmap=plt.cm.Greens,
30
normalize=normalize, values_format = '{:.5f}'.format)
31
disp.ax_.set_title(title)
32
33
print(title)
34
print(disp.confusion_matrix)
35
36
plt.show()
37
Advertisement
Answer
Just remove “.format” and the {} brackets from your call parameter declaration:
JavaScript
1
5
1
disp = plot_confusion_matrix(logreg, X_test, y_test,
2
display_labels=class_names,
3
cmap=plt.cm.Greens,
4
normalize=normalize, values_format = '.5f')
5
In addition, you can use '.5g'
to avoid decimal 0’s
Taken from source