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



考虑将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_digitals的最后十列(?

您可以首先创建一个函数(下面称为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)

接受的答案也为逻辑回归选择特征。如果这不是所需的行为,则可以使用ColumnTransfer。

https://scikit-learn.org/stable/modules/generated/sklearn.compose.ColumnTransformer.html

pca_transformer = ColumnTransformer([('pca', PCA(), [-10:])], remainder="passthrough")
pipe = Pipeline(steps=[('pca_transformer', pca_transformer), ('logistic', logistic)])

相关内容

  • 没有找到相关文章

最新更新