-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregression_tools.py
More file actions
287 lines (246 loc) · 8.88 KB
/
Copy pathregression_tools.py
File metadata and controls
287 lines (246 loc) · 8.88 KB
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
import numpy as np
import csv
### Generic tools
def batch_iter(y, tx, batch_size, num_batches=1, shuffle=True):
"""
Generate a minibatch iterator for a dataset.
Takes as input two iterables (here the output desired values 'y' and the input data 'tx')
Outputs an iterator which gives mini-batches of `batch_size` matching elements from `y` and `tx`.
Data can be randomly shuffled to avoid ordering in the original data messing with the randomness of the minibatches.
Example of use :
for minibatch_y, minibatch_tx in batch_iter(y, tx, 32):
<DO-SOMETHING>
"""
data_size = len(y)
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_y = y[shuffle_indices]
shuffled_tx = tx[shuffle_indices]
else:
shuffled_y = y
shuffled_tx = tx
for batch_num in range(num_batches):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
if start_index != end_index:
yield shuffled_y[start_index:end_index], shuffled_tx[start_index:end_index]
### Tools for linear regression
def grid_search(y, tx, w0, w1):
"""
Algorithm for grid search.
Takes as input: y, the objective variable
w0 and w1, vectors forming the grid along which optimum is searched
tx, input data
Returns: matrix of losses with an entry for each element in the grid.
"""
losses = np.zeros((len(w0), len(w1)))
for i in range(len(w0)):
for j in range(len(w1)):
w=np.array([w0[i],w1[j]])
losses[i,j]=compute_loss(y,tx,w)
return losses
def compute_loss(y,tx,w):
"""
Compute the MSE loss.
Takes as input: y, the objective variable (batch)
tx, input data (batch)
w, vector of coefficients of the model.
Returns: loss of the model.
"""
loss=np.linalg.norm(y-tx.dot(w))**2/(2*y.shape[0])
return loss
def compute_gradient_mse(y, tx, w):
"""
Compute the gradient of the MSE of the model.
Takes as input: y, the objective variable
tx, input data.
w, vector of coefficients of the model
Returns: gradient of the loss of the model.
"""
err=y-tx.dot(w)
d_L=-tx.transpose().dot(err)/tx.shape[0]
return d_L
def compute_stoch_gradient_mse(y, tx, w):
"""
Compute the gradient of the MSE relative to a batch.
Takes as input: y, the objective variable (batch)
tx, input data (batch)
w, vector of coefficients of the model.
Returns: stochastic gradient of the loss of the model.
"""
err=y-tx.dot(w)
if len(y)>1:
dL=-tx.transpose().dot(err)/tx.shape[0]
else:
dL=-tx.reshape(-1,1)*err
dL=-tx.transpose().dot(err)/tx.shape[0]
return dL
### Tools for ridge regression and the lasso
def compute_stoch_gradient_ridge(y, tx, w, lambda_):
"""
Computes the stochastic gradient in the ridge regression.
Takes as input: y, objective variable (batch)
tx, input data (batch)
w, coefficients of the model
lambda_, regularisation parameter
Returns: stochastic gradient of the loss.
"""
err=y-tx.dot(w)
if len(y)>1:
dL=-tx.transpose().dot(err)/tx.shape[0]+2*lambda_*w
else:
dL=-tx.reshape(-1,1)*err+2*lambda_*w
return dL
def sign(x):
"""
Computes the sign function.
"""
true_vec1=x[:]>0
true_vec2=x[:]<0
x=1*true_vec1-1*true_vec2
return x
def compute_gradient_lasso(y,tx,w,lambda_):
"""
Returns the gradient in the lasso model.
Takes as input: y, objective variable
tx, input data
w, coefficients of the model
lambda_, regularisation parameter
Returns: gradient of the loss in the lasso model.
"""
err=y-tx.dot(w)
dL=-tx.transpose().dot(err)/tx.shape[0]+lambda_*sign(w)
return dL
### Tools for Logistic Regression
def sigmoid(t):
"""
Apply sigmoid function to t.
"""
return(np.exp(t)/(1+np.exp(t)))
def calculate_loss_logistic(y, tx, w):
"""
Computes the cost by negative log likelihood.
Takes as input: y, objective variable
tx, input data
w, coefficients of the model
Returns: negative log-likelihood of the logit.
"""
return -np.sum(y*(np.dot(tx,w))-np.log(1+np.exp(np.dot(tx,w))))
def learning_by_gradient_descent(y, tx, w, gamma):
"""
Does one step of gradient descent using logistic regression.
Takes as input: y, objective variable
tx, input data
w, coefficients of the model
gamma, learning parameter
Return the loss and the updated w.
"""
y = y.reshape(-1,)
# compute the cost
loss=calculate_loss_logistic(y, tx, w)
# compute the gradient
grad=np.transpose(tx).dot(sigmoid(np.dot(tx,w))-y)
# update w
w=w-gamma*grad
return w,loss
def stochastic_gradient_descent_logistic(y, tx, initial_w, batch_size, max_iters, gamma):
"""
Do gradient descent using logistic regression.
Return the loss and the updated w.
Takes as input: y, objective variable
tx, input data
initial_w, initial approximation of the coefficients of the model
batch_size, size of the batch for which the gradient is computed
max_iters, maximum number of iterations of gradient descent
gamma, learning parameter
Return the loss and the updated w.
"""
w=initial_w
for n_iter in range(max_iters):
for mini_y,mini_x in batch_iter(y,tx,batch_size):
g=np.transpose(mini_x)*sigmoid(np.dot(mini_x,w)-mini_y)
w=w-gamma*g
loss=calculate_loss_logistic(y, tx, w)
return w,loss
def learning_by_newton_method(y, tx, w, gamma):
"""
Does one step of gradient descent using Newton's method.
Takes as input: y, objective variable
tx, input data
w, coefficients of the model
gamma, learning parameter
Returns the loss and the updated w.
"""
N = tx.shape[0]
D = tx.shape[1]
y = y.reshape(-1,)
# compute loss
loss=calculate_loss_logistic(y, tx, w)
# compute gradient
grad=np.transpose(tx).dot(sigmoid(np.dot(tx,w))-y)
# compute hessian
s1=sigmoid(np.dot(tx,w))
d = s1 * (1-s1)
H = np.zeros((D,D))
for n in range(N):
c = tx[n,:].reshape(-1,1)
H += c.dot(c.T) * d[n]
# update w
w=w-np.linalg.solve(H,gamma*grad)
return w,loss
def learning_by_penalized_gradient_descent(y, tx, w, gamma, lambda_):
"""
Does one step of gradient descent, using the penalized logistic regression.
Takes as input: y, objective variable
tx, input data
w, coefficients of the model
gamma, learning parameter
lambda_, regularising parameter
Returns the loss and the updated w.
"""
# return loss, gradient:
loss=calculate_loss_logistic(y, tx, w)+0.5*lambda_*np.linalg.norm(w)**2
grad=np.transpose(tx).dot(sigmoid(np.dot(tx,w))-y)+lambda_*w
# update w
w=w-gamma*grad
return w,loss
### Akaike Information Criterion
def AIC(w,l):
"""
Return the Akaike Information Criterion of a model with parameters w and negative log likelihood l.
Takes as input: w, coefficients of the model,
l, negative loglikelihood.
Returns: the AIC of the model estimated.
"""
return -2*w.shape[0]-2*l
### Subsample Extraction
def retrieve_subset(y, x, num_obs, set_seed=1):
"""
Extracts a subset of the data to speed up model estimation.
Takes as input: y, the objective variable
x, input data
num_obs, size of the subsample
seed
Returns: subsample of objective variable y and of model matrix x
"""
# Select randomly a subset
np.random.seed(set_seed)
tot_observation = x.shape[0]
idx = np.random.randint(tot_observation, size=num_obs)
x_small = x[idx,:]
y_small = y[idx]
return y_small , x_small
### File Submission
def create_csv_submission(ids, y_pred, name):
"""
Creates an output file in csv format for submission to kaggle
Takes as input: ids (event ids associated with each prediction)
y_pred (predicted class labels)
name (string name of .csv output file to be created)
"""
with open(name, 'w') as csvfile:
fieldnames = ['Id', 'Prediction']
writer = csv.DictWriter(csvfile, delimiter=",", fieldnames=fieldnames)
writer.writeheader()
for r1, r2 in zip(ids, y_pred):
writer.writerow({'Id':int(r1),'Prediction':int(r2)})