-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostics.py
169 lines (116 loc) · 4.37 KB
/
diagnostics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from datetime import datetime
from sklearn.metrics import roc_auc_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve
import numpy as np
import pickle
import matplotlib.pyplot as plt
import os
import itertools
import seaborn as sn
CLASS_NAMES = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', \
'identity_hate']
class Diagnostics:
"""
Encapsulates all our diagnostics so we can import to our model scripts and keep track of
important stats
Args:
build: string (tf or sklearn) indicating whether model was generated using tf or tensorflow
model_type: string indicating type of model (i.e. logistic, RNN, RF)
preds_targets:nX12 matrix of prediction probabilities and target labels
to use:
diag = Diagnostics(build='tf', model_type='logistic', preds_targets=pred_mat), example in logistic_baseline_tensorflow.py
diag.do_all_diagnostics()
"""
def __init__(self,build, model_type, preds_targets, dataset):
self.build = build #sklearn or tensorflow
self.model_type = model_type
self.preds_targets = preds_targets
self.data = dataset
(ts, micro) = datetime.utcnow().strftime('%Y%m%d%H%M%S.%f').split('.')
ts = "%s%03d" % (ts, int(micro) / 1000)
self.output_dir = 'model_info/' + dataset + "/" + str(build) + "/" + str(model_type) + "/" + ts + "/"
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
def get_confusion_matrices(self, cutoff_threshold = 0.5, save=True):
"""
Args:
cutoff_threshold: value between 0 and 1 used to cutoff positive from negative examples
Returns:
raw confusion matrices as well as pretty plot, 2 x 3 with one confusion matrix for each
independent model
"""
preds = self.preds_targets[:,0:6]
targets = self.preds_targets[:,6:12]
classifications = np.where(preds > cutoff_threshold, 1, 0)
fig, ax_array = plt.subplots(2,3)
ax_array = ax_array.ravel()
cm = []
for i, ax_row in enumerate(ax_array):
classification = classifications[:,i]
target = targets[:,i]
curr_cm = confusion_matrix(target, classification)
cm.append(curr_cm)
norm_cm = curr_cm.astype('float') / curr_cm.sum(axis=1)[:, np.newaxis]
sn.heatmap(norm_cm,annot=True,annot_kws={"size": 16}, ax=ax_row)
ax_row.set_title(CLASS_NAMES[i])
if save:
plot = self.output_dir + 'confusion_matrix_plots.png'
fig.savefig(plot)
raw_cm_fn = self.output_dir + 'confusion_matrices.pkl'
pickle.dump(cm, open(raw_cm_fn, "wb" ) )
plt.show()
def get_roc_plots(self, savefig = True):
"""
returns roc plots for each class
TODO: ADD AUC to legend
"""
preds = self.preds_targets[:,0:6]
targets = self.preds_targets[:,6:12]
fig, ax_array = plt.subplots(2,3)
ax_array = ax_array.ravel()
for i, ax_row in enumerate(ax_array):
fpr, tpr, threshold = roc_curve(targets[:,i], preds[:,i])
ax_row.plot(fpr, tpr, color='darkorange', lw=2)
ax_row.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
ax_row.set_title(CLASS_NAMES[i])
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
if savefig:
fn = self.output_dir + 'roc_plots.png'
fig.savefig(fn)
plt.show()
def make_pred_prob_histograms(self, savefig = True):
"""
for each of 6 classes, overlay probabilities of negative examples with probabilites of
positive examples
TODO: probably want to separate the positive and negative examples since there
are so few positive examples which makes it hard to read
"""
preds = self.preds_targets[:,0:6]
targets = self.preds_targets[:,6:12]
pos_examples = preds[targets==1]
neg_examples = preds[targets==0]
fig, ax_array = plt.subplots(2,3)
ax_array = ax_array.ravel()
for i, ax_row in enumerate(ax_array):
pos_col = preds[:,i]
pos_examples = pos_col[targets[:,i]==1]
neg_col = preds[:,i]
neg_examples = neg_col[targets[:,i]==0]
ax_row.hist(pos_examples)
ax_row.hist(neg_examples)
ax_row.set_title(CLASS_NAMES[i])
if savefig:
fn = self.output_dir + 'pred_prob_hist.png'
fig.savefig(fn)
plt.show()
def do_all_diagnostics(self):
self.get_confusion_matrices()
self.get_roc_plots()
self.make_pred_prob_histograms()
if __name__ == "__main__":
with open('pred_mat.pickle') as f:
pred_mat = pickle.load(f)
diagnostics = Diagnostics(build = 'tf', model_type = 'logistic-zero', preds_targets = pred_mat)
diagnostics.get_confusion_matrices()