通过嵌套的RFECV和GridSearchCV传递管道时出现问题

时间:2019-11-03 08:11:14

标签: python scikit-learn

我正在尝试为sklearn中的嵌套CV的内部循环执行特征选择和网格搜索。虽然我可以将流水线作为估计量传递到RFECV,但是当我将RFECV作为估计量传递给GridSearchCV时,会收到拟合错误。

我发现将管道中的模型名称更改为“估计”会使用“使无效参数回归”将错误移至管道,而不是在RFECV中将模型命名为无效的参数。

我已经使用rfcv.get_params().keys()pipeline.get_params().keys()验证了我所调用的参数确实存在。

如果我直接将SGDRegressor()命名为“ estimator”并完全忽略管道,则不会收到此错误,但是此模型需要特征缩放和Y变量的对数转换。

from sklearn.compose import TransformedTargetRegressor
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.feature_selection import RFECV
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import SGDRegressor

import numpy as np
# random sample data
X = np.random.rand(100,2)
y = np.random.rand(100)

#passing coef amd importance through pipe and TransformedTargetRegressor
class MyPipeline(Pipeline):
    @property
    def coef_(self):
        return self._final_estimator.coef_
    @property
    def feature_importances_(self):
        return self._final_estimator.feature_importances_

class MyTransformedTargetRegressor(TransformedTargetRegressor):
    @property
    def feature_importances_(self):
        return self.regressor_.feature_importances_

    @property
    def coef_(self):
        return self.regressor_.coef_

# build pipeline
pipeline = MyPipeline([ ('scaler', MinMaxScaler()),
                     ('estimator', MyTransformedTargetRegressor(regressor=SGDRegressor(), func=np.log1p, inverse_func=np.expm1))]) 

# define tuning grid
parameters = {"estimator__regressor__alpha": [1e-5,1e-4,1e-3,1e-2,1e-1], 
              "estimator__regressor__l1_ratio": [0.001,0.25,0.5,0.75,0.999]} 

# instantiate inner cv
inner_kv = KFold(n_splits=5, shuffle=True, random_state=42)
rfcv = RFECV(estimator=pipeline, step=1, cv=inner_kv, scoring="neg_mean_squared_error")

cv = GridSearchCV(estimator=rfcv, param_grid=parameters, cv=inner_kv, iid=True,
                  scoring= "neg_mean_squared_error", n_jobs=-1, verbose=True)
cv.fit(X,y)

我收到以下错误,并可以确认回归变量是估计器管道的参数:

ValueError: Invalid parameter regressor for estimator MyPipeline(memory=None,
           steps=[('scaler', MinMaxScaler(copy=True, feature_range=(0, 1))),
                  ('estimator',
                   MyTransformedTargetRegressor(check_inverse=True,
                                                func=<ufunc 'log1p'>,
                                                inverse_func=<ufunc 'expm1'>,
                                                regressor=SGDRegressor(alpha=0.0001,
                                                                       average=False,
                                                                       early_stopping=False,
                                                                       epsilon=0.1,
                                                                       eta0=0.01,
                                                                       fit_intercept=True,
                                                                       l1_ratio=0.15,
                                                                       learning_rate='invscaling',
                                                                       loss='squared_loss',
                                                                       max_iter=1000,
                                                                       n_iter_no_change=5,
                                                                       penalty='l2',
                                                                       power_t=0.25,
                                                                       random_state=None,
                                                                       shuffle=True,
                                                                       tol=0.001,
                                                                       validation_fraction=0.1,
                                                                       verbose=0,
                                                                       warm_start=False),
                                                transformer=None))],
           verbose=False). Check the list of available parameters with `estimator.get_params().keys()`.

谢谢

1 个答案:

答案 0 :(得分:0)

它必须为estimator__estimator__regressor,因为您在rfecv内部有管道。

尝试一下!

parameters = {"estimator__estimator__regressor__alpha": [1e-5,1e-4,1e-3,1e-2,1e-1], 
              "estimator__estimator__regressor__l1_ratio": [0.001,0.25,0.5,0.75,0.999]} 

注意:拥有嵌套的简历不是正确的方法。可能是您可以单独进行特征选择,然后进行模型训练。