Search code examples
pythonscikit-learnxgboostgrid-searchgridsearchcv

How do I use model.evals_result() in Xgboost if I am using GridsearchCV?


I am using xgboost regressor and I had a question about how to use model.evals_result() if I am using GridsearchCV

I know if I don't use Gridsearch I can get what I want using the below code

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()

ButI am not able to get evals_result() if I am using the GridsearchCV in my code (see below).

anyone clues?

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) 

Now when I run results = gbm.evals_result() I get this error

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

Solution

  • xgb_Gridcv will be the object which contains your best XGB model which can be accessed via xgb_Gridcv.best_estimator_ and now you can call evals_result method on it so in order to get the evals_result you need to use:

    xgb_Gridcv.best_estimator_.evals_result()
    

    Instead of

    gbm.evals_result()
    

    Hope it helps!