【问题标题】:How to rank features correctly from PCA's eigenvector如何从 PCA 特征向量中正确地对特征进行排序
【发布时间】:2019-07-25 03:33:49
【问题描述】:

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

我设置了一个实验,在该实验中我构建了一个数据集,其中依次包含 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。意思是说您必须单独调整它们,而不是将它们组合在一起。但是我对此不是 100% 确定的。
  • 感谢您的评论,数据集是由 sklearn 的 make_classification 方法 scikit-learn.org/stable/modules/generated/… 创建的。抱歉,我无法在此处正确粘贴数据框。但是您可以通过将random_state 设置为 0 来重现它。

标签: python machine-learning scikit-learn pca


【解决方案1】:

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

【讨论】:

  • 感谢 Axois,虽然答案不是我想要的,但您提供了另一种方法来解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多