Machine learning 键入对象';GridSearchCV';没有属性';cv#u结果';?

Machine learning 键入对象';GridSearchCV';没有属性';cv#u结果';?,machine-learning,scikit-learn,artificial-intelligence,Machine Learning,Scikit Learn,Artificial Intelligence,在尝试绘制测试错误与训练错误时,我遇到以下代码问题: 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="ne

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

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)
您可以查看以下示例,该示例取自文档():

>>> 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']