<s> import numpy as np <EOL> import pandas as pd <EOL> from scipy . linalg import svd <EOL> from skbio . _base import OrdinationResults <EOL> from . _utils import svd_rank <EOL> from skbio . util . _decorator import experimental <EOL> @ experimental ( as_of = "<STR_LIT>" ) <EOL> def ca ( X , scaling = <NUM_LIT:1> ) : <EOL> r"""<STR_LIT>""" <EOL> if scaling not in { <NUM_LIT:1> , <NUM_LIT:2> } : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" . format ( scaling ) ) <EOL> short_method_name = '<STR_LIT>' <EOL> long_method_name = '<STR_LIT>' <EOL> row_ids = X . index <EOL> column_ids = X . columns <EOL> X = np . asarray ( X . values , dtype = np . float64 ) <EOL> r , c = X . shape <EOL> if X . min ( ) < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> grand_total = X . sum ( ) <EOL> Q = X / grand_total <EOL> column_marginals = Q . sum ( axis = <NUM_LIT:0> ) <EOL> row_marginals = Q . sum ( axis = <NUM_LIT:1> ) <EOL> expected = np . outer ( row_marginals , column_marginals ) <EOL> Q_bar = ( Q - expected ) / np . sqrt ( expected ) <EOL> U_hat , W , Ut = svd ( Q_bar , full_matrices = False ) <EOL> rank = svd_rank ( Q_bar . shape , W ) <EOL> assert rank <= min ( r , c ) - <NUM_LIT:1> <EOL> U_hat = U_hat [ : , : rank ] <EOL> W = W [ : rank ] <EOL> U = Ut [ : rank ] . T <EOL> V = column_marginals [ : , None ] ** - <NUM_LIT:0.5> * U <EOL> V_hat = row_marginals [ : , None ] ** - <NUM_LIT:0.5> * U_hat <EOL> F = V_hat * W <EOL> F_hat = V * W <EOL> eigvals = W ** <NUM_LIT:2> <EOL> features_scores = [ V , F_hat ] [ scaling - <NUM_LIT:1> ] <EOL> sample_scores = [ F , V_hat ] [ scaling - <NUM_LIT:1> ] <EOL> sample_columns = [ '<STR_LIT>' % ( short_method_name , i + <NUM_LIT:1> ) <EOL> for i in range ( sample_scores . shape [ <NUM_LIT:1> ] ) ] <EOL> feature_columns = [ '<STR_LIT>' % ( short_method_name , i + <NUM_LIT:1> ) <EOL> for i in range ( features_scores . shape [ <NUM_LIT:1> ] ) ] <EOL> eigvals = pd . Series ( eigvals , [ '<STR_LIT>' % ( short_method_name , i + <NUM_LIT:1> ) for i in <EOL> range ( eigvals . shape [ <NUM_LIT:0> ] ) ] ) <EOL> samples = pd . DataFrame ( sample_scores , row_ids , sample_columns ) <EOL> features = pd . DataFrame ( features_scores , column_ids , feature_columns ) <EOL> return OrdinationResults ( short_method_name , long_method_name , eigvals , <EOL> samples = samples , features = features ) </s>
