如何根据 PCA 的特征向量对特征进行正确排序

How to rank features correctly from PCA's eigenvector

我的目标是根据对 theris 主成分的贡献对监督式机器学习数据集的特征进行排名,感谢 this answer

我设置了一个实验,其中我构建了一个数据集,其中依次包含 3 个信息特征、3 个冗余特征和 3 个噪声特征。然后找到每个主轴上最大组件的索引。

然而,我使用这种方法得到了一个更差的排名。不知道我犯了什么错误。非常感谢您的帮助。这是我的代码:

from sklearn.datasets import make_classification
from sklearn.decomposition import PCA
import pandas as pd
import numpy as np

# Make a dataset which contains 3 Infomative, redundant, noise features respectively
X, _ = make_classification(n_samples=20, n_features=9, n_informative=3,
                           n_redundant=3, random_state=0, shuffle=False)

cols = ['I_'+str(i) for i in range(3)]
cols += ['R_'+str(i) for i in range(3)]
cols += ['N_'+str(i) for i in range(3)]
dfX = pd.DataFrame(X, columns=cols)


# Rank each feature by each priciple axis maximum component
model = PCA().fit(dfX)
_ = model.transform(dfX)

n_pcs= model.components_.shape[0]
most_important = [np.abs(model.components_[i]).argmax() for i in range(n_pcs)]
most_important_names = [dfX.columns[most_important[i]] for i in range(n_pcs)]

rank = {'PC{}'.format(i): most_important_names[i] for i in range(n_pcs)}

排名输出:

{'PC0': 'R_1',
  'PC1': 'I_1',
  'PC2': 'N_1',
  'PC3': 'N_0',
  'PC4': 'N_2',
  'PC5': 'I_2',
  'PC6': 'R_1',
  'PC7': 'R_0',
  'PC8': 'R_2'}

我期待看到信息功能 I_x 排名前 3。

PCA排名标准是每一列的方差,如果你想有一个排名,你可以做的是输出每一列的VarianceThreshold。你可以这样做

from sklearn.feature_selection import VarianceThreshold

selector = VarianceThreshold()
selector.fit_transform(dfX)
print(selector.variances_)

# outputs [1.57412087 1.08363799 1.11752334 0.58501874 2.2983772  0.2857617
# 1.09782539 0.98715471 0.93262548]

您可以清楚地看到前 3 列(I0、I1、I2)的方差最大,因此最适合使用 PCA