<s> import numpy as np <EOL> import random <EOL> from scipy import ndimage <EOL> def read_image ( filename ) : <EOL> imarr = np . array ( [ ] ) <EOL> try : <EOL> im = Image . open ( os . path . join ( filename ) ) <EOL> im = im . convert ( "<STR_LIT:L>" ) <EOL> imarr = np . array ( im , dtype = np . uint8 ) <EOL> except IOError as ( errno , strerror ) : <EOL> print "<STR_LIT>" . format ( errno , strerror ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> return imarr <EOL> def asRowMatrix ( X ) : <EOL> """<STR_LIT>""" <EOL> if len ( X ) == <NUM_LIT:0> : <EOL> return np . array ( [ ] ) <EOL> total = <NUM_LIT:1> <EOL> for i in range ( <NUM_LIT:0> , np . ndim ( X [ <NUM_LIT:0> ] ) ) : <EOL> total = total * X [ <NUM_LIT:0> ] . shape [ i ] <EOL> mat = np . empty ( [ <NUM_LIT:0> , total ] , dtype = X [ <NUM_LIT:0> ] . dtype ) <EOL> for row in X : <EOL> mat = np . append ( mat , row . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> ) , axis = <NUM_LIT:0> ) <EOL> return np . asmatrix ( mat ) <EOL> def asColumnMatrix ( X ) : <EOL> """<STR_LIT>""" <EOL> if len ( X ) == <NUM_LIT:0> : <EOL> return np . array ( [ ] ) <EOL> total = <NUM_LIT:1> <EOL> for i in range ( <NUM_LIT:0> , np . ndim ( X [ <NUM_LIT:0> ] ) ) : <EOL> total = total * X [ <NUM_LIT:0> ] . shape [ i ] <EOL> mat = np . empty ( [ total , <NUM_LIT:0> ] , dtype = X [ <NUM_LIT:0> ] . dtype ) <EOL> for col in X : <EOL> mat = np . append ( mat , col . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) , axis = <NUM_LIT:1> ) <EOL> return np . asmatrix ( mat ) <EOL> def minmax_normalize ( X , low , high , minX = None , maxX = None , dtype = np . float ) : <EOL> """<STR_LIT>""" <EOL> if minX is None : <EOL> minX = np . min ( X ) <EOL> if maxX is None : <EOL> maxX = np . max ( X ) <EOL> minX = float ( minX ) <EOL> maxX = float ( maxX ) <EOL> X = X - minX <EOL> X = X / ( maxX - minX ) <EOL> X = X * ( high - low ) <EOL> X = X + low <EOL> return np . asarray ( X , dtype = dtype ) <EOL> def zscore ( X ) : <EOL> X = np . asanyarray ( X ) <EOL> mean = X . mean ( ) <EOL> std = X . std ( ) <EOL> X = ( X - mean ) / std <EOL> return X , mean , std <EOL> def shuffle ( X , y ) : <EOL> idx = np . argsort ( [ random . random ( ) for i in xrange ( y . shape [ <NUM_LIT:0> ] ) ] ) <EOL> return X [ : , idx ] , y [ idx ] <EOL> def shuffle_array ( X , y ) : <EOL> """<STR_LIT>""" <EOL> idx = np . argsort ( [ random . random ( ) for i in xrange ( len ( y ) ) ] ) <EOL> X = [ X [ i ] for i in idx ] <EOL> y = [ y [ i ] for i in idx ] <EOL> return ( X , y ) </s>
