/
regression.py
293 lines (232 loc) · 9.4 KB
/
regression.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
# stdlib
import random
# local
import progress as pg
import resultset
import spambase
import dataset
import stats
FOLDCOUNT = 10
RANDOMSEED = 1337
INDENT = ' '
###############################################################################
class Regression(object):
def __init__(self):
raise NotImplementedError
@staticmethod
def model(weights, features):
'''Calculate regression for features given weights.
[number] [number] --> number
'''
return stats.dotprod(weights, features)
@staticmethod
def gradient(model, feature, label):
'''Calculate regression gradient for a weight.
number number number --> number
'''
return (model - label) * feature
class LogisticRegression(object):
def __init__(self):
raise NotImplementedError
@staticmethod
def model(weights, features):
'''Calculate logistic regression for features given weights.
[number] [number] --> number
'''
return stats.logistic(Regression.model(weights, features))
@staticmethod
def gradient(model, feature, label):
'''Calculate logistic regression gradient for a weight.
number number number --> number
'''
return model * (1 - model) * Regression.gradient(model, feature,
label)
###############################################################################
class GradientDescent(object):
def __init__(self):
raise NotImplementedError
@staticmethod
def stochastic(weights, datapoint, score, gradient, learningrate):
'''Gradient descend new weights from a single datapoint.
weights -- list of weights
datapoint -- DataPoint instance
score -- function [weights features --> score]
gradient -- function [score feature label --> gradient]
learningrate -- learning rate parameter (lambda)
'''
s = score(weights, datapoint.features)
return [w - learningrate * gradient(s, datapoint.features[j],
datapoint.label) \
for j, w in enumerate(weights)]
@staticmethod
def stochastic_pass(weights, datapoints, score, gradient, learningrate):
'''Gradient descend new weights from a list of datapoints.
This is a wrapper for stochastic which simply completes a full pass
through the datapoints before returning.
weights -- list of weights
datapoints -- list of DataPoint instances
score -- function [weights features --> score]
gradient -- function [score feature label --> gradient]
learningrate -- learning rate parameter (lambda)
'''
newweights = weights[:]
for dp in datapoints:
newweights = GradientDescent.stochastic(newweights, dp, score,
gradient, learningrate)
return newweights
@staticmethod
def batch(weights, datapoints, score, gradient, learningrate):
'''Gradient-descend new weights from a list of datapoints.
weights -- list of weights
datapoints -- list of DataPoint instances
score -- function [weights features --> score]
gradient -- function [score feature label --> gradient]
learningrate -- learning rate parameter (lambda)
'''
return [w - learningrate * sum([gradient(score(weights, dp.features),
dp.features[j],
dp.label) \
for dp in datapoints]) \
for j, w in enumerate(weights)]
@staticmethod
def loop(weights, training, score, gradient, learningrate, reducelr,
gdfunction, maxratio):
'''A gradient descent loop for either batch or stochastic_pass.
weights -- initial weight vector
training -- list of DataPoint instances to train on
score -- function [weights features --> score]
gradient -- function [score feature label --> gradient]
learningrate -- learning rate parameter (lambda)
reducelr -- function [number --> number] to reduce learning rate
gdfunction -- function with a signature like GradientDescent.batch
maxraio -- maximum newerror:olderror ratio before stopping
'''
INDENT = ' '
print INDENT * 2 + 'Initial learning rate:', learningrate
# initialize the error
error = stats.rmse(
[score(weights, dp.features) for dp in training],
[dp.label for dp in training])
print INDENT * 2 + 'Initial Training RMSE:', error
# loop
while True:
# calculate new weights & error
newweights = gdfunction(weights, training, score, gradient,
learningrate)
try:
newerror = stats.rmse(
[score(newweights, dp.features) for dp in training],
[dp.label for dp in training])
except OverflowError:
newerror = error + 1
print INDENT * 3 + 'Training RMSE: Overflow'
else:
if newerror <= error:
print INDENT * 2 + 'Training RMSE: v', newerror
else:
print INDENT * 3 + 'Training RMSE: ^', newerror
# figure out what to do next
if newerror <= error:
ratio = newerror / error
# error went down; accept error and weights
error = newerror
weights = newweights
# do we stop?
if ratio > maxratio:
print INDENT * 2 + 'Finished learning; error ratio:',
print ratio, '>', maxratio
break
else:
# error went up; retry with a smaller lambda
learningrate = reducelr(learningrate)
print INDENT * 3 + 'Retrying with learning rate:', learningrate
# done
return weights
###############################################################################
def main2(gdname, gdfunction, training, regression, learningrate):
'''Perform gradient descent with a learner and analyze the results.'''
# learning rate
try:
initiallr, reducelr = learningrate
suffix = 'dynamic' + str(initiallr)
except TypeError:
initiallr = learningrate
reducelr = lambda x: x
suffix = initiallr
# learn
weights = GradientDescent.loop(
len(training[0].features) * [0.0],
training,
regression.model,
regression.gradient,
initiallr,
reducelr,
gdfunction,
0.99)
# test
terror = stats.rmse(
[regression.model(weights, dp.features) for dp in testing],
[dp.label for dp in testing])
print INDENT * 2 + 'Testing RMSE:', terror
## produce a result set
results = [resultset.DataResult(dp.label,
regression.model(weights, dp.features)) \
for dp in testing]
## ## find a good operating point
## op = resultset.minerrop(results)
## print INDENT * 2 + 'Operating Point:', op
## ## assign predictions
## results = resultset.applyop(op, results)
## output roc data
roc = resultset.rocdata(results)
auc = resultset.auc(roc)
with open('{}-{}_lambda={}_auc={}'.format(regression.__name__, gdname,
suffix, auc).lower(),
mode='wb') as fd:
for fpr, tpr in roc:
fd.write('{}, {}\n'.format(fpr, tpr))
###############################################################################
if __name__ == '__main__':
print
# load from file
spambase.load()
d = spambase.data
# zscore feature values
def pre(lon):
mu = stats.mean(lon)
sd = stats.stddev(stats.mvue(lon, mu))
return stats.zscore(lon, mu, sd)
dataset.applykernel(d, pre)
# insert phantom features
for dp in d:
dp.features = [1.0] + list(dp.features)
# roll into folds
folds = [[] for i in xrange(FOLDCOUNT)]
k = 0 # kurrent fold
for dp in d:
# add to the current fold & switch to the next fold
folds[k].append(dp)
k = (k + 1) % FOLDCOUNT
# unroll to testing & training
testing = folds[0]
training = []
[training.extend(f) for f in folds[1:]]
del folds
# randomize testing
random.seed(RANDOMSEED) # make shuffle the same each time
random.shuffle(training)
print 'Testing count:', len(testing)
print 'Training count:', len(training)
# do the four learners
rates = [0.0001,
(1.0, lambda lr: lr/10),
0.1,
0.01]
for reg in (Regression, Regression.LogisticRegression):
print '== {} =='.format(reg.__name__)
for gdfunc in (GradientDescent.stochastic_pass, GradientDescent.batch):
print INDENT * 1 + '== {} Gradient Descent =='.format(
gdfunc.__name__.capitalize())
lr = rates.pop(0)
main2(gdfunc.__name__, gdfunc, training, reg, lr)
###############################################################################