-
Notifications
You must be signed in to change notification settings - Fork 21
/
ffm.py
70 lines (54 loc) · 1.52 KB
/
ffm.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
#!/usr/bin/env python
import os
import sys
import csv
import time
import glob
import zipfile
import numpy as np
import xlearn as xl
from data import *
d = pd.read_csv('',header=0,iterator=True)
d = d.get_chunk()
def train():
ffm = xl.create_ffm()
train, test, _ = splitFFM()
ffm.setTrain(train)
ffm.setValidate(test)
model = './modelFFM'
sTime = time.strftime(
'%m%d-%H%M', time.localtime(time.time()))
if not os.path.exists(model): os.mkdir(model)
model = '%s/xlModel_%s.txt' % (model, sTime)
params = {
'epoch' : 100,
'metric' : 'auc',
'task' : 'binary',
'k' : 4,
'lr' : 0.02,
'lambda' : 1e-6,
'stop_window' : 3,
}
ffm.fit(params, model)
def predict():
ffm = xl.create_ffm()
_, _, test = splitFFM()
ffm.setTest(test); ffm.setSigmoid()
folder = './modelFFM'
model = sorted(
glob.glob('./modelFFM/xlModel_*.txt'))[-1]
output = model.replace('Model', 'Output')
ffm.predict(model, output)
df = getMerged('aid', 'uid', kind=2)
df['score'] = np.loadtxt(output)
df.to_csv('submission.csv', index=False)
zipName = '%s/submission.zip' % folder
with zipfile.ZipFile(zipName, 'w') as f:
f.write('submission.csv', compress_type=zipfile.ZIP_DEFLATED)
if __name__ == '__main__':
assert len(sys.argv)==2, 'Failed ...'
kind = int(sys.argv[1])
if kind == 1:
train()
elif kind == 0:
predict()