Search code examples
pythonmatlabpca

how to find number components dynamically such given in following Matlab code of PCA?


In the following link fixed number of principal components analysis parameter is pre-defined but should be dynamically defined as Matlab code. How it is possible?

https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html

how to find the number of principal components dynamically such as given in Matlab code:

[coeff,score,~,~,explained] = pca(train);            
sm = 0;
no_components = 0;
for k = 1:size(explained,1)
    sm = sm+explained(k);
    if sm <= 99.4029
       no_components= no_components+1;
    end
end
no_components

here train variable is a 2D matrix.


Solution

  • there is slight variation with the explained variable with MatLab and python that I got so it is resolved as follow:

    [x,y] = train.shape
    
    pca = PCA(n_components=(x-1))
    varPca = pca.fit(train)   
    
    explainedVariance = pca.explained_variance_ratio_*100
    
    sm = 0
    no_components = 0
    for k in range(0, x-1):
        sm = sm+explainedVariance[k]        
        if sm <= 99.4029:
            no_components= no_components+1    
    
    print(no_components)