-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
81 lines (72 loc) · 2.93 KB
/
main.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
import pickle
import pandas as pd
from csv import DictReader
from FTRL import FTRL_proximal
def train():
# Train the model using streaming data with epoch as 1, every sample only used once
# in a real off-line training senario, we can set training epoch larger than 1
model = FTRL_proximal()
with open("data/train.csv", "r") as f:
row_generator = DictReader(f)
# the loss is overall average loss on every sample, so
# it will be larger than the loss of test set because of
# the poor predictions at the training begining.
loss = 0
losses = []
for t, row in enumerate(row_generator):
y = int(row["target"])
del row["target"]
y_p, I, w = model.predict(row)
loss += FTRL_proximal.logloss_binary(y_p, y)
model.update_weights(I, w, y_p, y)
if t % 1000 == 0:
print('[Training] {} samples processed, current loss: {}'.format(t+1,
round(loss/(t+1), 5)))
losses.append((t+1, loss/(t+1)))
print("Training done.")
with open("model.pickle", "wb") as f:
f.write(pickle.dumps(model))
def evaluate():
"""
The test data set doesn't have true label. To evaluate and update the model,
one should do it on kaggle website and submit a submit file
The following code simulate the real online predicting and learning senario,
if true label is provided, the model can be updated with the streaming data
"""
with open("model.pickle", "rb") as f:
model = pickle.loads(f.read())
with open("data/test.csv", "r") as f:
row_generator = DictReader(f)
loss = 0
losses = []
print("Start testing.")
for t, row in enumerate(row_generator):
y = int(row["target"]) # actually, no target in the test data
del row["target"]
y_p, I, w = model.predict(row)
loss += FTRL_proximal.logloss_binary(y_p, y)
model.update_weights(I, w, y_p, y)
if t % 1000 == 0:
print('[Evaluating] {} samples processed, current loss: {}'.format(t+1,
round(loss/(t+1), 5)))
losses.append((t+1, loss/(t+1)))
def make_submission_file():
"""
make kaggle submission file
"""
with open("model.pickle", "rb") as f:
model = pickle.loads(f.read())
with open("data/test.csv", "r") as f:
row_generator = DictReader(f)
loss = 0
losses = []
print("Start testing.")
with open("submit.csv", "w") as f:
f.write("ID,target\n")
for t, row in enumerate(row_generator):
y_p, I, w = model.predict(row)
f.write("{},{}\n".format(row["ID"], y_p))
if __name__ == "__main__":
train()
# evaluate()
make_submission_file()