Search code examples
pythonmachine-learningscikit-learngridsearchcvmlp

Fitting a MLPRegressor model using GridSearchCV


I'm trying to use GridSearchCV with an MLPRegressor to fit a relationship between my input and output datasets. Does the GridSearchCV.predict() method use the best parameters learned during cross validation or do I need to manually create a new MLPRegessor?

Does this work?

# Fitting a Regression model to the train data
MLP_gridCV = GridSearchCV(
    estimator=MLPRegressor(max_iter=10000, n_iter_no_change=30),
    param_grid=param_list,
    n_jobs=-1,
    cv=5,
    verbose=5,
)
MLP_gridCV.fit(X_train, Y_train)

# Prediction
Y_prediction = MLP_gridCV.predict(X)

Or do I need to manually create a new model using the best parameters?

best_params = MLP_gridCV.best_params_
best_mlp = MLPRegressor(
    hidden_layer_sizes=best_params["hidden_layer_sizes"],
    activation=best_params["activation"],
    solver=best_params["solver"],
    max_iter=10000,
    n_iter_no_change=30,
)
best_mlp.fit(X_train, Y_train)
best_mlp.predict(X)

Solution

  • The predict method for the GridSearchCV object will use the best parameters found during the grid search. So your first block of code is correct. This applies to scikit-learn version 1.1.1 and goes back to at least 0.16.1 (the oldest version I spot checked)

    This can be verified by checking the GridSearchCV documentation on the scikit-learn site.

    https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html#sklearn.model_selection.GridSearchCV