使用 GridSearchCV 寻找最佳超参数

发布于 2025-01-13 16:37:47 字数 767 浏览 5 评论 0原文

您好,我是 GridSearchCV 的新手,我正在尝试使用 best_params_ 属性识别最佳参数。它运行正确,但它只返回“max_深度”:3,而我期望它也返回最佳的 max_leaf_nodes 和最佳的 min_samples_split 。请查看下面的代码,如果我没有正确执行或理解某些内容,请告诉我。谢谢!

from sklearn.model_selection import GridSearchCV

param_grid = [
  {'max_depth': [1,2,3,4,5,8,16,32]},
  {'max_leaf_nodes': list(range(2, 20, 1))},
  {'min_samples_split': [2,3,4,5,8,12,16,20]},
 ]

# Call the fit() method to perform the grid search using 3-fold cross-validation.
grid_search_cv = GridSearchCV(DecisionTreeClassifier(random_state=42), param_grid, verbose=1, cv=3) 

# Fit the model to the training set 
grid_search_cv.fit(X_train, y_train)

print("The best parameters are: ", grid_search_cv.best_params_)

Output:  The best parameters are: {'max_depth': 3}

Hi I'm new to GridSearchCV and I'm trying to get the best parameters identified using the best_params_ attribute. It runs correctly, but it only returns a 'max_depth':3, when I'm expecting it to also return the best max_leaf_nodes and best min_samples_split as well. Please see my code below and let me know if I'm not doing or understanding something correctly. Thanks!

from sklearn.model_selection import GridSearchCV

param_grid = [
  {'max_depth': [1,2,3,4,5,8,16,32]},
  {'max_leaf_nodes': list(range(2, 20, 1))},
  {'min_samples_split': [2,3,4,5,8,12,16,20]},
 ]

# Call the fit() method to perform the grid search using 3-fold cross-validation.
grid_search_cv = GridSearchCV(DecisionTreeClassifier(random_state=42), param_grid, verbose=1, cv=3) 

# Fit the model to the training set 
grid_search_cv.fit(X_train, y_train)

print("The best parameters are: ", grid_search_cv.best_params_)

Output:  The best parameters are: {'max_depth': 3}

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

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

发布评论

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

评论(1

掩耳倾听 2025-01-20 16:37:47

我想通了。我不应该在字典的每一行上都有括号。

I figured it out. I shouldn't have a bracket on every line of my dictionary.

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