【发布时间】:2021-02-11 08:15:48
【问题描述】:
我正在寻找一种方法来将稀疏 PCA 应用于我的数据集,同时仍保持原始特征不变,以便我可以尝试分类器算法。
将 PCA 插入管道的方法非常简单:
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import SparsePCA
from sklearn.ensemble import RandomForestClassifier
scaler = StandardScaler()
pca = SparsePCA(n_components=2)
rf = RandomForestClassifier()
pipeline_1 = Pipeline(steps=[
('scaler', scaler),
('PCA', pca),
('classifier', rf)
])
但是,这会降低原始数据集的维度。我需要的是某种方法将 Sparce PCA 找到的组件连接到原始数据集中。
我已经看到使用 passthrough 或 FeatureUnion 或 compose.make_column_transformer 可以工作,但我仍然不知道如何应用它。有什么建议吗?
DF 示例:
流水线前:
feature1 feature2 ...
0 1 2
1 2 4
2 3 6
3 4 8
4 5 10
5 6 12
流水线后:
feature1 feature2 ... PCA_comp_1 PCA_comp_1
0 1 2 ... result1 result1
1 2 4 ... result2 result2
2 3 6 ... result3 result3
3 4 8 ... ... ...
4 5 10 ... ... ...
5 6 12 ... ... ...
【问题讨论】:
标签: python scikit-learn pca