【发布时间】: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