Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. from sklearn.metrics import confusion_matrix
  2. from matplotlib import pyplot as plt
  3.  
  4. # Plot confusion matrix
  5. def plot_confusion_matrix(cm, classes,
  6. normalize=False,
  7. title='Confusion matrix',
  8. cmap=plt.cm.Blues):
  9. """
  10. This function prints and plots the confusion matrix.
  11. Normalization can be applied by setting `normalize=True`.
  12. """
  13. plt.imshow(cm, interpolation='nearest', cmap=cmap)
  14. plt.title(title)
  15. plt.colorbar()
  16. tick_marks = np.arange(len(classes))
  17. plt.xticks(tick_marks, classes, rotation=45)
  18. plt.yticks(tick_marks, classes)
  19.  
  20. if normalize:
  21. cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
  22. cm = np.round(cm,2)
  23. print("Normalized confusion matrix")
  24. else:
  25. print('Modelling: Confusion matrix, without normalization')
  26.  
  27. thresh = cm.max() / 2.
  28. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
  29. plt.text(j, i, cm[i, j],
  30. horizontalalignment="center",
  31. color="white" if cm[i, j] > thresh else "black")
  32.  
  33. plt.tight_layout()
  34. plt.ylabel('True label')
  35. plt.xlabel('Predicted label')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement