<s> """<STR_LIT>""" <EOL> from math import log , sqrt <EOL> import numpy as np <EOL> from scipy import linalg <EOL> from scipy . special import gammaln <EOL> from . . externals import six <EOL> from . base import _BasePCA <EOL> from . . base import BaseEstimator , TransformerMixin <EOL> from . . utils import deprecated <EOL> from . . utils import check_random_state , as_float_array <EOL> from . . utils import check_array <EOL> from . . utils . extmath import fast_dot , fast_logdet , randomized_svd , svd_flip <EOL> from . . utils . validation import check_is_fitted <EOL> from . . utils . arpack import svds <EOL> def _assess_dimension_ ( spectrum , rank , n_samples , n_features ) : <EOL> """<STR_LIT>""" <EOL> if rank > len ( spectrum ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> pu = - rank * log ( <NUM_LIT> ) <EOL> for i in range ( rank ) : <EOL> pu += ( gammaln ( ( n_features - i ) / <NUM_LIT> ) - <EOL> log ( np . pi ) * ( n_features - i ) / <NUM_LIT> ) <EOL> pl = np . sum ( np . log ( spectrum [ : rank ] ) ) <EOL> pl = - pl * n_samples / <NUM_LIT> <EOL> if rank == n_features : <EOL> pv = <NUM_LIT:0> <EOL> v = <NUM_LIT:1> <EOL> else : <EOL> v = np . sum ( spectrum [ rank : ] ) / ( n_features - rank ) <EOL> pv = - np . log ( v ) * n_samples * ( n_features - rank ) / <NUM_LIT> <EOL> m = n_features * rank - rank * ( rank + <NUM_LIT:1.> ) / <NUM_LIT> <EOL> pp = log ( <NUM_LIT> * np . pi ) * ( m + rank + <NUM_LIT:1.> ) / <NUM_LIT> <EOL> pa = <NUM_LIT:0.> <EOL> spectrum_ = spectrum . copy ( ) <EOL> spectrum_ [ rank : n_features ] = v <EOL> for i in range ( rank ) : <EOL> for j in range ( i + <NUM_LIT:1> , len ( spectrum ) ) : <EOL> pa += log ( ( spectrum [ i ] - spectrum [ j ] ) * <EOL> ( <NUM_LIT:1.> / spectrum_ [ j ] - <NUM_LIT:1.> / spectrum_ [ i ] ) ) + log ( n_samples ) <EOL> ll = pu + pl + pv + pp - pa / <NUM_LIT> - rank * log ( n_samples ) / <NUM_LIT> <EOL> return ll <EOL> def _infer_dimension_ ( spectrum , n_samples , n_features ) : <EOL> """<STR_LIT>""" <EOL> n_spectrum = len ( spectrum ) <EOL> ll = np . empty ( n_spectrum ) <EOL> for rank in range ( n_spectrum ) : <EOL> ll [ rank ] = _assess_dimension_ ( spectrum , rank , n_samples , n_features ) <EOL> return ll . argmax ( ) <EOL> class PCA ( _BasePCA ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_components = None , copy = True , whiten = False , <EOL> svd_solver = '<STR_LIT>' , tol = <NUM_LIT:0.0> , iterated_power = <NUM_LIT:4> , <EOL> random_state = None ) : <EOL> self . n_components = n_components <EOL> self . copy = copy <EOL> self . whiten = whiten <EOL> self . svd_solver = svd_solver <EOL> self . tol = tol <EOL> self . iterated_power = iterated_power <EOL> self . random_state = random_state <EOL> def fit ( self , X , y = None ) : <EOL> """<STR_LIT>""" <EOL> self . _fit ( X ) <EOL> return self <EOL> def fit_transform ( self , X , y = None ) : <EOL> """<STR_LIT>""" <EOL> U , S , V = self . _fit ( X ) <EOL> U = U [ : , : self . n_components_ ] <EOL> if self . whiten : <EOL> U *= sqrt ( X . shape [ <NUM_LIT:0> ] ) <EOL> else : <EOL> U *= S [ : self . n_components_ ] <EOL> return U <EOL> def _fit ( self , X ) : <EOL> """<STR_LIT>""" <EOL> X = check_array ( X , dtype = [ np . float64 ] , ensure_2d = True , <EOL> copy = self . copy ) <EOL> if self . n_components is None : <EOL> n_components = X . shape [ <NUM_LIT:1> ] <EOL> else : <EOL> n_components = self . n_components <EOL> svd_solver = self . svd_solver <EOL> if svd_solver == '<STR_LIT>' : <EOL> if max ( X . shape ) <= <NUM_LIT> : <EOL> svd_solver = '<STR_LIT>' <EOL> elif n_components >= <NUM_LIT:1> and n_components < <NUM_LIT> * min ( X . shape ) : <EOL> svd_solver = '<STR_LIT>' <EOL> else : <EOL> svd_solver = '<STR_LIT>' <EOL> if svd_solver == '<STR_LIT>' : <EOL> return self . _fit_full ( X , n_components ) <EOL> elif svd_solver in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return self . _fit_truncated ( X , n_components , svd_solver ) <EOL> def _fit_full ( self , X , n_components ) : <EOL> """<STR_LIT>""" <EOL> n_samples , n_features = X . shape <EOL> if n_components == '<STR_LIT>' : <EOL> if n_samples < n_features : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif not <NUM_LIT:0> <= n_components <= n_features : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( n_components , n_features ) ) <EOL> self . mean_ = np . mean ( X , axis = <NUM_LIT:0> ) <EOL> X -= self . mean_ <EOL> U , S , V = linalg . svd ( X , full_matrices = False ) <EOL> U , V = svd_flip ( U , V ) <EOL> components_ = V <EOL> explained_variance_ = ( S ** <NUM_LIT:2> ) / n_samples <EOL> total_var = explained_variance_ . sum ( ) <EOL> explained_variance_ratio_ = explained_variance_ / total_var <EOL> if n_components == '<STR_LIT>' : <EOL> n_components = _infer_dimension_ ( explained_variance_ , n_samples , n_features ) <EOL> elif <NUM_LIT:0> < n_components < <NUM_LIT:1.0> : <EOL> ratio_cumsum = explained_variance_ratio_ . cumsum ( ) <EOL> n_components = np . searchsorted ( ratio_cumsum , n_components ) + <NUM_LIT:1> <EOL> if n_components < min ( n_features , n_samples ) : <EOL> self . noise_variance_ = explained_variance_ [ n_components : ] . mean ( ) <EOL> else : <EOL> self . noise_variance_ = <NUM_LIT:0.> <EOL> self . n_samples_ , self . n_features_ = n_samples , n_features <EOL> self . components_ = components_ [ : n_components ] <EOL> self . n_components_ = n_components <EOL> self . explained_variance_ = explained_variance_ [ : n_components ] <EOL> self . explained_variance_ratio_ = explained_variance_ratio_ [ : n_components ] <EOL> return U , S , V <EOL> def _fit_truncated ( self , X , n_components , svd_solver ) : <EOL> """<STR_LIT>""" <EOL> n_samples , n_features = X . shape <EOL> if isinstance ( n_components , six . string_types ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( n_components , svd_solver ) ) <EOL> elif not <NUM_LIT:1> <= n_components <= n_features : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( n_components , n_features , svd_solver ) ) <EOL> elif svd_solver == '<STR_LIT>' and n_components == n_features : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( n_components , n_features , svd_solver ) ) <EOL> random_state = check_random_state ( self . random_state ) <EOL> self . mean_ = np . mean ( X , axis = <NUM_LIT:0> ) <EOL> X -= self . mean_ <EOL> if svd_solver == '<STR_LIT>' : <EOL> v0 = random_state . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , size = min ( X . shape ) ) <EOL> U , S , V = svds ( X , k = n_components , tol = self . tol , v0 = v0 ) <EOL> S = S [ : : - <NUM_LIT:1> ] <EOL> U , V = svd_flip ( U [ : , : : - <NUM_LIT:1> ] , V [ : : - <NUM_LIT:1> ] ) <EOL> elif svd_solver == '<STR_LIT>' : <EOL> U , S , V = randomized_svd ( X , n_components = n_components , <EOL> n_iter = self . iterated_power , <EOL> flip_sign = True , <EOL> random_state = random_state ) <EOL> self . n_samples_ , self . n_features_ = n_samples , n_features <EOL> self . components_ = V <EOL> self . n_components_ = n_components <EOL> self . explained_variance_ = ( S ** <NUM_LIT:2> ) / n_samples <EOL> total_var = np . var ( X , axis = <NUM_LIT:0> ) <EOL> self . explained_variance_ratio_ = self . explained_variance_ / total_var . sum ( ) <EOL> if self . n_components_ < n_features : <EOL> self . noise_variance_ = ( total_var . sum ( ) - <EOL> self . explained_variance_ . sum ( ) ) <EOL> else : <EOL> self . noise_variance_ = <NUM_LIT:0.> <EOL> return U , S , V <EOL> def score_samples ( self , X ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , '<STR_LIT>' ) <EOL> X = check_array ( X ) <EOL> Xr = X - self . mean_ <EOL> n_features = X . shape [ <NUM_LIT:1> ] <EOL> log_like = np . zeros ( X . shape [ <NUM_LIT:0> ] ) <EOL> precision = self . get_precision ( ) <EOL> log_like = - <NUM_LIT> * ( Xr * ( np . dot ( Xr , precision ) ) ) . sum ( axis = <NUM_LIT:1> ) <EOL> log_like -= <NUM_LIT> * ( n_features * log ( <NUM_LIT> * np . pi ) - <EOL> fast_logdet ( precision ) ) <EOL> return log_like <EOL> def score ( self , X , y = None ) : <EOL> """<STR_LIT>""" <EOL> return np . mean ( self . score_samples ( X ) ) <EOL> @ deprecated ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class RandomizedPCA ( BaseEstimator , TransformerMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n_components = None , copy = True , iterated_power = <NUM_LIT:2> , <EOL> whiten = False , random_state = None ) : <EOL> self . n_components = n_components <EOL> self . copy = copy <EOL> self . iterated_power = iterated_power <EOL> self . whiten = whiten <EOL> self . random_state = random_state <EOL> def fit ( self , X , y = None ) : <EOL> """<STR_LIT>""" <EOL> self . _fit ( check_array ( X ) ) <EOL> return self <EOL> def _fit ( self , X ) : <EOL> """<STR_LIT>""" <EOL> random_state = check_random_state ( self . random_state ) <EOL> X = np . atleast_2d ( as_float_array ( X , copy = self . copy ) ) <EOL> n_samples = X . shape [ <NUM_LIT:0> ] <EOL> self . mean_ = np . mean ( X , axis = <NUM_LIT:0> ) <EOL> X -= self . mean_ <EOL> if self . n_components is None : <EOL> n_components = X . shape [ <NUM_LIT:1> ] <EOL> else : <EOL> n_components = self . n_components <EOL> U , S , V = randomized_svd ( X , n_components , <EOL> n_iter = self . iterated_power , <EOL> random_state = random_state ) <EOL> self . explained_variance_ = exp_var = ( S ** <NUM_LIT:2> ) / n_samples <EOL> full_var = np . var ( X , axis = <NUM_LIT:0> ) . sum ( ) <EOL> self . explained_variance_ratio_ = exp_var / full_var <EOL> if self . whiten : <EOL> self . components_ = V / S [ : , np . newaxis ] * sqrt ( n_samples ) <EOL> else : <EOL> self . components_ = V <EOL> return X <EOL> def transform ( self , X , y = None ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , '<STR_LIT>' ) <EOL> X = check_array ( X ) <EOL> if self . mean_ is not None : <EOL> X = X - self . mean_ <EOL> X = fast_dot ( X , self . components_ . T ) <EOL> return X <EOL> def fit_transform ( self , X , y = None ) : <EOL> """<STR_LIT>""" <EOL> X = check_array ( X ) <EOL> X = self . _fit ( X ) <EOL> return fast_dot ( X , self . components_ . T ) <EOL> def inverse_transform ( self , X , y = None ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , '<STR_LIT>' ) <EOL> X_original = fast_dot ( X , self . components_ ) <EOL> if self . mean_ is not None : <EOL> X_original = X_original + self . mean_ <EOL> return X_original </s>
