<s> import json <EOL> import random <EOL> from collections import defaultdict <EOL> import numpy <EOL> def normalize ( v ) : <EOL> if isinstance ( v , numpy . bool_ ) : <EOL> return bool ( v ) <EOL> elif isinstance ( v , numpy . float ) : <EOL> return float ( v ) <EOL> elif isinstance ( v , tuple ) : <EOL> return list ( v ) <EOL> else : <EOL> return v <EOL> def key_normalize ( v ) : <EOL> v = normalize ( v ) <EOL> if isinstance ( v , bool ) or isinstance ( v , int ) or isinstance ( v , float ) or isinstance ( v , str ) : <EOL> return v <EOL> elif isinstance ( v , list ) or isinstance ( v , dict ) : <EOL> return json . dumps ( v ) <EOL> else : <EOL> return str ( v ) <EOL> def normalize_json ( doc ) : <EOL> if isinstance ( doc , dict ) : <EOL> return { key_normalize ( k ) : normalize_json ( v ) <EOL> for k , v in doc . items ( ) } <EOL> elif isinstance ( doc , list ) or isinstance ( doc , tuple ) : <EOL> return [ normalize_json ( v ) for v in doc ] <EOL> else : <EOL> return normalize ( doc ) <EOL> def format_params ( doc ) : <EOL> if doc is None : <EOL> return None <EOL> else : <EOL> return "<STR_LIT:U+002CU+0020>" . join ( "<STR_LIT>" . format ( k , json . dumps ( v ) ) <EOL> for k , v in doc . items ( ) ) <EOL> def balance_weights ( labels ) : <EOL> """<STR_LIT>""" <EOL> counts = { } <EOL> for l in labels : <EOL> counts [ l ] = counts . get ( l , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> return { l : ( len ( labels ) / counts [ l ] ) for l in counts } <EOL> def balance_sample_weights ( labels ) : <EOL> """<STR_LIT>""" <EOL> weights = balance_weights ( labels ) <EOL> return [ weights [ label ] for label in labels ] <EOL> def balance_sample ( values_labels ) : <EOL> """<STR_LIT>""" <EOL> groups = defaultdict ( list ) <EOL> for feature_values , label in values_labels : <EOL> groups [ label ] . append ( ( feature_values , label ) ) <EOL> max_label_count = max ( len ( groups [ label ] ) for label in groups ) <EOL> new_feature_values = [ ] <EOL> for label in groups : <EOL> new_feature_values . extend ( groups [ label ] ) <EOL> additional_obs = max_label_count - len ( groups [ label ] ) <EOL> new_feature_values . extend ( random . choice ( groups [ label ] ) <EOL> for i in range ( additional_obs ) ) <EOL> random . shuffle ( new_feature_values ) <EOL> return new_feature_values </s>
