forked from keyunluo/python-ffm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (50 loc) · 1.38 KB
/
Copy pathutils.py
File metadata and controls
56 lines (50 loc) · 1.38 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
# coding: utf-8
try:
import cPickle as pickle
except ImportError:
import pickle
def save_data(data, path=None):
'''
save data to pickle format
'''
if path is None:
path = './data.pkl'
pickle.dump(data, open(path, "wb"), protocol=4)
print("Save Data to %s Successfully" % path)
def load_data(path):
'''
load data from pickle to FFMData
'''
X, y = pickle.load(open(path, 'rb'))
return X, y
def save_libffm(X, y, path=None):
'''
save data to original libffm format
'''
if path is None:
path = './data.libffm'
convert_func = lambda t: '{}:{}:{}'.format(t[0], t[1], t[2])
with open(path, 'wt') as f:
for label, data in zip(y, X):
line = "%d " % label + " ".join([convert_func(t) for t in data]) + '\n'
f.write(line)
print("Save Libffm Data to %s Done." % path)
def load_libffm(path):
'''
load original libffm data format
'''
X, y = [], []
with open(path, 'rt') as f:
for line in f:
line = str.strip(line)
line = line.split(' ')
if len(line) < 2:
continue
y.append(int(line[0]))
l = []
for t in line[1:]:
t = t.split(':')
tp = tuple([int(t[0]), int(t[1]), float(t[2])])
l.append(tp)
X.append(l)
return X, y