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_'
In general you can access the dictionary evals_result
directly, as opposed to accessing a method of the model, e.g. xgb_model.evals_result(). For example:
eval_s = [(X_train, y_train), (X_test, y_test)]
evals_result = {}
xgb_model = xgb.train(param,
train_orig_data_dmat,
num_boost_round=100,
evals=eval_s,
early_stopping_rounds=10,
evals_result=evals_result)
print(evals_result)
will print out error for train and test respectively, together with any evaluation metrics you define. Here is another, more detailed reference: https://github.com/dmlc/xgboost/blob/master/demo/guide-python/evals_result.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With