使用FunctionTransformer在特征子集上使用PCA的sklearn管道

时间:2018-11-30 16:42:17

标签: python scikit-learn pca

考虑链接PCA和回归的任务,其中PCA执行降维,而回归执行预测。

示例来自sklearn文档:

import numpy as np
import matplotlib.pyplot as plt

from sklearn import linear_model, decomposition, datasets
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV

logistic = linear_model.LogisticRegression()

pca = decomposition.PCA()
pipe = Pipeline(steps=[('pca', pca), ('logistic', logistic)])

digits = datasets.load_digits()
X_digits = digits.data
y_digits = digits.target

n_components = [5, 10]
Cs = np.logspace(-4, 4, 3)

param_grid = dict(pca__n_components=n_components, logistic__C=Cs)
estimator = GridSearchCV(pipe,param_grid)
estimator.fit(X_digits, y_digits)

如何仅使用FunctionTransformer对特征集的子集执行降维处理(例如,将PCA限制为X_digits的最后十列)?

1 个答案:

答案 0 :(得分:1)

您可以首先创建一个函数(以下称为last_ten_columns),该函数返回输入X_digits的最后10列。创建指向该函数的函数转换器,并将其用作管道的第一步。

import numpy as np
import matplotlib.pyplot as plt

from sklearn import linear_model, decomposition, datasets
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import FunctionTransformer

logistic = linear_model.LogisticRegression()

pca = decomposition.PCA()

def last_ten_columns(X):
    return X[:, -10:]

func_trans = FunctionTransformer(last_ten_columns)

pipe = Pipeline(steps=[('func_trans',func_trans), ('pca', pca), ('logistic', logistic)])

digits = datasets.load_digits()
X_digits = digits.data
y_digits = digits.target

n_components = [5, 10]
Cs = np.logspace(-4, 4, 3)

param_grid = dict(pca__n_components=n_components, logistic__C=Cs)
estimator = GridSearchCV(pipe, param_grid)
estimator.fit(X_digits, y_digits)