Skip to content
Advertisement

how to fix the values displayed in a confusion matrix in exponential form to normal form

While working with my project, I have obtained a confusion matrix from test data as:

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm

Output as:

array([[1102,   88],
   [  85,  725]], dtype=int64)

Using seaborn and matplotlib, I visualized it using the code:

import seaborn as sns
import matplotlib.pyplot as plt     

ax= plt.subplot();
sns.heatmap(cm, annot=True,cmap='Blues',ax=ax);
# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); 
ax.set_ylim(2.0, 0)
ax.set_title('Confusion Matrix');
ax.xaxis.set_ticklabels(['Fake','Real']); 
ax.yaxis.set_ticklabels(['Fake','Real']);

The output obtained is:

Confusion matrix

The problem is values with 3 digits (here 1102 been displayed as 11e+03) or above are being displayed in exponential form.

Is there a way to display it in its normal form?

Advertisement

Answer

You can use the "fmt" option:

cm = np.array([[1102,   88],[85,  725]])

import seaborn as sns
import matplotlib.pyplot as plt     

sns.heatmap(cm, annot=True,fmt="d",cmap='Blues')

enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement