1+ label_names = ['Non-Flood' , 'Flood' ]
2+
3+ def plot_confusion_matrix (cm ,
4+ target_names ,
5+ title = 'Confusion matrix' ,
6+ cmap = None ,
7+ normalize = True ):
8+ """
9+ given a sklearn confusion matrix (cm), make a nice plot
10+
11+ Arguments
12+ ---------
13+ cm: confusion matrix from sklearn.metrics.confusion_matrix
14+
15+ target_names: given classification classes such as [0, 1, 2]
16+ the class names, for example: ['high', 'medium', 'low']
17+
18+ title: the text to display at the top of the matrix
19+
20+ cmap: the gradient of the values displayed from matplotlib.pyplot.cm
21+ see http://matplotlib.org/examples/color/colormaps_reference.html
22+ plt.get_cmap('jet') or plt.cm.Blues
23+
24+ normalize: If False, plot the raw numbers
25+ If True, plot the proportions
26+
27+ Usage
28+ -----
29+ plot_confusion_matrix(cm = cm, # confusion matrix created by
30+ # sklearn.metrics.confusion_matrix
31+ normalize = True, # show proportions
32+ target_names = y_labels_vals, # list of names of the classes
33+ title = best_estimator_name) # title of graph
34+
35+ Citiation
36+ ---------
37+ http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
38+
39+ """
40+ import matplotlib .pyplot as plt
41+ import numpy as np
42+ import itertools
43+
44+ accuracy = np .trace (cm ) / float (np .sum (cm ))
45+ misclass = 1 - accuracy
46+
47+ true_pos = np .diag (cm )
48+ precision = np .sum (true_pos / np .sum (cm , axis = 0 ))/ 2
49+ recall = np .sum (true_pos / np .sum (cm , axis = 1 ))/ 2
50+
51+ if cmap is None :
52+ cmap = plt .get_cmap ('Blues' )
53+
54+ plt .figure (figsize = (8 , 6 ))
55+ plt .imshow (cm , interpolation = 'nearest' , cmap = cmap )
56+ plt .title (title )
57+ plt .colorbar ()
58+
59+ if target_names is not None :
60+ tick_marks = np .arange (len (target_names ))
61+ plt .xticks (tick_marks , target_names , rotation = 45 )
62+ plt .yticks (tick_marks , target_names )
63+
64+ if normalize :
65+ cm = cm .astype ('float' ) / cm .sum (axis = 1 )[:, np .newaxis ]
66+
67+
68+ thresh = cm .max () / 1.5 if normalize else cm .max () / 2
69+ for i , j in itertools .product (range (cm .shape [0 ]), range (cm .shape [1 ])):
70+ if normalize :
71+ plt .text (j , i , "{:0.4f}" .format (cm [i , j ]),
72+ horizontalalignment = "center" ,
73+ color = "white" if cm [i , j ] > thresh else "black" )
74+ else :
75+ plt .text (j , i , "{:,}" .format (cm [i , j ]),
76+ horizontalalignment = "center" ,
77+ color = "white" if cm [i , j ] > thresh else "black" )
78+
79+
80+ plt .tight_layout ()
81+ plt .ylabel ('True label' )
82+ plt .xlabel ('Predicted label\n accuracy={:0.4f}; misclass={:0.4f}' .format (accuracy , misclass ))
83+ plt .show ()
84+
85+ from sklearn .metrics import confusion_matrix
86+ #Getting cmat from confusion_matrix.py outputs
87+ cmat = np .array ([[ 160 , 40 ],
88+ [ 41 , 159 ]])
89+ plot_confusion_matrix (cm = cmat ,
90+ normalize = False ,
91+ target_names = label_names ,
92+ cmap = plt .get_cmap ('Greens' ),
93+ title = "ResNet101 Confusion Matrix" )
0 commit comments