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



我想对一些数据使用主成分分析,以获得矩阵的顶级主成分,这些成分可以捕获95%的总方差。我一直在寻找一个函数来实现这一点,但我找不到方法
我唯一能找到的是以下内容:

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)

这种方法的问题是我拟合了两次,这是性能瓶颈。有更好的方法吗?

从文档中,您可以看到如果0<n_components<1和svd_solver=="full",选择分量的数量,以便需要解释的方差大于n_components指定的百分比。

要获得至少满足95%方差的分量,请使用PCA(n_components=0.95,svd_solver="完整"(

最新更新