在进行 PCA 分析时,我们如何知道选择了哪些主成分?

When doing a PCA analysis, how can we know which principal components were selected?

我阅读了下面 link 的文章。

https://towardsdatascience.com/principal-component-analysis-pca-with-scikit-learn-1e84a0c731b0

作者很好地描述了PCA分解的过程。我觉得除了一件事我什么都懂。我们如何知道选择了哪些主成分,从而为最终提高我们的 ML 算法的性能提供保护?比如作者是这样开头的。

from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
df = pd.DataFrame(data=cancer.data, columns=cancer.feature_names)
df.head()

好的,我知道有什么特点了。伟大的。然后所有的乐趣都发生了,我们最终得到了这个。

df_new = pd.DataFrame(X_pca_95, columns=['PC1','PC2','PC3','PC4','PC5','PC6','PC7','PC8','PC9','PC10'])
df_new['label'] = cancer.target
df_new

在我们开始的 30 个特征中,我们如何知道最后 10 列由什么组成?似乎必须有某种最后一步才能将 df_new 映射到 df?

几乎可以肯定,这 10 个结果列都是由全部 30 个原始特征组成的。 PCA 对象有一个属性 components_,显示根据原始特征定义主成分的系数。

要理解,你需要对PCA多了解一点。实际上,PCA returns 构成整个向量 space 的所有主成分,即特征协方差矩阵的特征值和特征向量。因此,您可以 select 基于其相应特征值大小的特征向量。因此,您需要选择最大的特征值及其对应的特征向量。

现在,如果您查看 scikit learn 中 PCA 方法的文档,您会发现一些有用的属性,如下所示:

components_ ndarray of shape (n_components, n_features): Principal axes in feature space, representing the directions of maximum variance in the data. The components are sorted by explained_variance_.

explained_variance_ratio_ ndarray of shape (n_components,) Percentage of variance explained by each of the selected components. If n_components is not set then all components are stored and the sum of the ratios is equal to 1.0.

explained_variance_ratio_ 是一个非常有用的 属性,您可以使用它来 select 主成分,基于覆盖方差百分比的所需阈值。例如,取这个数组中的值为[0.4, 0.3, 0.2, 0.1]。如果我们采用前三个分量,则覆盖方差是原始数据整个方差的 90%