gpt4 book ai didi

machine-learning - 类型对象 'GridSearchCV' 没有属性 'cv_results_' ?

转载 作者:行者123 更新时间:2023-11-30 09:58:12 36 4
gpt4 key购买 nike

在尝试绘制测试错误与训练错误时,我遇到以下代码问题:

from sklearn.model_selection import GridSearchCV


trees_grid = {"n_estimators":[100,150,200,250,300,350,400,450]}

grid_search = GridSearchCV(estimator=xgb,n_jobs=1,param_grid=trees_grid,
scoring="neg_mean_absolute_error",cv=10,verbose=1,return_train_score=True)


results = pd.DataFrame(grid_search.cv_results_)
figsize(8,8)
plt.style.use("ggplot")
plt.plot(results["param_n_estimators"], -1*results["mean_test_score"], label="testing error")
plt.plot(results["param_n_estimators"], -1*results["mean_train_score"], label="training error")
plt.legend()
plt.ylabel("mean absolute error", size=20)
plt.xlabel("number of trees", size= 20)
plt.show()

我的sklearn版本是0.22.1。我也尝试过 grid_search.grid_scores_ 但显然不起作用。

最佳答案

为了拥有此属性,您必须首先适合您的 GridSearchCV:

grid_search.fit(train_data, train_labels)

您可以查看以下示例,该示例取自文档 ( https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html ):

>>> from sklearn import svm, datasets
>>> from sklearn.model_selection import GridSearchCV
>>> iris = datasets.load_iris()
>>> parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
>>> svc = svm.SVC()
>>> clf = GridSearchCV(svc, parameters)
>>> clf.fit(iris.data, iris.target)
GridSearchCV(estimator=SVC(),
param_grid={'C': [1, 10], 'kernel': ('linear', 'rbf')})
>>> sorted(clf.cv_results_.keys())
['mean_fit_time', 'mean_score_time', 'mean_test_score',...
'param_C', 'param_kernel', 'params',...
'rank_test_score', 'split0_test_score',...
'split2_test_score', ...
'std_fit_time', 'std_score_time', 'std_test_score']

关于machine-learning - 类型对象 'GridSearchCV' 没有属性 'cv_results_' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60133822/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com