#NEWLINE# def _nipals_twoblocks_inner_loop( X , Y , mode = 'A' , max_iter = 500 , tol = 1e-06 , norm_y_weights = False ) : #NEWLINE# #INDENT# "Inner loop of the iterative NIPALS algorithm.\n\n    Provides an alternative to the svd(X'Y); returns the first left and right\n    singular vectors of X'Y.  See PLS for the meaning of the parameters.  It is\n    similar to the Power method for determining the eigenvectors and\n    eigenvalues of a X'Y.\n    " #NEWLINE# y_score = Y [ : , [ 0 ] ] #NEWLINE# x_weights_old = 0 #NEWLINE# ite = 1 #NEWLINE# X_pinv = Y_pinv = None #NEWLINE# eps = np . finfo ( X . dtype ) . eps #NEWLINE# while True : #NEWLINE# #INDENT# if ( mode == 'B' ) : #NEWLINE# #INDENT# if ( X_pinv is None ) : #NEWLINE# #INDENT# X_pinv = linalg . pinv2 ( X , ** pinv2_args ) #NEWLINE# #UNINDENT# x_weights = np . dot ( X_pinv , y_score ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# x_weights = ( np . dot ( X . T , y_score ) / np . dot ( y_score . T , y_score ) ) #NEWLINE# #UNINDENT# x_weights /= ( np . sqrt ( np . dot ( x_weights . T , x_weights ) ) + eps ) #NEWLINE# x_score = np . dot ( X , x_weights ) #NEWLINE# if ( mode == 'B' ) : #NEWLINE# #INDENT# if ( Y_pinv is None ) : #NEWLINE# #INDENT# Y_pinv = linalg . pinv2 ( Y , ** pinv2_args ) #NEWLINE# #UNINDENT# y_weights = np . dot ( Y_pinv , x_score ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# y_weights = ( np . dot ( Y . T , x_score ) / np . dot ( x_score . T , x_score ) ) #NEWLINE# #UNINDENT# if norm_y_weights : #NEWLINE# #INDENT# y_weights /= ( np . sqrt ( np . dot ( y_weights . T , y_weights ) ) + eps ) #NEWLINE# #UNINDENT# y_score = ( np . dot ( Y , y_weights ) / ( np . dot ( y_weights . T , y_weights ) + eps ) ) #NEWLINE# x_weights_diff = ( x_weights - x_weights_old ) #NEWLINE# if ( ( np . dot ( x_weights_diff . T , x_weights_diff ) < tol ) or ( Y . shape [ 1 ] == 1 ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# if ( ite == max_iter ) : #NEWLINE# #INDENT# warnings . warn ( 'Maximum number of iterations reached' ) #NEWLINE# break #NEWLINE# #UNINDENT# x_weights_old = x_weights #NEWLINE# ite += 1 #NEWLINE# #UNINDENT# return ( x_weights , y_weights , ite )