<s> from PIL import Image <EOL> from numpy import * <EOL> def pca ( X ) : <EOL> """<STR_LIT>""" <EOL> num_data , dim = X . shape <EOL> mean_X = X . mean ( axis = <NUM_LIT:0> ) <EOL> X = X - mean_X <EOL> if dim > num_data : <EOL> M = dot ( X , X . T ) <EOL> e , EV = linalg . eigh ( M ) <EOL> tmp = dot ( X . T , EV ) . T <EOL> V = tmp [ : : - <NUM_LIT:1> ] <EOL> S = sqrt ( e ) [ : : - <NUM_LIT:1> ] <EOL> for i in range ( V . shape [ <NUM_LIT:1> ] ) : <EOL> V [ : , i ] /= S <EOL> else : <EOL> U , S , V = linalg . svd ( X ) <EOL> V = V [ : num_data ] <EOL> return V , S , mean_X <EOL> def center ( X ) : <EOL> """<STR_LIT>""" <EOL> n , m = X . shape <EOL> if n != m : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> colsum = X . sum ( axis = <NUM_LIT:0> ) / n <EOL> rowsum = X . sum ( axis = <NUM_LIT:1> ) / n <EOL> totalsum = X . sum ( ) / ( n ** <NUM_LIT:2> ) <EOL> Y = array ( [ [ X [ i , j ] - rowsum [ i ] - colsum [ j ] + totalsum for i in range ( n ) ] for j in range ( n ) ] ) <EOL> return Y </s>
