获取所有组件的pca.explained_variance_ratio_,而不需要两次进行PCA。

huangapple go评论72阅读模式
英文:

Getting pca.explained_variance_ratio_ for all components without doing PCA twice

问题

我理解explained_variance_ratio_可以在PCA中轻松获得,但将受限于来自前n_components的贡献。我想知道是否可以在所有组件上获得explained_variance_ratio_,而不必在获得完整特征值后再次进行PCA,因为所有参数都是在获得完整特征值之后派生的。我打算这样做是因为矩阵非常庞大,我希望减少计算时间。

英文:

I understand that explained_variance_ratio_ can be obtained easily using PCA but will be restricted to the contribution from the first n_components. I was wondering if explained_variance_ratio_ can be obtained for all components without doing PCA twice, after all the parameters are derived after having the full eigen values. This I intend to do as the matrix is huge and I am looking to reduce time in the computation.

答案1

得分: 2

要回答你的问题:

是的,你可以获取所有主成分的explained_variance_ratio_,而不必两次进行PCA。当你执行PCA时,你可以指定要保留的主成分数量。如果你不指定这个数量,PCA将保留所有的主成分。

如果你想在拟合PCA之后减少主成分的数量,你可以这样做,而不必重新拟合PCA。试一下以下方法:

from sklearn.decomposition import PCA
import numpy as np

pca = PCA()
pca.fit(X_train)

现在来打印explained variance ratio:

print(pca.explained_variance_ratio_)

获取前10个主成分的explained variance ratio:

explained_variance_ratio_10 = pca.explained_variance_ratio_[:10]
英文:

To answer your question:

Yes, you can obtain the explained_variance_ratio_ for all components without doing PCA twice. When you perform PCA, you can specify the number of components you want to keep. If you don't specify this number, PCA will keep all the components.

If you want to reduce the number of components after fitting the PCA, you can do so without having to fit the PCA again. Try the folllowing:

from sklearn.decomposition import PCA
import numpy as np

pca = PCA()
pca.fit(X_train)

Now to print explained varience ratio:

print(pca.explained_variance_ratio_)

Get the explained variance ratio for the first 10 components

explained_variance_ratio_10 = pca.explained_variance_ratio_[:10]

huangapple
  • 本文由 发表于 2023年6月26日 06:07:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定