-
Notifications
You must be signed in to change notification settings - Fork 328
/
models.py
303 lines (227 loc) · 10.5 KB
/
models.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
import numpy
numpy.random.seed(123)
from sklearn import linear_model
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
import xgboost as xgb
from sklearn import neighbors
from sklearn.preprocessing import Normalizer
from keras.models import Sequential
from keras.models import Model as KerasModel
from keras.layers import Input, Dense, Activation, Reshape
from keras.layers import Concatenate
from keras.layers.embeddings import Embedding
from keras.callbacks import ModelCheckpoint
import pickle
def embed_features(X, saved_embeddings_fname):
# f_embeddings = open("embeddings_shuffled.pickle", "rb")
f_embeddings = open(saved_embeddings_fname, "rb")
embeddings = pickle.load(f_embeddings)
index_embedding_mapping = {1: 0, 2: 1, 4: 2, 5: 3, 6: 4, 7: 5}
X_embedded = []
(num_records, num_features) = X.shape
for record in X:
embedded_features = []
for i, feat in enumerate(record):
feat = int(feat)
if i not in index_embedding_mapping.keys():
embedded_features += [feat]
else:
embedding_index = index_embedding_mapping[i]
embedded_features += embeddings[embedding_index][feat].tolist()
X_embedded.append(embedded_features)
return numpy.array(X_embedded)
def split_features(X):
X_list = []
store_index = X[..., [1]]
X_list.append(store_index)
day_of_week = X[..., [2]]
X_list.append(day_of_week)
promo = X[..., [3]]
X_list.append(promo)
year = X[..., [4]]
X_list.append(year)
month = X[..., [5]]
X_list.append(month)
day = X[..., [6]]
X_list.append(day)
State = X[..., [7]]
X_list.append(State)
return X_list
class Model(object):
def evaluate(self, X_val, y_val):
assert(min(y_val) > 0)
guessed_sales = self.guess(X_val)
relative_err = numpy.absolute((y_val - guessed_sales) / y_val)
result = numpy.sum(relative_err) / len(y_val)
return result
class LinearModel(Model):
def __init__(self, X_train, y_train, X_val, y_val):
super().__init__()
self.clf = linear_model.LinearRegression()
self.clf.fit(X_train, numpy.log(y_train))
print("Result on validation data: ", self.evaluate(X_val, y_val))
def guess(self, feature):
return numpy.exp(self.clf.predict(feature))
class RF(Model):
def __init__(self, X_train, y_train, X_val, y_val):
super().__init__()
self.clf = RandomForestRegressor(n_estimators=200, verbose=True, max_depth=35, min_samples_split=2,
min_samples_leaf=1)
self.clf.fit(X_train, numpy.log(y_train))
print("Result on validation data: ", self.evaluate(X_val, y_val))
def guess(self, feature):
return numpy.exp(self.clf.predict(feature))
class SVM(Model):
def __init__(self, X_train, y_train, X_val, y_val):
super().__init__()
self.X_train = X_train
self.y_train = y_train
self.__normalize_data()
self.clf = SVR(kernel='linear', degree=3, gamma='auto', coef0=0.0, tol=0.001,
C=1.0, epsilon=0.1, shrinking=True, cache_size=200, verbose=False, max_iter=-1)
self.clf.fit(self.X_train, numpy.log(self.y_train))
print("Result on validation data: ", self.evaluate(X_val, y_val))
def __normalize_data(self):
self.scaler = StandardScaler()
self.X_train = self.scaler.fit_transform(self.X_train)
def guess(self, feature):
return numpy.exp(self.clf.predict(feature))
class XGBoost(Model):
def __init__(self, X_train, y_train, X_val, y_val):
super().__init__()
dtrain = xgb.DMatrix(X_train, label=numpy.log(y_train))
evallist = [(dtrain, 'train')]
param = {'nthread': -1,
'max_depth': 7,
'eta': 0.02,
'silent': 1,
'objective': 'reg:linear',
'colsample_bytree': 0.7,
'subsample': 0.7}
num_round = 3000
self.bst = xgb.train(param, dtrain, num_round, evallist)
print("Result on validation data: ", self.evaluate(X_val, y_val))
def guess(self, feature):
dtest = xgb.DMatrix(feature)
return numpy.exp(self.bst.predict(dtest))
class HistricalMedian(Model):
def __init__(self, X_train, y_train, X_val, y_val):
super().__init__()
self.history = {}
self.feature_index = [1, 2, 3, 4]
for x, y in zip(X_train, y_train):
key = tuple(x[self.feature_index])
self.history.setdefault(key, []).append(y)
print("Result on validation data: ", self.evaluate(X_val, y_val))
def guess(self, features):
features = numpy.array(features)
features = features[:, self.feature_index]
guessed_sales = [numpy.median(self.history[tuple(feature)]) for feature in features]
return numpy.array(guessed_sales)
class KNN(Model):
def __init__(self, X_train, y_train, X_val, y_val):
super().__init__()
self.normalizer = Normalizer()
self.normalizer.fit(X_train)
self.clf = neighbors.KNeighborsRegressor(n_neighbors=10, weights='distance', p=1)
self.clf.fit(self.normalizer.transform(X_train), numpy.log(y_train))
print("Result on validation data: ", self.evaluate(self.normalizer.transform(X_val), y_val))
def guess(self, feature):
return numpy.exp(self.clf.predict(self.normalizer.transform(feature)))
class NN_with_EntityEmbedding(Model):
def __init__(self, X_train, y_train, X_val, y_val):
super().__init__()
self.epochs = 10
self.checkpointer = ModelCheckpoint(filepath="best_model_weights.hdf5", verbose=1, save_best_only=True)
self.max_log_y = max(numpy.max(numpy.log(y_train)), numpy.max(numpy.log(y_val)))
self.__build_keras_model()
self.fit(X_train, y_train, X_val, y_val)
def preprocessing(self, X):
X_list = split_features(X)
return X_list
def __build_keras_model(self):
input_store = Input(shape=(1,))
output_store = Embedding(1115, 10, name='store_embedding')(input_store)
output_store = Reshape(target_shape=(10,))(output_store)
input_dow = Input(shape=(1,))
output_dow = Embedding(7, 6, name='dow_embedding')(input_dow)
output_dow = Reshape(target_shape=(6,))(output_dow)
input_promo = Input(shape=(1,))
output_promo = Dense(1)(input_promo)
input_year = Input(shape=(1,))
output_year = Embedding(3, 2, name='year_embedding')(input_year)
output_year = Reshape(target_shape=(2,))(output_year)
input_month = Input(shape=(1,))
output_month = Embedding(12, 6, name='month_embedding')(input_month)
output_month = Reshape(target_shape=(6,))(output_month)
input_day = Input(shape=(1,))
output_day = Embedding(31, 10, name='day_embedding')(input_day)
output_day = Reshape(target_shape=(10,))(output_day)
input_germanstate = Input(shape=(1,))
output_germanstate = Embedding(12, 6, name='state_embedding')(input_germanstate)
output_germanstate = Reshape(target_shape=(6,))(output_germanstate)
input_model = [input_store, input_dow, input_promo,
input_year, input_month, input_day, input_germanstate]
output_embeddings = [output_store, output_dow, output_promo,
output_year, output_month, output_day, output_germanstate]
output_model = Concatenate()(output_embeddings)
output_model = Dense(1000, kernel_initializer="uniform")(output_model)
output_model = Activation('relu')(output_model)
output_model = Dense(500, kernel_initializer="uniform")(output_model)
output_model = Activation('relu')(output_model)
output_model = Dense(1)(output_model)
output_model = Activation('sigmoid')(output_model)
self.model = KerasModel(inputs=input_model, outputs=output_model)
self.model.compile(loss='mean_absolute_error', optimizer='adam')
def _val_for_fit(self, val):
val = numpy.log(val) / self.max_log_y
return val
def _val_for_pred(self, val):
return numpy.exp(val * self.max_log_y)
def fit(self, X_train, y_train, X_val, y_val):
self.model.fit(self.preprocessing(X_train), self._val_for_fit(y_train),
validation_data=(self.preprocessing(X_val), self._val_for_fit(y_val)),
epochs=self.epochs, batch_size=128,
# callbacks=[self.checkpointer],
)
# self.model.load_weights('best_model_weights.hdf5')
print("Result on validation data: ", self.evaluate(X_val, y_val))
def guess(self, features):
features = self.preprocessing(features)
result = self.model.predict(features).flatten()
return self._val_for_pred(result)
class NN(Model):
def __init__(self, X_train, y_train, X_val, y_val):
super().__init__()
self.epochs = 10
self.checkpointer = ModelCheckpoint(filepath="best_model_weights.hdf5", verbose=1, save_best_only=True)
self.max_log_y = max(numpy.max(numpy.log(y_train)), numpy.max(numpy.log(y_val)))
self.__build_keras_model()
self.fit(X_train, y_train, X_val, y_val)
def __build_keras_model(self):
self.model = Sequential()
self.model.add(Dense(1000, kernel_initializer="uniform", input_dim=1183))
self.model.add(Activation('relu'))
self.model.add(Dense(500, kernel_initializer="uniform"))
self.model.add(Activation('relu'))
self.model.add(Dense(1))
self.model.add(Activation('sigmoid'))
self.model.compile(loss='mean_absolute_error', optimizer='adam')
def _val_for_fit(self, val):
val = numpy.log(val) / self.max_log_y
return val
def _val_for_pred(self, val):
return numpy.exp(val * self.max_log_y)
def fit(self, X_train, y_train, X_val, y_val):
self.model.fit(X_train, self._val_for_fit(y_train),
validation_data=(X_val, self._val_for_fit(y_val)),
epochs=self.epochs, batch_size=128,
# callbacks=[self.checkpointer],
)
# self.model.load_weights('best_model_weights.hdf5')
print("Result on validation data: ", self.evaluate(X_val, y_val))
def guess(self, features):
result = self.model.predict(features).flatten()
return self._val_for_pred(result)