Optimization of code to calculate n_component of truncated SVD

Viewed 30

I need find optimal n_compnent of trucated svd. But the matrix size is very big of order(10**(6)). I have written the code but it is taking very long time. Is there any way i can optimize the below code.Is there any way i can calculate it using numpy to speed up.

from sklearn.decomposition import TruncatedSVD

def n_component(var_ratio,optimum_var):
    n_component=0
    for i in range(2,x.shape[1]-1,3):
        t_svd=TruncatedSVD(n_components=i)
        X_tsvd=t_svd.fit(x)
        var_ratio=t_svd.explained_variance_ratio_
        total_var=var_ratio.sum()
        if total_var>=optimum_var:
            n_component=i
            break
        
    return n_component
       
n_comp=n_component(var_ratio,0.95)
0 Answers
Related