Xgboost-How to use mae as objective function?
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
When people ask for "MAE as the objective" in XGBoost, they often mean two different things: optimizing with absolute error during training, or simply reporting MAE while training a different loss. Those are not the same, and the right answer depends on your XGBoost version and how stable you need the optimization to be.
Objective vs Evaluation Metric
The objective controls how XGBoost updates the model. The eval_metric controls what gets reported on validation data. You can use MAE as an evaluation metric without training on an MAE objective:
This setup still optimizes squared error, but it shows validation performance in MAE units. That is useful when MAE is the business-facing metric, yet you want the smoother optimization behavior of squared loss.
Using a True MAE Objective in Modern XGBoost
Current XGBoost releases support an absolute-error regression objective named reg:absoluteerror. Official documentation notes that this objective was added in version 1.7.0. If your environment is recent enough, the simplest solution is:
This trains the model with L1 loss and also reports MAE during evaluation. For many tabular regression tasks, that is the most direct answer.
Why MAE Used to Be Awkward
Absolute error is less smooth than squared error. Around zero residual, its derivative changes abruptly, which makes optimization more awkward for methods that rely on gradient and Hessian information. That is why older XGBoost advice often said "use MAE only as an evaluation metric" or "use a custom approximation instead."
You will still see older examples using reg:squarederror plus eval_metric="mae" because:
- older XGBoost versions did not expose
reg:absoluteerror - some teams prefer the training stability of squared loss
- distributed training has special notes around how leaf values are refreshed for absolute error
So if you read conflicting answers online, check the library version first.
What to Do on Older Versions
If you are pinned to an older XGBoost release, you have two practical options.
First, keep the training objective as squared error and track MAE as the validation metric:
Second, if you truly need a custom loss, use a differentiable approximation such as pseudo-Huber loss:
Pseudo-Huber behaves like a smoothed form of absolute error. It is not identical to MAE, but it is often a practical compromise when you want robustness to outliers without the sharp corner of plain L1 loss.
A Version Check You Can Run
Before choosing an approach, confirm what version is installed:
If that prints a version older than 1.7.0, using reg:absoluteerror may fail with an "unknown objective" style error.
Choosing Between the Options
Use reg:absoluteerror when:
- you are on a recent XGBoost release
- MAE is genuinely the loss you want to optimize
- a slightly different training behavior is acceptable
Use reg:squarederror with eval_metric="mae" when:
- you mostly care about MAE for reporting
- your codebase is on an older release
- you want more conventional optimization behavior
Use reg:pseudohubererror when:
- you want robustness similar to MAE
- you need a smoother alternative
- plain squared error is too sensitive to outliers
Common Pitfalls
- Setting
eval_metric="mae"does not change the training objective by itself. - Copying old examples without checking
xgboost.__version__can lead to outdated advice. - Assuming MAE is always better for outliers ignores the fact that optimization and convergence behavior also matter.
- Using a custom objective when a built-in one exists adds unnecessary complexity.
- Comparing models trained with different objectives but only one metric can hide tradeoffs in stability and calibration.
Summary
- In XGBoost,
objectiveandeval_metricsolve different problems. - Modern XGBoost supports true L1 training with
objective="reg:absoluteerror". - Older versions often need either
eval_metric="mae"with squared error or a smoother approximation such asreg:pseudohubererror. - Check your installed version before choosing an answer from older tutorials.
- If you only need MAE for reporting, using it as the evaluation metric is often enough.
Related reading
- 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
- xgboost binary logistic regression
- XGboost cannot pass validation data for eval_set in pipeline
- XGBoost error - When categorical type is supplied, DMatrix parameter enable_categorical must be set to True
- XGBoost for multilabel classification?
.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.