<s> import warnings <EOL> import numpy as np <EOL> from . . externals import six <EOL> from . . utils . fixes import in1d <EOL> from . fixes import bincount <EOL> def compute_class_weight ( class_weight , classes , y ) : <EOL> """<STR_LIT>""" <EOL> from . . preprocessing import LabelEncoder <EOL> if set ( y ) - set ( classes ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if class_weight is None or len ( class_weight ) == <NUM_LIT:0> : <EOL> weight = np . ones ( classes . shape [ <NUM_LIT:0> ] , dtype = np . float64 , order = '<STR_LIT:C>' ) <EOL> elif class_weight in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> le = LabelEncoder ( ) <EOL> y_ind = le . fit_transform ( y ) <EOL> if not all ( np . in1d ( classes , le . classes_ ) ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if class_weight == '<STR_LIT>' : <EOL> recip_freq = <NUM_LIT:1.> / bincount ( y_ind ) <EOL> weight = recip_freq [ le . transform ( classes ) ] / np . mean ( recip_freq ) <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning ) <EOL> else : <EOL> recip_freq = len ( y ) / ( len ( le . classes_ ) * <EOL> bincount ( y_ind ) . astype ( np . float64 ) ) <EOL> weight = recip_freq [ le . transform ( classes ) ] <EOL> else : <EOL> weight = np . ones ( classes . shape [ <NUM_LIT:0> ] , dtype = np . float64 , order = '<STR_LIT:C>' ) <EOL> if not isinstance ( class_weight , dict ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % class_weight ) <EOL> for c in class_weight : <EOL> i = np . searchsorted ( classes , c ) <EOL> if i >= len ( classes ) or classes [ i ] != c : <EOL> raise ValueError ( "<STR_LIT>" % c ) <EOL> else : <EOL> weight [ i ] = class_weight [ c ] <EOL> return weight <EOL> def compute_sample_weight ( class_weight , y , indices = None ) : <EOL> """<STR_LIT>""" <EOL> y = np . atleast_1d ( y ) <EOL> if y . ndim == <NUM_LIT:1> : <EOL> y = np . reshape ( y , ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> n_outputs = y . shape [ <NUM_LIT:1> ] <EOL> if isinstance ( class_weight , six . string_types ) : <EOL> if class_weight not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % class_weight ) <EOL> elif ( indices is not None and <EOL> not isinstance ( class_weight , six . string_types ) ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % class_weight ) <EOL> elif n_outputs > <NUM_LIT:1> : <EOL> if ( not hasattr ( class_weight , "<STR_LIT>" ) or <EOL> isinstance ( class_weight , dict ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if len ( class_weight ) != n_outputs : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> expanded_class_weight = [ ] <EOL> for k in range ( n_outputs ) : <EOL> y_full = y [ : , k ] <EOL> classes_full = np . unique ( y_full ) <EOL> classes_missing = None <EOL> if class_weight in [ '<STR_LIT>' , '<STR_LIT>' ] or n_outputs == <NUM_LIT:1> : <EOL> class_weight_k = class_weight <EOL> else : <EOL> class_weight_k = class_weight [ k ] <EOL> if indices is not None : <EOL> y_subsample = y [ indices , k ] <EOL> classes_subsample = np . unique ( y_subsample ) <EOL> weight_k = np . choose ( np . searchsorted ( classes_subsample , <EOL> classes_full ) , <EOL> compute_class_weight ( class_weight_k , <EOL> classes_subsample , <EOL> y_subsample ) , <EOL> mode = '<STR_LIT>' ) <EOL> classes_missing = set ( classes_full ) - set ( classes_subsample ) <EOL> else : <EOL> weight_k = compute_class_weight ( class_weight_k , <EOL> classes_full , <EOL> y_full ) <EOL> weight_k = weight_k [ np . searchsorted ( classes_full , y_full ) ] <EOL> if classes_missing : <EOL> weight_k [ in1d ( y_full , list ( classes_missing ) ) ] = <NUM_LIT:0.> <EOL> expanded_class_weight . append ( weight_k ) <EOL> expanded_class_weight = np . prod ( expanded_class_weight , <EOL> axis = <NUM_LIT:0> , <EOL> dtype = np . float64 ) <EOL> return expanded_class_weight </s>
