<s> import numpy as np <EOL> import scipy . sparse as sparse <EOL> from scipy . sparse . linalg import spsolve <EOL> import time <EOL> def load_matrix ( filename , num_users , num_items ) : <EOL> t0 = time . time ( ) <EOL> counts = np . zeros ( ( num_users , num_items ) ) <EOL> total = <NUM_LIT:0.0> <EOL> num_zeros = num_users * num_items <EOL> for i , line in enumerate ( open ( filename , '<STR_LIT:r>' ) ) : <EOL> user , item , count = line . strip ( ) . split ( '<STR_LIT:\t>' ) <EOL> user = int ( user ) <EOL> item = int ( item ) <EOL> count = float ( count ) <EOL> if user >= num_users : <EOL> continue <EOL> if item >= num_items : <EOL> continue <EOL> if count != <NUM_LIT:0> : <EOL> counts [ user , item ] = count <EOL> total += count <EOL> num_zeros -= <NUM_LIT:1> <EOL> if i % <NUM_LIT> == <NUM_LIT:0> : <EOL> print '<STR_LIT>' % i <EOL> alpha = num_zeros / total <EOL> print '<STR_LIT>' % alpha <EOL> counts *= alpha <EOL> counts = sparse . csr_matrix ( counts ) <EOL> t1 = time . time ( ) <EOL> print '<STR_LIT>' % ( t1 - t0 ) <EOL> return counts <EOL> class ImplicitMF ( ) : <EOL> def __init__ ( self , counts , num_factors = <NUM_LIT> , num_iterations = <NUM_LIT:30> , <EOL> reg_param = <NUM_LIT> ) : <EOL> self . counts = counts <EOL> self . num_users = counts . shape [ <NUM_LIT:0> ] <EOL> self . num_items = counts . shape [ <NUM_LIT:1> ] <EOL> self . num_factors = num_factors <EOL> self . num_iterations = num_iterations <EOL> self . reg_param = reg_param <EOL> def train_model ( self ) : <EOL> self . user_vectors = np . random . normal ( size = ( self . num_users , <EOL> self . num_factors ) ) <EOL> self . item_vectors = np . random . normal ( size = ( self . num_items , <EOL> self . num_factors ) ) <EOL> for i in xrange ( self . num_iterations ) : <EOL> t0 = time . time ( ) <EOL> print '<STR_LIT>' <EOL> self . user_vectors = self . iteration ( True , sparse . csr_matrix ( self . item_vectors ) ) <EOL> print '<STR_LIT>' <EOL> self . item_vectors = self . iteration ( False , sparse . csr_matrix ( self . user_vectors ) ) <EOL> t1 = time . time ( ) <EOL> print '<STR_LIT>' % ( i + <NUM_LIT:1> , t1 - t0 ) <EOL> def iteration ( self , user , fixed_vecs ) : <EOL> num_solve = self . num_users if user else self . num_items <EOL> num_fixed = fixed_vecs . shape [ <NUM_LIT:0> ] <EOL> YTY = fixed_vecs . T . dot ( fixed_vecs ) <EOL> eye = sparse . eye ( num_fixed ) <EOL> lambda_eye = self . reg_param * sparse . eye ( self . num_factors ) <EOL> solve_vecs = np . zeros ( ( num_solve , self . num_factors ) ) <EOL> t = time . time ( ) <EOL> for i in xrange ( num_solve ) : <EOL> if user : <EOL> counts_i = self . counts [ i ] . toarray ( ) <EOL> else : <EOL> counts_i = self . counts [ : , i ] . T . toarray ( ) <EOL> CuI = sparse . diags ( counts_i , [ <NUM_LIT:0> ] ) <EOL> pu = counts_i . copy ( ) <EOL> pu [ np . where ( pu != <NUM_LIT:0> ) ] = <NUM_LIT:1.0> <EOL> YTCuIY = fixed_vecs . T . dot ( CuI ) . dot ( fixed_vecs ) <EOL> YTCupu = fixed_vecs . T . dot ( CuI + eye ) . dot ( sparse . csr_matrix ( pu ) . T ) <EOL> xu = spsolve ( YTY + YTCuIY + lambda_eye , YTCupu ) <EOL> solve_vecs [ i ] = xu <EOL> if i % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> print '<STR_LIT>' % ( i , time . time ( ) - t ) <EOL> t = time . time ( ) <EOL> return solve_vecs </s>
