XGBModel' object has no attribute 'evals_result_'
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
If you see AttributeError: 'XGBModel' object has no attribute 'evals_result_', the most likely issue is that you are mixing API styles. In current XGBoost scikit-learn wrappers, evaluation history is typically retrieved with the evals_result() method after fitting with an eval_set, not through an evals_result_ attribute.
Why the Error Happens
XGBoost exposes more than one Python interface. The native training API and the scikit-learn wrapper do not surface training history in exactly the same way, and older blog posts often mix them.
A common failing pattern looks like this:
There are two problems here:
- no evaluation set was supplied, so no per-round validation history was recorded
- the wrapper API expects
evals_result()in modern documentation
That combination is what usually produces the attribute error.
The Correct Pattern in the Scikit-Learn Wrapper
To record evaluation metrics during training, pass an eval_set to fit(). After that, call evals_result().
This returns a dictionary keyed by validation set name, usually validation_0, validation_1, and so on.
Plotting the Training Curve
The evaluation history is most useful when you graph it. That makes overfitting or early convergence much easier to spot.
If the validation curve stops improving while the training curve keeps improving, you are probably training for too many boosting rounds.
Early Stopping Works with the Same History
Evaluation history becomes especially useful when you enable early stopping.
Now you can inspect both the stopping point and the metric history that led to it.
Native API Looks Different
The native xgboost.train() function uses a different pattern. There you pass a dictionary that gets filled during training.
This is a separate interface, so code examples from it should not be copied directly into XGBRegressor or XGBClassifier examples.
A Safe Way to Access Results
If you are working in a codebase with mixed XGBoost versions or styles, guard the call explicitly.
That is much safer than assuming an underscore-suffixed attribute will exist.
Common Pitfalls
A common mistake is forgetting the eval_set. Without it, there is nothing to record for each boosting round.
Another issue is copying outdated examples that use evals_result_ instead of the wrapper method documented in current releases.
Developers also sometimes expect evaluation history to survive every serialization path automatically. Save the history separately if your workflow depends on it.
Finally, do not mix native API patterns with scikit-learn wrapper patterns unless you are deliberately switching interfaces.
Summary
- In the XGBoost scikit-learn wrapper, use
evals_result()rather thanevals_result_. - Pass an
eval_settofit()if you want per-round metric history. - Use the returned dictionary to inspect training and validation metrics.
- Early stopping and evaluation history work naturally together.
- Keep native
xgboost.train()examples separate from wrapper-based code.
Related reading
- Xgboost-How to use mae as objective function?
- XGBOOST-Multi class prediction. Prediction matrix is set of probabilities for classes. How to perform confusion matrix
- XGBoost - n_estimators 1 equal to single-tree classifier?
- XGBoost AttributeError 'DataFrame' object has no attribute 'feature_names
- xgboost AttributeError 'DMatrix' object has no attribute 'handle
- xlrd.biffh.XLRDError Excel xlsx file; not supported
- xgboost binary logistic regression
- XGboost cannot pass validation data for eval_set in pipeline
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.