管道和 GridSearch 不能一起运行:错误识别参数

原文标题Pipeline & GridSearch not functioning together: error recognizing parameters

如果我使用如下管道,我会收到错误消息

Invalid parameter C for estimator Pipeline(steps=[('robustscaler', RobustScaler()), 
('svc', SVC())]). Check the list of available parameters with `estimator.get_params().keys()`
pipe = make_pipeline(RobustScaler(), SVC())
param_grid = {'C':[1,2,3,4]}
grid = GridSearchCV(pipe,param_grid=param_grid , cv=5)
mod1 = grid.fit(X_train, y_train)

相反,管道外相同的param_grid拟合模型没有错误

param_grid = {'C':[1,2,3,4]}
grid = GridSearchCV(SVC(),param_grid=param_grid , cv=5)
mod1 = grid.fit(X_train, y_train)

无论我想指定什么参数,我也尝试过的其他估计器和缩放器都会发生这种情况。同样,使用相同的管道,我可以直接拟合模型而无需 GridSearchCV (pipe.fit(X_train,y_train))。但是,两者不能同时发挥作用。

原文链接:https://stackoverflow.com//questions/71511793/pipeline-gridsearch-not-functioning-together-error-recognizing-parameters

回复

我来回复
  • I'mahdi的头像
    I'mahdi 评论

    你可以像下面这样使用sklearn.pipeline.Pipeline

    from sklearn.model_selection import GridSearchCV
    from sklearn.preprocessing import RobustScaler
    from sklearn.pipeline import Pipeline
    from sklearn.svm import SVC
    
    param_grid = {'C':[1,2,3,4]}
    pipe = Pipeline(steps=[('scaler', RobustScaler()), ('svc', SVC())])
    grid = GridSearchCV(estimator=pipe, param_grid=param_grid, cv=5)
    mod1 = grid.fit(X_train, y_train)
    
    2年前 0条评论