File tree 1 file changed +13
-9
lines changed
1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -18,21 +18,25 @@ def predict(otrain):
18
18
norm = NormalizePositive ()
19
19
train = norm .fit_transform (otrain .T ).T
20
20
21
- proximity = distance .pdist (binary , 'correlation' )
22
- proximity = distance .squareform (proximity )
21
+ dists = distance .pdist (binary , 'correlation' )
22
+ dists = distance .squareform (dists )
23
23
24
- neighbors = proximity .argsort (axis = 1 )
24
+ neighbors = dists .argsort (axis = 1 )
25
25
filled = train .copy ()
26
26
for u in range (filled .shape [0 ]):
27
+ # n_u are the neighbors of user
27
28
n_u = neighbors [u , 1 :]
28
- t_u = train [n_u ].T
29
- b_u = binary [n_u ].T
30
29
for m in range (filled .shape [1 ]):
31
- revs = t_u [m ]
32
- brevs = b_u [m ]
33
- revs = revs [brevs ]
30
+ # This code could be faster using numpy indexing trickery as the
31
+ # cost of readibility (this is left as an exercise to the reader):
32
+ revs = [train [neigh , m ]
33
+ for neigh in n_u
34
+ if binary [neigh , m ]]
34
35
if len (revs ):
35
- revs = revs [:len (revs )// 2 + 1 ]
36
+ n = len (revs )
37
+ n //= 2
38
+ n += 1
39
+ revs = revs [:n ]
36
40
filled [u ,m ] = revs .mean ()
37
41
38
42
return norm .inverse_transform (filled .T ).T
You can’t perform that action at this time.
0 commit comments