<s> '''<STR_LIT>''' <EOL> import numpy <EOL> try : <EOL> import pylab <EOL> import matplotlib <EOL> except : <EOL> pass <EOL> from numpy . linalg . linalg import LinAlgError <EOL> from operator import setitem <EOL> import itertools <EOL> from functools import reduce <EOL> class PCA ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , X ) : <EOL> self . mu = None <EOL> self . sigma = None <EOL> X = self . center ( X ) <EOL> if X . shape [ <NUM_LIT:0> ] >= X . shape [ <NUM_LIT:1> ] : <EOL> self . eigvals , self . eigvectors = self . _primal_eig ( X ) <EOL> else : <EOL> self . eigvals , self . eigvectors = self . _dual_eig ( X ) <EOL> self . sort = numpy . argsort ( self . eigvals ) [ : : - <NUM_LIT:1> ] <EOL> self . eigvals = self . eigvals [ self . sort ] <EOL> self . eigvectors = self . eigvectors [ : , self . sort ] <EOL> self . fracs = self . eigvals / self . eigvals . sum ( ) <EOL> self . Q = self . eigvals . shape [ <NUM_LIT:0> ] <EOL> def center ( self , X ) : <EOL> """<STR_LIT>""" <EOL> X = X . copy ( ) <EOL> inan = numpy . isnan ( X ) <EOL> if self . mu is None : <EOL> X_ = numpy . ma . masked_array ( X , inan ) <EOL> self . mu = X_ . mean ( <NUM_LIT:0> ) . base <EOL> self . sigma = X_ . std ( <NUM_LIT:0> ) . base <EOL> reduce ( lambda y , x : setitem ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] , x [ <NUM_LIT:2> ] ) , zip ( X . T , inan . T , self . mu ) , None ) <EOL> X = X - self . mu <EOL> X = X / numpy . where ( self . sigma == <NUM_LIT:0> , <NUM_LIT> , self . sigma ) <EOL> return X <EOL> def _primal_eig ( self , X ) : <EOL> return numpy . linalg . eigh ( numpy . einsum ( '<STR_LIT>' , X , X ) ) <EOL> def _dual_eig ( self , X ) : <EOL> dual_eigvals , dual_eigvects = numpy . linalg . eigh ( numpy . einsum ( '<STR_LIT>' , X , X ) ) <EOL> relevant_dimensions = numpy . argsort ( numpy . abs ( dual_eigvals ) ) [ - X . shape [ <NUM_LIT:1> ] : ] <EOL> eigvals = dual_eigvals [ relevant_dimensions ] <EOL> eigvects = dual_eigvects [ : , relevant_dimensions ] <EOL> eigvects = ( <NUM_LIT:1.> / numpy . sqrt ( X . shape [ <NUM_LIT:0> ] * numpy . abs ( eigvals ) ) ) * X . T . dot ( eigvects ) <EOL> eigvects /= numpy . sqrt ( numpy . diag ( eigvects . T . dot ( eigvects ) ) ) <EOL> return eigvals , eigvects <EOL> def project ( self , X , Q = None ) : <EOL> """<STR_LIT>""" <EOL> if Q is None : <EOL> Q = self . Q <EOL> if Q > X . shape [ <NUM_LIT:1> ] : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> X = self . center ( X ) <EOL> return X . dot ( self . eigvectors [ : , : Q ] ) <EOL> def plot_fracs ( self , Q = None , ax = None , fignum = None ) : <EOL> """<STR_LIT>""" <EOL> from . . plotting import Tango <EOL> Tango . reset ( ) <EOL> col = Tango . nextMedium ( ) <EOL> if ax is None : <EOL> fig = pylab . figure ( fignum ) <EOL> ax = fig . add_subplot ( <NUM_LIT> ) <EOL> if Q is None : <EOL> Q = self . Q <EOL> ticks = numpy . arange ( Q ) <EOL> bar = ax . bar ( ticks - <NUM_LIT> , self . fracs [ : Q ] , color = col ) <EOL> ax . set_xticks ( ticks , map ( lambda x : r"<STR_LIT>" . format ( x ) , ticks + <NUM_LIT:1> ) ) <EOL> ax . set_ylabel ( "<STR_LIT>" ) <EOL> ax . set_xlabel ( "<STR_LIT>" ) <EOL> ax . set_ylim ( <NUM_LIT:0> , ax . get_ylim ( ) [ <NUM_LIT:1> ] ) <EOL> ax . set_xlim ( ticks . min ( ) - <NUM_LIT> , ticks . max ( ) + <NUM_LIT> ) <EOL> try : <EOL> pylab . tight_layout ( ) <EOL> except : <EOL> pass <EOL> return bar <EOL> def plot_2d ( self , X , labels = None , s = <NUM_LIT:20> , marker = '<STR_LIT:o>' , <EOL> dimensions = ( <NUM_LIT:0> , <NUM_LIT:1> ) , ax = None , colors = None , <EOL> fignum = None , cmap = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if cmap is None : <EOL> cmap = matplotlib . cm . jet <EOL> if ax is None : <EOL> fig = pylab . figure ( fignum ) <EOL> ax = fig . add_subplot ( <NUM_LIT> ) <EOL> if labels is None : <EOL> labels = numpy . zeros ( X . shape [ <NUM_LIT:0> ] ) <EOL> ulabels = [ ] <EOL> for lab in labels : <EOL> if not lab in ulabels : <EOL> ulabels . append ( lab ) <EOL> nlabels = len ( ulabels ) <EOL> if colors is None : <EOL> colors = iter ( [ cmap ( float ( i ) / nlabels ) for i in range ( nlabels ) ] ) <EOL> else : <EOL> colors = iter ( colors ) <EOL> X_ = self . project ( X , self . Q ) [ : , dimensions ] <EOL> kwargs . update ( dict ( s = s ) ) <EOL> plots = list ( ) <EOL> for i , l in enumerate ( ulabels ) : <EOL> kwargs . update ( dict ( color = colors . next ( ) , marker = marker [ i % len ( marker ) ] ) ) <EOL> plots . append ( ax . scatter ( * X_ [ labels == l , : ] . T , label = str ( l ) , ** kwargs ) ) <EOL> ax . set_xlabel ( r"<STR_LIT>" ) <EOL> ax . set_ylabel ( r"<STR_LIT>" ) <EOL> try : <EOL> pylab . tight_layout ( ) <EOL> except : <EOL> pass <EOL> return plots </s>
