-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhelpers.py
304 lines (244 loc) · 8.59 KB
/
helpers.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
Generally helpful functions.
"""
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
from collections import Counter
from sklearn.metrics import roc_curve, auc, roc_auc_score
from matplotlib.colors import ListedColormap
def show_scatterplot(data, labels, title, xlabel = 'x values', ylabel = 'y values', figsize = (10, 6)):
"""
Produce scatterplot for passed data.
Arguments:
data: The data to plot
Shape: (x, y)
labels: Class labels for data
Shape: (x, )
title: Plot title
Type: String
xlabel (optional): x axis label
Type: String
ylabel (optional): y axis label
Type: String
figsize (optional): Figure size (inches)
Type: (int, int)
Returns:
None
Author: Cameron Trotter
Email: [email protected]
"""
plt.figure(figsize = figsize)
sns.scatterplot(x = data[:,0], y = data[:,1], hue = labels)
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.show()
plt.close()
def plot_line_of_best_fit(classifier, data, labels, title, logistic):
"""
Given a classifier (either linear or logistic regression),
produce visualisation for the line of best fit.
Arguments:
classifier: The classifier trained
Type: sklearn model
data: The data to plot
Shape: (x, y)
labels: Class labels for data
Shape: (x, )
title: Plot title
Type: String
logistic: If the model is logistic or not
Type: Boolean
Returns:
None
Author: Cameron Trotter & Paolo Missier
Email: [email protected] & [email protected]
"""
data_min, data_max = data[:, 0].min(), data[:, 0].max()
w = classifier.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(data_min, data_max)
yy = a * xx - (classifier.intercept_[0]) / w[1]
fig = plt.figure(figsize=(20,6))
fig.subplots_adjust(hspace=1, wspace=0.4)
ax = fig.add_subplot(1, 2, 1)
sns.scatterplot(x=data[:,0],y=data[:,1], hue=labels)
ax.set_title(title)
ax2 = ax.twinx()
sns.regplot(x=xx,y=yy, ax=ax2, line_kws={"color": "red"}, scatter = False, logistic = logistic)
plt.show()
plt.close()
def plot_confusion_matrix(conf_matrix, labels, title):
"""
Plot confusion matrix
Arguments:
conf_matrix: A produced confusion matrix
labels: Class labels for data
Shape: (x, )
title: Plot title
Type: String
Returns:
None
Author: Cameron Trotter
Email: [email protected]
"""
fig, ax = plt.subplots(figsize=(10,6))
sns.heatmap(conf_matrix, annot=True, fmt='.2f', xticklabels=labels, yticklabels=labels)
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.title(title)
plt.show(block=False)
def plot_ROC(clf, XTest, CLTest, CL_pred_Test=None):
"""
Plot ROC
Arguments:
clf: The classifier trained
Type: sklearn model
XTest: Test set data points
Shape: (x, y)
CLTest: Class labels per data point
Shape: (x, )
CL_pred_Test (optional): Predictions on test set
Shape: (x, )
Default: None
Author: Paolo Missier
Email: [email protected]
"""
if hasattr(clf, "decision_function"):
print("using decision_function")
probs = clf.decision_function(XTest)
preds = probs
else:
print("using predict_proba")
probs = clf.predict_proba(XTest)
preds = probs[:,1]
fpr, tpr, threshold = roc_curve(CLTest, preds)
roc_auc = auc(fpr, tpr)
if CL_pred_Test is not None:
print("\n\n====== ROC ======")
print("roc_auc_score = %0.2f" % roc_auc_score(CLTest, CL_pred_Test))
print("auc = %0.2f" % roc_auc)
fig = plt.figure()
plt.title('ROC curve')
plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
def plot_contour_fit(clf, XTrain, CLTrain, XTest, CLTest):
"""
Plot contour fit
Arguments:
clf: The classifier trained
Type: sklearn model
XTrain: Train set data points
Shape: (x, y)
CLTrain: Train class labels per data point
Shape: (x, )
XTest: Test set data points
Shape: (x, y)
CLTest: Test class labels per data point
Shape: (x, )
Author: Paolo Missier
Email: [email protected]
"""
h = .02 # step size in the mesh
fig = plt.figure(figsize=(20,6))
fig.subplots_adjust(hspace=1, wspace=0.4)
cm = plt.cm.RdBu
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
## plot training set
x_min, x_max = XTrain[:, 0].min() - .5, XTrain[:, 0].max() + .5
y_min, y_max = XTrain[:, 1].min() - .5, XTrain[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
ax = fig.add_subplot(1, 2, 1)
sns.scatterplot(x=XTrain[:,0],y=XTrain[:,1], hue=CLTrain, ax=ax) # plot training set
ax.set_title("training set wih contour line")
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
if hasattr(clf, "decision_function"):
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
else:
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
# Put the result into a color plot
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
## plot test set
x_min, x_max = XTest[:, 0].min() - .5, XTest[:, 0].max() + .5
y_min, y_max = XTest[:, 1].min() - .5, XTest[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
ax = fig.add_subplot(1, 2, 2)
sns.scatterplot(x=XTest[:,0],y=XTest[:,1], hue=CLTest, ax=ax) # plot training set
ax.set_title("test set wih contour line")
# Plot the decision boundary.
if hasattr(clf, "decision_function"):
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
else:
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
# Put the result into a color plot
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
def create_imbalance(labels, percentage_imbalance):
"""
Given some labels for a dataset, randomly flip percentage_inbalance of them.
Arguments:
labels: Dataset labels
Shape: (x, )
percentage_inbalance: percentage of labels to flip
Type: float (0.0 - 1.0)
Returns: Dataset labels with percentage_inbalance flipped
Shape: (x, )
Author: Cameron Trotter & Paolo Missier
Email: [email protected] & [email protected]
"""
n = 0
for i in range(len(labels)):
if labels[i] == 0:
if randn() <= percentage_imbalance:
n += 1
labels[i] = 1
print(f"{n} labels values flipped")
print("class labels ratio: %0.2f" % (Counter(labels)[0] / Counter(labels)[1]))
return labels
def downsample(X,CL):
"""
Utilise downsampling to balance a dataset.
Arguments:
X: Data points
Shape: (x, y)
CL: Class labels
Shape: (x, )
Returns:
A rebalanced dataset.
Author: Paolo Missier
Email: [email protected]
"""
## we want to achieve roughly 50% contribution for each class
currentRatio = Counter(CL)[0] / Counter(CL)[1]
print("current class labels ratio: %0.2f" % currentRatio)
if currentRatio < 1:
majority = 1
threshold = 1- currentRatio
else:
majority = 0
threshold = 1 - 1/ currentRatio
n = 0
X_reb = np.arange(0).reshape(0, X.shape[1])
CL_reb = np.arange(0)
for i in range(len(CL)):
if CL[i] == majority and randn() <= threshold:
## removing record
n +=1
else:
## copying record
CL_reb = np.append(CL_reb, CL[i])
X_reb = np.append(X_reb, X[i:1+i], axis=0)
print("%d majority class records removed "% n)
print("new class labels ratio: %0.2f" % (Counter(CL_reb)[0] / Counter(CL_reb)[1]))
print("counts: ",Counter(CL_reb))
return X_reb, CL_reb