-
Notifications
You must be signed in to change notification settings - Fork 258
/
model_selection.py
298 lines (230 loc) · 7.12 KB
/
model_selection.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
# -*- coding: utf-8 -*-
"""
@Author: tushushu
@Date: 2018-11-14 11:02:02
@Last Modified by: tushushu
@Last Modified time: 2018-11-14 11:02:02
"""
from itertools import chain
import numpy as np
from numpy.random import choice, seed
from random import random
def train_test_split(data, label=None, prob=0.7, random_state=None):
"""Split data, label into train set and test set.
Arguments:
data {ndarray} -- Training data.
Keyword Arguments:
label {ndarray} -- Target values.
prob {float} -- Train data expected rate between 0 and 1.
(default: {0.7})
random_state {int} -- Random seed. (default: {None})
Returns:
data_train {ndarray}
data_test {ndarray}
label_train {ndarray}
y_test {ndarray}
"""
# Set random state.
if random_state is not None:
seed(random_state)
# Split data
n_rows, _ = data.shape
k = int(n_rows * prob)
train_indexes = choice(range(n_rows), size=k, replace=False)
test_indexes = np.array([i for i in range(n_rows) if i not in train_indexes])
data_train = data[train_indexes]
data_test = data[test_indexes]
# Split label.
if label is not None:
label_train = label[train_indexes]
label_test = label[test_indexes]
ret = (data_train, data_test, label_train, label_test)
else:
ret = (data_train, data_test)
# Cancel random state.
if random_state is not None:
seed(None)
return ret
def train_test_split_list(X, y, prob=0.7, random_state=None):
"""Split X, y into train set and test set.
Arguments:
X {list} -- 2d list object with int or float.
y {list} -- 1d list object with int or float.
Keyword Arguments:
prob {float} -- Train data expected rate between 0 and 1.
(default: {0.7})
random_state {int} -- Random seed. (default: {None})
Returns:
X_train {list} -- 2d list object with int or float.
X_test {list} -- 2d list object with int or float.
y_train {list} -- 1d list object with int 0 or 1.
y_test {list} -- 1d list object with int 0 or 1.
"""
if random_state is not None:
seed(random_state)
X_train = []
X_test = []
y_train = []
y_test = []
for i in range(len(X)):
if random() < prob:
X_train.append(X[i])
y_train.append(y[i])
else:
X_test.append(X[i])
y_test.append(y[i])
# Make the fixed random_state random again
seed()
return X_train, X_test, y_train, y_test
def get_r2(reg, X, y):
"""Calculate the goodness of fit of regression model.
Arguments:
reg {model} -- regression model.
X {ndarray} -- 2d array object with int or float.
y {ndarray} -- 1d array object with int.
Returns:
float
"""
if isinstance(y, list):
y = np.array(y)
y_hat = reg.predict(X)
if isinstance(y_hat, list):
y_hat = np.array(y_hat)
r2 = _get_r2(y, y_hat)
print("Test r2 is %.3f!\n" % r2)
return r2
def model_evaluation(clf, X, y):
"""Calculate the prediction accuracy, recall, precision and auc
of classification model.
Arguments:
clf {model} -- classification model.
X {ndarray} -- 2d array object with int or float.
y {ndarray} -- 1d array object with int.
Returns:
dict
"""
y_hat = clf.predict(X)
y_hat_prob = clf.predict_prob(X)
if isinstance(y_hat, list):
y_hat = np.array(y_hat)
if isinstance(y_hat_prob, list):
y_hat_prob = np.array(y_hat_prob)
ret = dict()
ret["Accuracy"] = _get_acc(y, y_hat)
ret["Recall"] = _get_recall(y, y_hat)
ret["Precision"] = _get_precision(y, y_hat)
ret["AUC"] = _get_auc(y, y_hat_prob)
for k, v in ret.items():
print("%s: %.3f" % (k, v))
print()
return ret
def _get_r2(y, y_hat):
"""Calculate the goodness of fit.
Arguments:
y {ndarray} -- 1d array object with int.
y_hat {ndarray} -- 1d array object with int.
Returns:
float
"""
m = y.shape[0]
n = y_hat.shape[0]
assert m == n, "Lengths of two arrays do not match!"
assert m != 0, "Empty array!"
sse = ((y - y_hat) ** 2).mean()
sst = y.var()
r2 = 1 - sse / sst
return r2
def _clf_input_check(y, y_hat):
m = len(y)
n = len(y_hat)
elements = chain(y, y_hat)
valid_elements = {0, 1}
assert m == n, "Lengths of two arrays do not match!"
assert m != 0, "Empty array!"
assert all(element in valid_elements
for element in elements), "Array values have to be 0 or 1!"
def _get_acc(y, y_hat):
"""Calculate the prediction accuracy.
Arguments:
y {ndarray} -- 1d array object with int.
y_hat {ndarray} -- 1d array object with int.
Returns:
float
"""
_clf_input_check(y, y_hat)
return (y == y_hat).sum() / len(y)
def _get_precision(y, y_hat):
"""Calculate the prediction precision.
Arguments:
y {ndarray} -- 1d array object with int.
y_hat {ndarray} -- 1d array object with int.
Returns:
float
"""
_clf_input_check(y, y_hat)
true_positive = (y * y_hat).sum()
predicted_positive = y_hat.sum()
return true_positive / predicted_positive
def _get_recall(y, y_hat):
"""Calculate the prediction recall.
Arguments:
y {ndarray} -- 1d array object with int.
y_hat {ndarray} -- 1d array object with int.
Returns:
float
"""
return _get_tpr(y, y_hat)
def _get_tpr(y, y_hat):
"""Calculate the prediction TPR.
Arguments:
y {ndarray} -- 1d array object with int.
y_hat {ndarray} -- 1d array object with int.
Returns:
float
"""
_clf_input_check(y, y_hat)
true_positive = (y * y_hat).sum()
actual_positive = y.sum()
return true_positive / actual_positive
def _get_tnr(y, y_hat):
"""Calculate the prediction TNR.
Arguments:
y {ndarray} -- 1d array object with int.
y_hat {ndarray} -- 1d array object with int.
Returns:
float
"""
_clf_input_check(y, y_hat)
true_negative = ((1 - y) * (1 - y_hat)).sum()
actual_negative = len(y) - y.sum()
return true_negative / actual_negative
def _get_auc(y, y_hat_prob):
"""Calculate the prediction AUC.
Arguments:
y {ndarray} -- 1d array object with int.
y_hat_prob {ndarray} -- 1d array object with int.
Returns:
float
"""
roc = iter(_get_roc(y, y_hat_prob))
tpr_pre, fpr_pre = next(roc)
auc = 0
for tpr, fpr in roc:
auc += (tpr + tpr_pre) * (fpr - fpr_pre) / 2
tpr_pre = tpr
fpr_pre = fpr
return auc
def _get_roc(y, y_hat_prob):
"""Calculate the points of ROC.
Arguments:
y {ndarray} -- 1d array object with int.
y_hat_prob {ndarray} -- 1d array object with int.
Returns:
array
"""
thresholds = sorted(set(y_hat_prob), reverse=True)
ret = [[0, 0]]
for threshold in thresholds:
y_hat = (y_hat_prob >= threshold).astype(int)
ret.append([_get_tpr(y, y_hat), 1 - _get_tnr(y, y_hat)])
return ret