PCA 恢复数据框中最重要的特征
PCA recover most important features in a dataframe
我正在尝试研究如何使用 PCA 来确定最重要的特征。我想我已经在下面做到了。
然后我想知道,如何将最重要的特征及其原始列名(来自 pandas 数据框)传递回我在底部创建的新数据框中 - 这样我就可以使用那作为新的 'lightweight' 数据集?
这样,如果我将 n_components 设置为 10;我会将 10 个特征列(带有名称)传递到新数据框中。
有什么想法吗?
from sklearn.decomposition import PCA
# PCA (principal component analysis) aims to reduce the number of dimensions in the dataset, without losing those which are very relevant to the model
# it provides a score, you can drop those with poor scores.
X_pc = PCA(n_components=2).fit_transform(train_features)
pd.DataFrame({'PC1': X_pc[:, 0], 'PC2': X_pc[:, 1], 'Y': train_labels.ravel()}).sample(10)
PCA 通过线性组合初始特征将维度降为 2。转换后,输出是一个具有 [samples, components] 大小的矩阵,因此无法创建数据框,因为您无法投影回 names/features.
重要的特征是对成分影响较大的特征,因此在成分上具有较大的绝对值。
如果您更改代码,您可以获得 PC 上最重要的功能:
from sklearn.decomposition import PCA
import pandas as pd
import numpy as np
np.random.seed(0)
# 10 samples with 5 features
train_features = np.random.rand(10,5)
model = PCA(n_components=2).fit(train_features)
X_pc = model.transform(train_features)
# number of components
n_pcs= model.components_.shape[0]
# get the index of the most important feature on EACH component
# LIST COMPREHENSION HERE
most_important = [np.abs(model.components_[i]).argmax() for i in range(n_pcs)]
initial_feature_names = ['a','b','c','d','e']
# get the names
most_important_names = [initial_feature_names[most_important[i]] for i in range(n_pcs)]
# LIST COMPREHENSION HERE AGAIN
dic = {'PC{}'.format(i+1): most_important_names[i] for i in range(n_pcs)}
# build the dataframe
df = pd.DataFrame(sorted(dic.items()))
这会打印:
0 1
0 PC1 e
1 PC2 d
所以在 PC1 上名为 e
的功能是最重要的,在 PC2 上名为 d
.
我正在尝试研究如何使用 PCA 来确定最重要的特征。我想我已经在下面做到了。
然后我想知道,如何将最重要的特征及其原始列名(来自 pandas 数据框)传递回我在底部创建的新数据框中 - 这样我就可以使用那作为新的 'lightweight' 数据集?
这样,如果我将 n_components 设置为 10;我会将 10 个特征列(带有名称)传递到新数据框中。
有什么想法吗?
from sklearn.decomposition import PCA
# PCA (principal component analysis) aims to reduce the number of dimensions in the dataset, without losing those which are very relevant to the model
# it provides a score, you can drop those with poor scores.
X_pc = PCA(n_components=2).fit_transform(train_features)
pd.DataFrame({'PC1': X_pc[:, 0], 'PC2': X_pc[:, 1], 'Y': train_labels.ravel()}).sample(10)
PCA 通过线性组合初始特征将维度降为 2。转换后,输出是一个具有 [samples, components] 大小的矩阵,因此无法创建数据框,因为您无法投影回 names/features.
重要的特征是对成分影响较大的特征,因此在成分上具有较大的绝对值。
如果您更改代码,您可以获得 PC 上最重要的功能:
from sklearn.decomposition import PCA
import pandas as pd
import numpy as np
np.random.seed(0)
# 10 samples with 5 features
train_features = np.random.rand(10,5)
model = PCA(n_components=2).fit(train_features)
X_pc = model.transform(train_features)
# number of components
n_pcs= model.components_.shape[0]
# get the index of the most important feature on EACH component
# LIST COMPREHENSION HERE
most_important = [np.abs(model.components_[i]).argmax() for i in range(n_pcs)]
initial_feature_names = ['a','b','c','d','e']
# get the names
most_important_names = [initial_feature_names[most_important[i]] for i in range(n_pcs)]
# LIST COMPREHENSION HERE AGAIN
dic = {'PC{}'.format(i+1): most_important_names[i] for i in range(n_pcs)}
# build the dataframe
df = pd.DataFrame(sorted(dic.items()))
这会打印:
0 1
0 PC1 e
1 PC2 d
所以在 PC1 上名为 e
的功能是最重要的,在 PC2 上名为 d
.