简体   繁体   English

Sklearn.decomposition.PCA:按给定比例获取组件

[英]Sklearn.decomposition.PCA: Get components by given ratio

I want to use PCA on some data to get the top principle components of a matrix that capture 95% of the total variance.我想在一些数据上使用 PCA 来获取矩阵的主要主成分,该成分可捕获总方差的 95%。 I was looking for a function doing that, but I could not find a way.我正在寻找一个 function 这样做,但我找不到办法。
The only I could find out was the following:我唯一能找到的是以下内容:

from sklearn.decomposition import PCA
# W_0 is a matrix 
pca = PCA().fit(W_0)
# get the index of the component which has variance higher than 0.95
index_component = np.min(np.argwhere(np.cumsum(pca.explained_variance_ratio_)>0.95))
# Now fit again with the given component 
pca = PCA(n_components= index_component+1)
pca.fit(W_0)

The problem with this approach is that I am fitting two times which is performance bottleneck.这种方法的问题是我拟合了两次,这是性能瓶颈。 Is there a better way to do that?有没有更好的方法来做到这一点?

From the documentation , you can see If 0 < n_components < 1 and svd_solver == 'full', select the number of components such that the amount of variance that needs to be explained is greater than the percentage specified by n_components.文档中,您可以看到 If 0 < n_components < 1 and svd_solver == 'full', select 组件的数量使得需要解释的方差量大于 n_components 指定的百分比。

To get components that satisfy atleast 95% variance, use PCA(n_components=0.95, svd_solver='full')要获得满足至少 95% 方差的组件,请使用 PCA(n_components=0.95, svd_solver='full')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM