66# It is made available under the MIT License
77
88import numpy as np
9- from sklearn .linear_model import LassoCV , RidgeCV , ElasticNetCV
9+ from sklearn .linear_model import ElasticNetCV
1010from sklearn .cross_validation import KFold
11- from load_ml100k import load
1211
1312
1413def learn_for (reviews , i ):
1514 reg = ElasticNetCV (fit_intercept = True , alphas = [
1615 0.0125 , 0.025 , 0.05 , .125 , .25 , .5 , 1. , 2. , 4. ])
16+ nusers ,nmovies = reviews .shape
1717 u = reviews [i ]
18- us = range (reviews .shape [0 ])
19- del us [ i ]
20- ps , = np .where (u .toarray (). ravel () > 0 )
18+ us = np . arange (reviews .shape [0 ])
19+ us = np . delete ( us , i )
20+ ps , = np .where (u .ravel () > 0 )
2121 x = reviews [us ][:, ps ].T
22- y = u .data
23- kf = KFold (len (y ), n_folds = 4 )
22+ kf = KFold (len (ps ), n_folds = 4 )
2423 predictions = np .zeros (len (ps ))
2524 for train , test in kf :
26- xc = x [train ].copy (). toarray ()
25+ xc = x [train ].copy ()
2726 x1 = np .array ([xi [xi > 0 ].mean () for xi in xc ])
2827 x1 = np .nan_to_num (x1 )
2928
3029 for i in range (xc .shape [0 ]):
3130 xc [i ] -= (xc [i ] > 0 ) * x1 [i ]
3231
33- reg .fit (xc , y [train ] - x1 )
32+ reg .fit (xc , u [train ] - x1 )
3433
35- xc = x [test ].copy (). toarray ()
34+ xc = x [test ].copy ()
3635 x1 = np .array ([xi [xi > 0 ].mean () for xi in xc ])
3736 x1 = np .nan_to_num (x1 )
3837
3938 for i in range (xc .shape [0 ]):
4039 xc [i ] -= (xc [i ] > 0 ) * x1 [i ]
4140
42- p = np . array ( map ( reg .predict , xc ) ).ravel ()
41+ p = reg .predict ( xc ).ravel ()
4342 predictions [test ] = p
44- return predictions
43+ fill_preds = np .zeros (nmovies )
44+ fill_preds [ps ] = predictions
45+ return fill_preds
4546
4647
4748def all_estimates (reviews ):
@@ -50,3 +51,4 @@ def all_estimates(reviews):
5051 s = learn_for (reviews , i )
5152 whole_data .append (s )
5253 return np .array (whole_data )
54+
0 commit comments