I am using xgboost
in the following way:
from xgboost import XGBClassifier
clf = XGBClassifier()
clf = clf.fit(df_train, df_train_labels, verbose=True)
This works well. However, if I add an early_stopping_rounds
parameter, like this:
clf = clf.fit(df_train, df_train_labels, early_stopping_rounds=10, verbose=True)
I get this error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-16-786925228ae5> in <module>()
9
10
---> 11 clf = clf.fit(df_train, df_train_labels, early_stopping_rounds=10, verbose=True)
12 print("after fit")
13 prediction = np.exp(clf.predict(df_test))
~/anaconda3/envs/python3/lib/python3.6/site-packages/xgboost/sklearn.py in fit(self, X, y, sample_weight, eval_set, eval_metric, early_stopping_rounds, verbose)
443 early_stopping_rounds=early_stopping_rounds,
444 evals_result=evals_result, obj=obj, feval=feval,
--> 445 verbose_eval=verbose)
446
447 self.objective = xgb_options["objective"]
~/anaconda3/envs/python3/lib/python3.6/site-packages/xgboost/training.py in train(params, dtrain, num_boost_round, evals, obj, feval, maximize, early_stopping_rounds, evals_result, verbose_eval, learning_rates, xgb_model, callbacks)
203 evals=evals,
204 obj=obj, feval=feval,
--> 205 xgb_model=xgb_model, callbacks=callbacks)
206
207
~/anaconda3/envs/python3/lib/python3.6/site-packages/xgboost/training.py in _train_internal(params, dtrain, num_boost_round, evals, obj, feval, xgb_model, callbacks)
99 end_iteration=num_boost_round,
100 rank=rank,
--> 101 evaluation_result_list=evaluation_result_list))
102 except EarlyStopException:
103 break
~/anaconda3/envs/python3/lib/python3.6/site-packages/xgboost/callback.py in callback(env)
190 def callback(env):
191 """internal function"""
--> 192 score = env.evaluation_result_list[-1][1]
193 if len(state) == 0:
194 init(env)
IndexError: list index out of range
I looked this up and I saw that the fit
method can be passed a multitude of parameters, so I don't believe that the fact that I added early_stopping_rounds
should cause problems.
Any idea what could be the cause of this error?
The reason for this error is that you haven't specified an eval_set, which xgboost uses to determine when to stop for early-stopping.
See the docs for the fit method here.
eval_set (list, optional) – A list of (X, y) tuple pairs to use as a validation set for early-stopping
For example, if you've split your data into train and test sets, you would use something along these lines:
eval_set = [(X_test, y_test)]
clf = clf.fit(df_train,
df_train_labels,
eval_set=eval_set,
early_stopping_rounds=10,
verbose=True)
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