<s> import numpy as np <EOL> from itertools import izip <EOL> import operator <EOL> def sort_dic_by_value ( dic , reverse = False ) : <EOL> return sorted ( dic . iteritems ( ) , <EOL> key = operator . itemgetter ( <NUM_LIT:1> ) , <EOL> reverse = reverse ) <EOL> def dict_max ( dic ) : <EOL> """<STR_LIT>""" <EOL> aux = dict ( map ( lambda item : ( item [ <NUM_LIT:1> ] , item [ <NUM_LIT:0> ] ) , dic . items ( ) ) ) <EOL> if aux . keys ( ) == [ ] : <EOL> return <NUM_LIT:0> <EOL> max_value = max ( aux . keys ( ) ) <EOL> return max_value , aux [ max_value ] <EOL> def perp_2d ( a ) : <EOL> """<STR_LIT>""" <EOL> res = <NUM_LIT:1.> / a <EOL> res = res [ : , ] * [ - <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> return res <EOL> def l2norm ( a ) : <EOL> """<STR_LIT>""" <EOL> return np . sqrt ( l2norm_squared ( a ) ) <EOL> def l2norm_squared ( a ) : <EOL> """<STR_LIT>""" <EOL> value = <NUM_LIT:0> <EOL> for i in xrange ( a . shape [ <NUM_LIT:1> ] ) : <EOL> value += np . dot ( a [ : , i ] , a [ : , i ] ) <EOL> return value <EOL> def normalize_array ( a , direction = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> b = a . copy ( ) <EOL> if ( direction == "<STR_LIT>" ) : <EOL> sums = np . sum ( b , <NUM_LIT:0> ) <EOL> return np . nan_to_num ( b / sums ) <EOL> elif ( direction == "<STR_LIT>" ) : <EOL> sums = np . sum ( b , <NUM_LIT:1> ) <EOL> return np . nan_to_num ( ( b . transpose ( ) / sums ) . transpose ( ) ) <EOL> elif ( direction == "<STR_LIT:all>" ) : <EOL> sums = np . sum ( b ) <EOL> return np . nan_to_num ( b / sums ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> return b </s>
