LoginSignup
1
1

More than 1 year has passed since last update.

[sklearn]pipeline実行できない

Posted at

scikit-learnのpipelineは前処理やモデルを定義しておくと処理を簡潔にかけるので重要です。
今回、pipelineをつかってgridsearchを組み込んだときにpipelineが動かなかったエラーにハマったのでその解決法です。

param_grid = {'n_components': [2, 3, 4, 5, 6], 'scale':[True, False], 'max_iter': [1000]}

pipe = Pipeline(steps=[('scaler', StandardScaler()),('model', PLSRegression())])
kf = KFold(n_splits=5, shuffle=True, random_state=0)
grid_search = GridSearchCV(pipe, param_grid, cv=kf, refit=True, n_jobs=-1, verbose=1)
エラー
ValueError: Invalid parameter 'max_iter' for estimator Pipeline(steps=[('scaler', StandardScaler()), ('model', PLSRegression())]). Valid parameters are: ['memory', 'steps', 'verbose'].

解決法

param_gridの指定方法を間違えていました。pipelineを組むときはどの処理の引数か判断できるようにprefixをつける必要があります。
指定の方法は「pipeline名 + '_'」をつければok。
今回だと'model_
'をつければ良いです

⭕️
param_grid = {'model__n_components': [2, 3, 4, 5, 6], 'model__scale':[True, False], 'model__max_iter': [1000]}

# ...
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1