使用GridSearchCV拟合MLPRegressor模型



我试图使用GridSearchCV与MLPRegressor来适应我的输入和输出数据集之间的关系。GridSearchCV.predict()方法是否使用交叉验证过程中学习到的最佳参数,还是我需要手动创建一个新的MLPRegessor?

这个工作吗?

# 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)

或者我需要使用最佳参数手动创建一个新模型吗?

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)

GridSearchCV对象的predict方法将使用在网格搜索中找到的最佳参数。所以你的第一个代码块是正确的。这适用于scikit-learn 1.1.1版本,并且至少可以追溯到0.16.1(我抽查的最旧版本)

这可以通过查看scikit-learn网站上的GridSearchCV文档来验证。

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

最新更新