使用 GridSearchCV 寻找最佳超参数
您好,我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。我不应该在字典的每一行上都有括号。
I figured it out. I shouldn't have a bracket on every line of my dictionary.