【问题标题】:how to find number components dynamically such given in following Matlab code of PCA?如何动态查找数字分量,如以下 PCA 的 Matlab 代码中给出的?
【发布时间】:2019-03-26 13:18:49
【问题描述】:

在下面的链接中,固定数量的主成分分析参数是预定义的,但应该动态定义为 Matlab 代码。怎么可能?

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

如Matlab代码中给出的如何动态求主成分个数:

[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

这里的 train 变量是一个二维矩阵。

【问题讨论】:

  • 您是在询问 Python 或 Matlab 中的内容吗?你的问题不是很清楚。
  • 是的,应该在python中
  • 哪个应该在 Python 中?你的问题是“我如何在 Python 中使用 PCA?”?我无法完全弄清楚您要做什么。
  • 你的问题到底是什么?
  • 这里的组件数量取决于我在 python 代码中想要的解释变量

标签: python matlab pca


【解决方案1】:

我得到的 MatLab 和 python 的解释变量略有不同,因此解决如下:

[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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多