This repository was archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression.py
More file actions
204 lines (170 loc) · 6.4 KB
/
regression.py
File metadata and controls
204 lines (170 loc) · 6.4 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
import argparse
import csv
from collections import defaultdict
from math import sqrt
from cffi import FFI
class Regressor:
def __init__(self, method: str = 'sgd', iterations: int = 1000, alpha: float = 1e-4,
penalty: str = 'l2',
tolerance: float = 1e-3, shuffle: bool = True, verbose: bool = True,
stumble: int = 12,
eta: float = 1e-2):
if method == 'sgd':
self.method = 1
elif method == 'adagrad':
self.method = 2
elif method == 'rmsprop':
self.method = 3
elif method == 'adam':
self.method = 4
else:
self.method = 1
self.iterations = iterations
self.alpha = alpha
if not penalty:
self.penalty = 0
elif penalty == 'l1':
self.penalty = 1
else:
self.penalty = 2
self.tolerance = tolerance
self.shuffle = int(shuffle)
self.verbose = int(verbose)
self.stumble = stumble
self.eta = eta
def fit(self, n: int) -> [float]:
"""
Fit regressor.
:param n: Number of features.
"""
ffi = FFI()
ffi.cdef("""
void fit(double * buffer, uint64_t n, uint64_t method, uint64_t iterations, double alpha,
uint64_t penalty, double tolerance, uint64_t shuffle, uint64_t verbose, uint64_t stumble, double eta);
""")
C = ffi.dlopen('target/release/libregression.so')
buffer = ffi.new(f'double [{n}]')
C.fit(buffer, n, self.method, self.iterations, self.alpha, self.penalty,
self.tolerance, self.shuffle, self.verbose, self.stumble, self.eta)
weights = ffi.unpack(buffer, n)
return weights
def assess_alpha(k: int, left: float, right: float, size: int, penalty: str) -> float:
ffi = FFI()
ffi.cdef("""
double assess_alpha(uint64_t k, double left, double right, uint64_t size, uint64_t penalty);
""")
C = ffi.dlopen('target/release/libregression.so')
if not penalty:
penalty = 0
elif penalty == 'l1':
penalty = 1
else:
penalty = 2
alpha = C.assess_alpha(k, left, right, size, penalty)
return alpha
def onehot(X):
features = [f for f in X.keys()
if isinstance(X[f][-1], str)]
replacement = {}
for f in features:
column = X[f]
uniques = list(set(column))
for u in uniques:
replacement[f + str(u)] = [1. if v == u else 0. for v in column]
X.pop(f)
X.update(replacement)
def drop(X):
for f in list(X.keys()):
if isinstance(X[f][-1], str):
X.pop(f)
elif X[f].count(X[f][-1]) == len(X[f]):
X.pop(f)
else:
column = [float(x) for x in X[f]]
X[f] = column
def __standard(X):
for f, column in X.items():
mu = sum(column) / len(column)
sigma = sqrt(sum([(v - mu) ** 2 for v in column]) / len(column))
X[f] = [0. if sigma == 0. else (v - mu) / sigma for v in column]
def __minmax(X):
for f, column in X.items():
x, y = min(column), max(column)
X[f] = [0. if x == y else (v - x) / (y - x) for v in column]
def scale(X, method='standard'):
if method == 'standard':
return __standard(X)
elif method == 'minmax':
return __minmax(X)
else:
raise ValueError('Unknown scaling method')
def split(X, k):
length = max([len(v) for v in X.values()])
__train, __test = {}, {}
for f, column in X.items():
__train[f], __test[f] = [], []
for i in range(0, int(k * length)):
__test[f].append(column[i])
for i in range(int(k * length), length):
__train[f].append(column[i])
return __train, __test
def read_csv(filepath):
with open(filepath, 'r') as f:
reader = csv.DictReader(f)
result = defaultdict(list)
# Put value to corresponding feature.
for row in reader:
for k, v in row.items():
try:
result[k].append(float(v))
except ValueError:
result[k].append(v)
result = dict(result)
# Comma may occur on end of line.
if '' in result:
result.pop('', None)
return result
def write_csv(filepath, X):
with open(filepath, 'w+', newline='') as f:
writer = csv.writer(f, delimiter=',')
length = max([len(v) for v in X.values()])
for i in range(0, length):
row = [X[v][i] for v in X.keys()]
writer.writerow(row)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Preprocess data with airline delays.')
parser.add_argument('filename', metavar='FILE', nargs='?', default='data.csv',
help='CSV file with data')
parser.add_argument('--target', nargs='?', required=True, help='Target variable to predict')
parser.add_argument('--drop', nargs='+', help='Columns to drop from data')
parser.add_argument('--scale', nargs='?', choices=['standard', 'minmax'], default='minmax',
help='Method of scaling of numerical data')
parser.add_argument('--split', nargs='?', type=float, default=0.2,
help='Test to train split in %%')
parser.add_argument('--ohe', action='store_true',
help='Apply one-hot encoding to string columns, drop otherwise')
parser.add_argument('--outliers', nargs='?', type=int, default=3,
help='Remove observations further than n standard derivations')
parser.add_argument('--method', choices=['sgd', 'adagrad', 'rmsprop', 'adam'], default='sgd',
help='Optimization method for regression')
args = parser.parse_args()
data = read_csv(args.filename)
if args.drop:
for feature in args.drop:
data.pop(feature)
if args.ohe:
onehot(data)
else:
drop(data)
scale(data, args.scale)
train, test = split(data, args.split)
# Write split data.
write_csv('./data/train/y.csv', {'y': train[args.target]})
train.pop(args.target)
write_csv('./data/train/X.csv', train)
write_csv('./data/test/y.csv', {'y': test[args.target]})
test.pop(args.target)
write_csv('./data/test/X.csv', test)
# Find optimum.
regression = Regressor(method='sgd', alpha=2e-6)
regression.fit(n=15)