MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / plot_confusion_matrix

Function plot_confusion_matrix

cnn_class2/util.py:14–47  ·  view source on GitHub ↗

This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`.

(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues)

Source from the content-addressed store, hash-verified

12
13
14def plot_confusion_matrix(cm, classes,
15 normalize=False,
16 title='Confusion matrix',
17 cmap=plt.cm.Blues):
18 """
19 This function prints and plots the confusion matrix.
20 Normalization can be applied by setting `normalize=True`.
21 """
22 if normalize:
23 cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
24 print("Normalized confusion matrix")
25 else:
26 print('Confusion matrix, without normalization')
27
28 print(cm)
29
30 plt.imshow(cm, interpolation='nearest', cmap=cmap)
31 plt.title(title)
32 plt.colorbar()
33 tick_marks = np.arange(len(classes))
34 plt.xticks(tick_marks, classes, rotation=45)
35 plt.yticks(tick_marks, classes)
36
37 fmt = '.2f' if normalize else 'd'
38 thresh = cm.max() / 2.
39 for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
40 plt.text(j, i, format(cm[i, j], fmt),
41 horizontalalignment="center",
42 color="white" if cm[i, j] > thresh else "black")
43
44 plt.tight_layout()
45 plt.ylabel('True label')
46 plt.xlabel('Predicted label')
47 plt.show()
48
49
50def y2indicator(Y):

Calls

no outgoing calls

Tested by

no test coverage detected