#NEWLINE# def pca( dataset ) : #NEWLINE# #INDENT# mean = dataset [ : 3000 ] . mean ( axis = 0 ) #NEWLINE# logging . info ( 'computing whitening transform' ) #NEWLINE# x = ( dataset [ : 3000 ] - mean ) #NEWLINE# ( vals , vecs ) = np . linalg . eigh ( ( np . dot ( x . T , x ) / len ( x ) ) ) #NEWLINE# vals = vals [ : : ( - 1 ) ] #NEWLINE# vecs = vecs [ : , : : ( - 1 ) ] #NEWLINE# vals = np . sqrt ( vals [ : K ] ) #NEWLINE# vecs = vecs [ : , : K ] #NEWLINE# def whiten( x ) : #NEWLINE# #INDENT# return np . dot ( x , np . dot ( vecs , np . diag ( ( 1.0 / vals ) ) ) ) #NEWLINE# #UNINDENT# def color( z ) : #NEWLINE# #INDENT# return np . dot ( z , np . dot ( np . diag ( vals ) , vecs . T ) ) #NEWLINE# #UNINDENT# return ( whiten , color )