#NEWLINE# def _solve_sparse_cg( X , y , alpha , max_iter = None , tol = 0.001 , verbose = 0 ) : #NEWLINE# #INDENT# ( n_samples , n_features ) = X . shape #NEWLINE# X1 = sp_linalg . aslinearoperator ( X ) #NEWLINE# coefs = np . empty ( ( y . shape [ 1 ] , n_features ) ) #NEWLINE# if ( n_features > n_samples ) : #NEWLINE# #INDENT# def create_mv( curr_alpha ) : #NEWLINE# #INDENT# def _mv( x ) : #NEWLINE# #INDENT# return ( X1 . matvec ( X1 . rmatvec ( x ) ) + ( curr_alpha * x ) ) #NEWLINE# #UNINDENT# return _mv #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# def create_mv( curr_alpha ) : #NEWLINE# #INDENT# def _mv( x ) : #NEWLINE# #INDENT# return ( X1 . rmatvec ( X1 . matvec ( x ) ) + ( curr_alpha * x ) ) #NEWLINE# #UNINDENT# return _mv #NEWLINE# #UNINDENT# #UNINDENT# for i in range ( y . shape [ 1 ] ) : #NEWLINE# #INDENT# y_column = y [ : , i ] #NEWLINE# mv = create_mv ( alpha [ i ] ) #NEWLINE# if ( n_features > n_samples ) : #NEWLINE# #INDENT# C = sp_linalg . LinearOperator ( ( n_samples , n_samples ) , matvec = mv , dtype = X . dtype ) #NEWLINE# ( coef , info ) = sp_linalg . cg ( C , y_column , tol = tol ) #NEWLINE# coefs [ i ] = X1 . rmatvec ( coef ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# y_column = X1 . rmatvec ( y_column ) #NEWLINE# C = sp_linalg . LinearOperator ( ( n_features , n_features ) , matvec = mv , dtype = X . dtype ) #NEWLINE# ( coefs [ i ] , info ) = sp_linalg . cg ( C , y_column , maxiter = max_iter , tol = tol ) #NEWLINE# #UNINDENT# if ( info < 0 ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Failed with error code %d' % info ) ) #NEWLINE# #UNINDENT# if ( ( max_iter is None ) and ( info > 0 ) and verbose ) : #NEWLINE# #INDENT# warnings . warn ( ( 'sparse_cg did not converge after %d iterations.' % info ) ) #NEWLINE# #UNINDENT# #UNINDENT# return coefs