尝试绘制测试错误与训练错误时,以下代码有问题:

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/

10-12 22:12