我正在使用 xgboost 回归器,如果我使用的是 GridsearchCV,我有一个关于如何使用 model.evals_result() 的问题
我知道如果我不使用 Gridsearch,我可以使用下面的代码得到我想要的
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.33, random_state=1,shuffle=False)
evals_result = {}
eval_s = [(X_train, y_train), (X_test, y_test)]
gbm = xgb.XGBRegressor()
gbm.fit(X_train, y_train,eval_metric=["rmse"],eval_set=eval_s)
results = gbm.evals_result()
但是,如果我在我的代码中使用 GridsearchCV(见下文),我将无法获得 evals_result()。
任何线索?
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.33, random_state=1,shuffle=False)
gbm_param_grid = {'learning_rate': [.01, .1, .5, .9],
'n_estimators': [200, 300],
'subsample': [0.3, 0.5, 0.9]
}
fit_params = {"early_stopping_rounds": 100,
"eval_metric": "mae",
"eval_set": [(X_train, y_train), (X_test, y_test)]}
evals_result = {}
eval_s = [(X_train, y_train), (X_test, y_test)]
gbm = xgb.XGBRegressor()
tscv = TimeSeriesSplit(n_splits=2)
xgb_Gridcv = GridSearchCV(estimator=gbm, param_grid=gbm_param_grid, cv=tscv,refit = True, verbose=0)
xgb_Gridcv.fit(X_train, y_train,eval_metric=["rmse"],eval_set=eval_s)
ypred = xgb_Gridcv.predict(X_test)
现在当我运行
results = gbm.evals_result()
我得到这个错误
Traceback (most recent call last):
File "/Users/prasadkamath/.conda/envs/Pk/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-11-95ef57081806>", line 1, in <module>
results = gbm.evals_result()
File "/Users/prasadkamath/.conda/envs/Pk/lib/python3.5/site-packages/xgboost/sklearn.py", line 401, in evals_result
if self.evals_result_:
AttributeError: 'XGBRegressor' object has no attribute 'evals_result_'