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

标签 python machine-learning scikit-learn pca

我的目标是通过对主成分的贡献对监督机器学习数据集的特征进行排名,感谢 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 排名前三。

最佳答案

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 的最佳候选。

关于python - 如何根据 PCA 的特征向量对特征进行正确排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193903/

相关文章:

python - 使用 Django Admin 上传文件

python - 如何在随机森林中设置自己的概率阈值?

machine-learning - LSTM 后接均值池化

python - wn.synset ('whale.n.01' ) 返回错误的同义词集

python - 从Python访问firebird,提交后无法发送新命令

python - 将逻辑回归和连续回归与 scikit-learn 相结合

function - 人工智能/机器学习项目使用什么语言

python - 我们如何计算 statsmodels OLS 中的截距和斜率?

machine-learning - 如何使用 scikit 交叉验证模块将数据(原始文本)拆分为测试/训练集?

cluster-analysis - 选择和实现聚类方法 : DBSCAN something else?