<s> import numpy as np <EOL> import scipy . sparse as sp <EOL> def safe_hstack ( blocks ) : <EOL> if any ( [ sp . issparse ( b ) for b in blocks ] ) : <EOL> return sp . hstack ( blocks ) <EOL> else : <EOL> return np . hstack ( blocks ) <EOL> def safe_vstack ( Xs ) : <EOL> if any ( sp . issparse ( X ) for X in Xs ) : <EOL> return sp . vstack ( Xs ) <EOL> else : <EOL> return np . vstack ( Xs ) <EOL> def normalize_sum ( a , axis = <NUM_LIT:0> , eps = <NUM_LIT> ) : <EOL> if axis >= len ( a . shape ) : <EOL> raise ValueError <EOL> return a / ( eps + np . expand_dims ( np . sum ( a , axis = axis ) , axis ) ) <EOL> def normalize_features ( X ) : <EOL> """<STR_LIT>""" <EOL> sums = np . asarray ( X . sum ( axis = <NUM_LIT:0> ) ) . flatten ( ) <EOL> if sp . issparse ( X ) : <EOL> X . eliminate_zeros ( ) <EOL> x = X . copy ( ) . tocsr ( ) <EOL> x . data /= sums [ x . indices ] <EOL> y = x . tocsc ( ) <EOL> else : <EOL> y = X . copy ( ) <EOL> y /= ( <NUM_LIT:1.> * ( sums == <NUM_LIT:0> ) + sums ) [ np . newaxis , : ] <EOL> return y [ : , sums . nonzero ( ) [ <NUM_LIT:0> ] ] <EOL> class GrowingLILMatrix ( sp . lil_matrix ) : <EOL> def __init__ ( self ) : <EOL> sp . lil_matrix . __init__ ( self , ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . format = '<STR_LIT>' <EOL> self . _shape = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def add_row ( self , row ) : <EOL> """<STR_LIT>""" <EOL> sparse_row = sp . lil_matrix ( row ) <EOL> if self . _shape == ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> self . _shape = sparse_row . _shape <EOL> self . rows = sparse_row . rows <EOL> self . data = sparse_row . data <EOL> else : <EOL> nb_col = max ( len ( row ) , self . shape [ <NUM_LIT:1> ] ) <EOL> self . _shape = ( <NUM_LIT:1> + self . _shape [ <NUM_LIT:0> ] , nb_col ) <EOL> self . rows = np . concatenate ( [ self . rows , sparse_row . rows [ [ <NUM_LIT:0> ] ] ] ) <EOL> self . data = np . concatenate ( [ self . data , sparse_row . data [ [ <NUM_LIT:0> ] ] ] ) </s>
