<s> import numpy as np <EOL> from scipy import sparse <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class Lsa ( object ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self , corpus = None , context_type = None , td_matrix = None ) : <EOL> """<STR_LIT>""" <EOL> self . word_matrix = None <EOL> self . doc_matrix = None <EOL> self . eigenvalues = None <EOL> self . context_type = context_type <EOL> if corpus is not None : <EOL> self . corpus = corpus . corpus <EOL> else : <EOL> self . corpus = [ ] <EOL> if td_matrix is None : <EOL> self . td_matrix = np . array ( [ ] ) <EOL> else : <EOL> td_matrix = sparse . coo_matrix ( td_matrix ) <EOL> finite_mask = np . isfinite ( td_matrix . data ) <EOL> coo_in = ( td_matrix . data [ finite_mask ] , <EOL> ( td_matrix . row [ finite_mask ] , <EOL> td_matrix . col [ finite_mask ] ) ) <EOL> td_matrix = sparse . coo_matrix ( coo_in , shape = td_matrix . shape , <EOL> dtype = np . float64 ) <EOL> self . td_matrix = td_matrix . tocsr ( ) <EOL> def train ( self , k_factors = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> from scipy . sparse import linalg as linalgs <EOL> u , s , v = np . array ( [ ] ) , np . array ( [ ] ) , np . array ( [ ] ) <EOL> if self . td_matrix . size > <NUM_LIT:0> : <EOL> s = min ( self . td_matrix . shape ) <EOL> if s < k_factors : <EOL> k_factors = s - <NUM_LIT:1> <EOL> u , s , v = linalgs . svds ( self . td_matrix , k = k_factors ) <EOL> indices = s . argsort ( ) [ : : - <NUM_LIT:1> ] <EOL> self . word_matrix = u [ : , indices ] <EOL> self . eigenvalues = s [ indices ] <EOL> self . doc_matrix = v [ indices , : ] <EOL> def save ( self , f ) : <EOL> """<STR_LIT>""" <EOL> arrays_out = dict ( ) <EOL> arrays_out [ '<STR_LIT>' ] = self . word_matrix <EOL> arrays_out [ '<STR_LIT>' ] = self . eigenvalues <EOL> arrays_out [ '<STR_LIT>' ] = self . doc_matrix <EOL> arrays_out [ '<STR_LIT>' ] = self . context_type <EOL> np . savez ( f , ** arrays_out ) <EOL> @ staticmethod <EOL> def load ( f ) : <EOL> """<STR_LIT>""" <EOL> arrays_in = np . load ( f ) <EOL> m = Lsa ( context_type = arrays_in [ '<STR_LIT>' ] ) <EOL> m . word_matrix = arrays_in [ '<STR_LIT>' ] <EOL> m . eigenvalues = arrays_in [ '<STR_LIT>' ] <EOL> m . doc_matrix = arrays_in [ '<STR_LIT>' ] <EOL> return m <EOL> @ staticmethod <EOL> def from_tf ( tf_model ) : <EOL> """<STR_LIT>""" <EOL> model = Lsa ( td_matrix = tf_model . matrix ) <EOL> model . corpus = tf_model . corpus <EOL> model . context_type = tf_model . context_type <EOL> return model <EOL> @ staticmethod <EOL> def from_tfidf ( tfidf_model ) : <EOL> """<STR_LIT>""" <EOL> model = Lsa ( td_matrix = tfidf_model . matrix ) <EOL> model . corpus = tfidf_model . corpus <EOL> model . context_type = tfidf_model . context_type <EOL> return model </s>
