Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. def plot_confusion_matrix(cm, classes,
  2. normalize=False,
  3. title='Confusion matrix',
  4. cmap=plt.cm.Blues):
  5. """
  6. This function prints and plots the confusion matrix.
  7. Normalization can be applied by setting `normalize=True`.
  8. """
  9. if normalize:
  10. cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
  11. print("Normalized confusion matrix")
  12. else:
  13. print('Confusion matrix, without normalization')
  14.  
  15. print(cm)
  16.  
  17. plt.imshow(cm, interpolation='nearest', cmap=cmap)
  18. plt.title(title)
  19. plt.colorbar()
  20. tick_marks = np.arange(len(classes))
  21. plt.xticks(tick_marks, classes, rotation=45)
  22. plt.yticks(tick_marks, classes)
  23. plt.axes().grid(b=None)
  24.  
  25. fmt = '.2f' if normalize else 'd'
  26. thresh = cm.max() / 2.
  27. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
  28. plt.text(j, i, format(cm[i, j], fmt),
  29. horizontalalignment="center",
  30. color="white" if cm[i, j] > thresh else "black")
  31.  
  32. plt.ylabel('True label')
  33. plt.xlabel('Predicted label')
  34. # plt.tight_layout()
  35.  
  36. fig = plt.gcf()
  37. fig.set_size_inches(15,12)
  38.  
  39. plt.show()
  40.  
  41.  
  42. np.set_printoptions(precision=2)
  43.  
  44.  
  45.  
  46. for name, conf_matrix, classes in cms:
  47. plot_confusion_matrix(conf_matrix, classes, title=name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement