<s> import numpy as np <EOL> CLNAME_OTHER = '<STR_LIT>' <EOL> class Balancer ( object ) : <EOL> '''<STR_LIT>''' <EOL> def get_class_count ( self , other_clname = CLNAME_OTHER ) : <EOL> """<STR_LIT>""" <EOL> self . has_other_cl = other_clname is not None and other_clname != '<STR_LIT>' <EOL> if self . l . ndim == <NUM_LIT:2> : <EOL> class_count = np . sum ( self . l , axis = <NUM_LIT:0> ) <EOL> other_count = int ( len ( self . l ) - np . sum ( class_count ) ) <EOL> d = { } <EOL> for i , x in enumerate ( class_count ) : <EOL> d [ i ] = int ( x ) <EOL> if self . has_other_cl : <EOL> d [ other_clname ] = other_count <EOL> return d <EOL> def get_idxs_to_balance_class_count ( self , class_counts ) : <EOL> """<STR_LIT>""" <EOL> mx = np . max ( class_counts ) <EOL> num_examples , num_classes = self . l . shape <EOL> idxs = np . arange ( num_examples ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> for i , c in enumerate ( class_counts ) : <EOL> delta_ = mx - c <EOL> if delta_ > <NUM_LIT:0> : <EOL> if i < num_classes : <EOL> rows = np . where ( self . l [ : , i ] == <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> else : <EOL> rows = np . where ( np . sum ( self . l , axis = - <NUM_LIT:1> ) <= <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> rows_to_sample = rows [ np . random . randint ( <NUM_LIT:0> , high = rows . size , <EOL> size = ( delta_ , <NUM_LIT:1> ) ) <EOL> ] <EOL> idxs = np . vstack ( ( idxs , rows_to_sample ) ) <EOL> return idxs . ravel ( ) <EOL> def __init__ ( self , labls ) : <EOL> '''<STR_LIT>''' <EOL> self . l = labls <EOL> self . has_other_cl = True </s>
