如何从主成分分析(PCA)的特征向量中正确排列特征

我的目标是通过对监督机器学习数据集的特征贡献,借助这个回答来对特征进行排名。

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

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

from sklearn.datasets import make_classificationfrom sklearn.decomposition import PCAimport pandas as pdimport numpy as np# Make a dataset which contains 3 Infomative, redundant, noise features respectivelyX, _ = 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 componentmodel = 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 VarianceThresholdselector = 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]

可以清楚地看到,前三列(I0, I1, I2)的方差最大,因此最适合用于PCA分析。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注