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)
| 12 | |
| 13 | |
| 14 | def 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 | |
| 50 | def y2indicator(Y): |
no outgoing calls
no test coverage detected