2

I made a GridsearchCV with a pipeline and I want to extract one attribute (n_iter_) of a component of the pipeline (MLPRegressor) for the best model.

I'm using Python 3.0.

Creation of the pipeline

pipeline_steps = [('scaler', StandardScaler()), ('MLPR', MLPRegressor(solver='lbfgs', early_stopping=True, validation_fraction=0.1, max_iter=10000))]

MLPR_parameters = {'MLPR__hidden_layer_sizes':[(50,), (100,), (50,50)], 'MLPR__alpha':[0.001, 10, 1000]}

MLPR_pipeline = Pipeline(pipeline_steps)

gridCV_MLPR = GridSearchCV(MLPR_pipeline, MLPR_parameters, cv=kfold)
gridCV_MLPR.fit(X_train, y_train)

When I want to extract the best model with gridCV_GBR.best_params_, I only have the result for the GridsearchCV :

{'MLPR__alpha': 0.001, 'MLPR__hidden_layer_sizes': (50,)}

But I want to have the number of iteration of MLPRegressor used by the best model of gridCV_MLPR.

How is it possible to use the n_iter_ attribute designed for MLPRegressor() through the pipeline with GridsearhCV ?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
rmarion37
  • 73
  • 7
  • 1
    What about `gridCV_MLPR.best_estimator_.n_iter_`? – desertnaut Aug 09 '19 at 10:17
  • 1
    With this code, I have **'Pipeline' object has no attribute 'n_iter_'**. It's normal because **.n_iter_** is an attribute of MLPregressor(). Which is included in a pipeline (the best one, defined by the GridsearchCV). – rmarion37 Aug 09 '19 at 10:25

1 Answers1

2

Thanks for your help,

I found the solution :

gridCV_MLPR.best_estimator_.named_steps['MLPR'].n_iter_

As the gridCV_MLPR.best_estimator_ is a pipeline, we need to select the MLPRegressor parameters with .named_steps['MLPR'].

Thanks a lot for your very, very quick answer ...

rmarion37
  • 73
  • 7
  • Good job (+1); after the 2 days period, don't forget to actually [accept your own answer](https://stackoverflow.com/help/self-answer). – desertnaut Aug 09 '19 at 10:53