<s> """<STR_LIT>""" <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> import numpy as np <EOL> from itertools import izip <EOL> from operator import itemgetter <EOL> from scipy . sparse import csr_matrix , coo_matrix <EOL> from . . sparse import fast_sparse_matrix <EOL> from . . base_recommender import BaseRecommender <EOL> class ItemSimilarityRecommender ( BaseRecommender ) : <EOL> """<STR_LIT>""" <EOL> def fit ( self , dataset , item_features = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( dataset , fast_sparse_matrix ) : <EOL> dataset = fast_sparse_matrix ( dataset ) <EOL> num_users , num_items = dataset . shape <EOL> data = [ ] <EOL> row = [ ] <EOL> col = [ ] <EOL> for j in xrange ( num_items ) : <EOL> w = self . compute_similarities ( dataset , j ) <EOL> for k , v in enumerate ( w ) : <EOL> if v != <NUM_LIT:0> : <EOL> data . append ( v ) <EOL> row . append ( j ) <EOL> col . append ( k ) <EOL> idx = np . array ( [ row , col ] , dtype = '<STR_LIT>' ) <EOL> self . similarity_matrix = csr_matrix ( ( data , idx ) , ( num_items , num_items ) ) <EOL> def _create_archive ( self ) : <EOL> """<STR_LIT>""" <EOL> tmp = self . similarity_matrix <EOL> self . similarity_matrix = None <EOL> m = pickle . dumps ( self ) <EOL> self . similarity_matrix = tmp <EOL> if isinstance ( self . similarity_matrix , np . ndarray ) : <EOL> archive = { '<STR_LIT>' : self . similarity_matrix , '<STR_LIT>' : m } <EOL> elif isinstance ( self . similarity_matrix , csr_matrix ) : <EOL> d = self . similarity_matrix . tocoo ( copy = False ) <EOL> archive = { '<STR_LIT>' : d . row , '<STR_LIT>' : d . col , '<STR_LIT:data>' : d . data , '<STR_LIT>' : d . shape , '<STR_LIT>' : m } <EOL> else : <EOL> archive = None <EOL> return archive <EOL> def _load_archive ( self , archive ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in archive . files : <EOL> self . similarity_matrix = archive [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in archive . files : <EOL> data = archive [ '<STR_LIT:data>' ] <EOL> row = archive [ '<STR_LIT>' ] <EOL> col = archive [ '<STR_LIT>' ] <EOL> shape = archive [ '<STR_LIT>' ] <EOL> self . similarity_matrix = coo_matrix ( ( data , ( row , col ) ) , shape = shape ) . tocsr ( ) <EOL> else : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> def load_similarity_matrix ( self , filepath , num_items , offset = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> y = np . loadtxt ( filepath ) <EOL> row = y [ : , <NUM_LIT:0> ] <EOL> col = y [ : , <NUM_LIT:1> ] <EOL> data = y [ : , <NUM_LIT:2> ] <EOL> idx = np . array ( [ row , col ] , dtype = '<STR_LIT>' ) - offset <EOL> self . similarity_matrix = csr_matrix ( ( data , idx ) , ( num_items , num_items ) ) <EOL> def compute_similarities ( self , dataset , j ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_similar_items ( self , j , max_similar_items = <NUM_LIT:30> , dataset = None ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) and self . similarity_matrix is not None : <EOL> w = zip ( self . similarity_matrix [ j ] . indices , self . similarity_matrix [ j ] . data ) <EOL> sims = sorted ( w , key = itemgetter ( <NUM_LIT:1> ) , reverse = True ) [ : max_similar_items ] <EOL> sims = [ ( i , f ) for i , f in sims if f > <NUM_LIT:0> ] <EOL> else : <EOL> w = self . compute_similarities ( dataset , j ) <EOL> sims = [ ( i , w [ i ] ) for i in w . argsort ( ) [ - <NUM_LIT:1> : - max_similar_items - <NUM_LIT:1> : - <NUM_LIT:1> ] if w [ i ] > <NUM_LIT:0> ] <EOL> return sims <EOL> def recommend_items ( self , dataset , u , max_items = <NUM_LIT:10> , return_scores = True , item_features = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> r = ( self . similarity_matrix * dataset [ u ] . T ) . toarray ( ) . flatten ( ) <EOL> except AttributeError : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> known_items = set ( dataset [ u ] . indices ) <EOL> recs = [ ] <EOL> for i in r . argsort ( ) [ : : - <NUM_LIT:1> ] : <EOL> if i not in known_items : <EOL> if return_scores : <EOL> recs . append ( ( i , r [ i ] ) ) <EOL> else : <EOL> recs . append ( i ) <EOL> if len ( recs ) >= max_items : <EOL> break <EOL> return recs <EOL> def batch_recommend_items ( self , <EOL> dataset , <EOL> max_items = <NUM_LIT:10> , <EOL> return_scores = True , <EOL> show_progress = False , <EOL> item_features = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> r = dataset * self . similarity_matrix . T <EOL> except AttributeError : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> return self . _get_recommendations_from_predictions ( r , dataset , <NUM_LIT:0> , r . shape [ <NUM_LIT:0> ] , max_items , return_scores , show_progress ) <EOL> def range_recommend_items ( self , <EOL> dataset , <EOL> user_start , <EOL> user_end , <EOL> max_items = <NUM_LIT:10> , <EOL> return_scores = True , <EOL> item_features = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> r = dataset [ user_start : user_end , : ] * self . similarity_matrix . T <EOL> except AttributeError : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> return self . _get_recommendations_from_predictions ( r , dataset , user_start , user_end , max_items , return_scores ) <EOL> def _get_recommendations_from_predictions ( self , r , dataset , user_start , user_end , max_items , return_scores = True , show_progress = False ) : <EOL> """<STR_LIT>""" <EOL> r = self . _zero_known_item_scores ( r , dataset [ user_start : user_end , : ] ) <EOL> recs = [ [ ] for u in xrange ( user_start , user_end ) ] <EOL> for u in xrange ( user_start , user_end ) : <EOL> ux = u - user_start <EOL> if show_progress and ux % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> print ux , '<STR_LIT:..>' , <EOL> ru = r [ ux , : ] <EOL> if return_scores : <EOL> recs [ ux ] = [ ( i , v ) for v , i in sorted ( izip ( ru . data , ru . indices ) , reverse = True ) if v > <NUM_LIT:0> ] [ : max_items ] <EOL> else : <EOL> recs [ ux ] = [ i for v , i in sorted ( izip ( ru . data , ru . indices ) , reverse = True ) if v > <NUM_LIT:0> ] [ : max_items ] <EOL> if show_progress : <EOL> print <EOL> return recs </s>
