#NEWLINE# def fit( self , X , Y ) : #NEWLINE# #INDENT# 'Fit model to data.\n\n        Parameters\n        ----------\n        X : array-like, shape = [n_samples, n_features]\n            Training vectors, where n_samples in the number of samples and\n            n_features is the number of predictors.\n\n        Y : array-like of response, shape = [n_samples, n_targets]\n            Target vectors, where n_samples in the number of samples and\n            n_targets is the number of response variables.\n        ' #NEWLINE# check_consistent_length ( X , Y ) #NEWLINE# X = check_array ( X , dtype = np . float64 , copy = self . copy ) #NEWLINE# Y = check_array ( Y , dtype = np . float64 , copy = self . copy , ensure_2d = False ) #NEWLINE# if ( Y . ndim == 1 ) : #NEWLINE# #INDENT# Y = Y . reshape ( ( - 1 ) , 1 ) #NEWLINE# #UNINDENT# n = X . shape [ 0 ] #NEWLINE# p = X . shape [ 1 ] #NEWLINE# q = Y . shape [ 1 ] #NEWLINE# if ( ( self . n_components < 1 ) or ( self . n_components > p ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Invalid number of components: %d' % self . n_components ) ) #NEWLINE# #UNINDENT# if ( self . algorithm not in ( 'svd' , 'nipals' ) ) : #NEWLINE# #INDENT# raise ValueError ( ( "Got algorithm %s when only 'svd' and 'nipals' are known" % self . algorithm ) ) #NEWLINE# #UNINDENT# if ( ( self . algorithm == 'svd' ) and ( self . mode == 'B' ) ) : #NEWLINE# #INDENT# raise ValueError ( 'Incompatible configuration: mode B is not implemented with svd algorithm' ) #NEWLINE# #UNINDENT# if ( self . deflation_mode not in [ 'canonical' , 'regression' ] ) : #NEWLINE# #INDENT# raise ValueError ( 'The deflation mode is unknown' ) #NEWLINE# #UNINDENT# ( X , Y , self . x_mean_ , self . y_mean_ , self . x_std_ , self . y_std_ ) = _center_scale_xy ( X , Y , self . scale ) #NEWLINE# Xk = X #NEWLINE# Yk = Y #NEWLINE# self . x_scores_ = np . zeros ( ( n , self . n_components ) ) #NEWLINE# self . y_scores_ = np . zeros ( ( n , self . n_components ) ) #NEWLINE# self . x_weights_ = np . zeros ( ( p , self . n_components ) ) #NEWLINE# self . y_weights_ = np . zeros ( ( q , self . n_components ) ) #NEWLINE# self . x_loadings_ = np . zeros ( ( p , self . n_components ) ) #NEWLINE# self . y_loadings_ = np . zeros ( ( q , self . n_components ) ) #NEWLINE# self . n_iter_ = [ ] #NEWLINE# for k in range ( self . n_components ) : #NEWLINE# #INDENT# if np . all ( ( np . dot ( Yk . T , Yk ) < np . finfo ( np . double ) . eps ) ) : #NEWLINE# #INDENT# warnings . warn ( ( 'Y residual constant at iteration %s' % k ) ) #NEWLINE# break #NEWLINE# #UNINDENT# if ( self . algorithm == 'nipals' ) : #NEWLINE# #INDENT# ( x_weights , y_weights , n_iter_ ) = _nipals_twoblocks_inner_loop ( X = Xk , Y = Yk , mode = self . mode , max_iter = self . max_iter , tol = self . tol , norm_y_weights = self . norm_y_weights ) #NEWLINE# self . n_iter_ . append ( n_iter_ ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . algorithm == 'svd' ) : #NEWLINE# #INDENT# ( x_weights , y_weights ) = _svd_cross_product ( X = Xk , Y = Yk ) #NEWLINE# #UNINDENT# #UNINDENT# ( x_weights , y_weights ) = svd_flip ( x_weights , y_weights . T ) #NEWLINE# y_weights = y_weights . T #NEWLINE# x_scores = np . dot ( Xk , x_weights ) #NEWLINE# if self . norm_y_weights : #NEWLINE# #INDENT# y_ss = 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# y_ss = np . dot ( y_weights . T , y_weights ) #NEWLINE# #UNINDENT# y_scores = ( np . dot ( Yk , y_weights ) / y_ss ) #NEWLINE# if ( np . dot ( x_scores . T , x_scores ) < np . finfo ( np . double ) . eps ) : #NEWLINE# #INDENT# warnings . warn ( ( 'X scores are null at iteration %s' % k ) ) #NEWLINE# break #NEWLINE# #UNINDENT# x_loadings = ( np . dot ( Xk . T , x_scores ) / np . dot ( x_scores . T , x_scores ) ) #NEWLINE# Xk -= np . dot ( x_scores , x_loadings . T ) #NEWLINE# if ( self . deflation_mode == 'canonical' ) : #NEWLINE# #INDENT# y_loadings = ( np . dot ( Yk . T , y_ss ) / np . dot ( y_scores . T , y_scores ) ) #NEWLINE# Yk -= np . dot ( y_scores , y_loadings . T ) #NEWLINE# #UNINDENT# if ( self . deflation_mode == 'regression' ) : #NEWLINE# #INDENT# y_loadings = ( np . dot ( Yk . T , x_scores ) / np . dot ( x_scores . T , x_scores ) ) #NEWLINE# Yk -= np . dot ( x_scores , y_loadings . T ) #NEWLINE# #UNINDENT# self . x_scores_ [ : , k ] = x_scores . ravel ( ) #NEWLINE# self . y_scores_ [ : , k ] = y_scores . ravel ( ) #NEWLINE# self . x_weights_ [ : , k ] = x_weights . ravel ( ) #NEWLINE# self . y_weights_ [ : , k ] = y_weights . ravel ( ) #NEWLINE# self . x_loadings_ [ : , k ] = x_loadings . ravel ( ) #NEWLINE# self . y_loadings_ [ : , k ] = y_loadings . ravel ( ) #NEWLINE# #UNINDENT# self . x_rotations_ = np . dot ( self . x_weights_ , linalg . pinv2 ( np . dot ( self . x_loadings_ . T , self . x_weights_ ) , ** pinv2_args ) ) #NEWLINE# if ( Y . shape [ 1 ] > 1 ) : #NEWLINE# #INDENT# self . y_rotations_ = np . dot ( self . y_weights_ , linalg . pinv2 ( np . dot ( self . y_loadings_ . T , self . y_weights_ ) , ** pinv2_args ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . y_rotations_ = np . ones ( 1 ) #NEWLINE# #UNINDENT# if ( True or ( self . deflation_mode == 'regression' ) ) : #NEWLINE# #INDENT# self . coef_ = np . dot ( self . x_rotations_ , self . y_loadings_ . T ) #NEWLINE# self . coef_ = ( ( ( 1.0 / self . x_std_ . reshape ( ( p , 1 ) ) ) * self . coef_ ) * self . y_std_ ) #NEWLINE# #UNINDENT# return self