使用GridSearchCV拟合MLPregressor模型

发布于 2025-02-03 07:50:29 字数 924 浏览 5 评论 0 原文

我正在尝试将 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2025-02-10 07:50:29

预测 GridSearchCV 对象将使用网格搜索过程中找到的最佳参数。因此,您的第一个代码块是正确的。这适用于Scikit-Learn版本1.1.1,至少回到0.16.1(我发现了最古老的版本),

可以通过检查Scikit-Learn网站上的 GridSearchCV 文档来验证这一点。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文