-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathslim.py
84 lines (59 loc) · 2.07 KB
/
slim.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
"""
SLIM basic implementation. To understand deeply how it works we encourage you to
read "SLIM: Sparse LInear Methods for Top-N Recommender Systems".
"""
from sklearn.linear_model import SGDRegressor
from util import tsv_to_matrix
from util.metrics import compute_precision
from util.recommender import slim_recommender
from scipy.sparse import lil_matrix
from util import parse_args
import simplejson as json
def slim_train(A, l1_reg=0.001, l2_reg=0.0001):
"""
Computes W matrix of SLIM
This link is useful to understand the parameters used:
http://web.stanford.edu/~hastie/glmnet_matlab/intro.html
Basically, we are using this:
Sum( yi - B0 - xTB) + ...
As:
Sum( aj - 0 - ATwj) + ...
Remember that we are wanting to learn wj. If you don't undestand this
mathematical notation, I suggest you to read section III of:
http://glaros.dtc.umn.edu/gkhome/slim/overview
"""
alpha = l1_reg + l2_reg
l1_ratio = l1_reg / alpha
model = SGDRegressor(
penalty='elasticnet',
fit_intercept=False,
alpha=alpha,
l1_ratio=l1_ratio,
)
# TODO: get dimensions in the right way
m, n = A.shape
# Fit each column of W separately
W = lil_matrix((n, n))
for j in range(n):
if j % 50 == 0:
print('-> %2.2f%%' % ((j / float(n)) * 100))
aj = A[:, j].copy()
# We need to remove the column j before training
A[:, j] = 0
model.fit(A, aj.toarray().ravel())
# We need to reinstate the matrix
A[:, j] = aj
w = model.coef_
# Removing negative values because it makes no sense in our approach
w[w < 0] = 0
for el in w.nonzero()[0]:
W[(el, j)] = w[el]
return W
def main(train_file, test_file):
A = tsv_to_matrix(train_file)
W = slim_train(A)
recommendations = slim_recommender(A, W)
return compute_precision(recommendations, test_file)
args = parse_args()
precisions = main(args.train, args.test)
open(args.output, 'w').write(json.dumps(precisions))