Metrics¶
Evaluation metrics for XGBoost/LightGBM that work with custom objectives.
Important: Disable Default Metrics
When using custom objectives, XGBoost's default evaluation metrics may not be meaningful.
Always set 'disable_default_eval_metric': 1 in params for XGBoost, or 'metric': 'None' for LightGBM.
Quick Start¶
from jaxboost.objective import ordinal_logit
from jaxboost.metric import qwk_metric
# Create ordinal objective
ordinal = ordinal_logit(n_classes=6)
ordinal.init_thresholds_from_data(y_train)
# Train with custom metric
model = xgb.train(
{'disable_default_eval_metric': 1, 'max_depth': 4},
dtrain,
obj=ordinal.xgb_objective,
custom_metric=ordinal.qwk_metric.xgb_metric, # Built-in metric
evals=[(dtest, 'test')]
)
Base Classes¶
Metric¶
Metric ¶
Metric(name: str, fn: Callable[[ndarray, ndarray], float], transform: Callable[[ndarray], ndarray] | None = None, higher_is_better: bool = True)
Base class for XGBoost/LightGBM evaluation metrics.
Provides both XGBoost and LightGBM compatible interfaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Metric name displayed during training |
required |
fn
|
Callable[[ndarray, ndarray], float]
|
Metric function (y_true, y_pred) -> float |
required |
transform
|
Callable[[ndarray], ndarray] | None
|
Optional prediction transform (e.g., sigmoid for binary) |
None
|
higher_is_better
|
bool
|
Whether higher metric values are better |
True
|
Example
metric = Metric( ... name='accuracy', ... fn=lambda y, p: (y == p).mean(), ... transform=lambda p: (p > 0.5).astype(int), ... higher_is_better=True ... )
Use with XGBoost¶
model = xgb.train(params, dtrain, custom_metric=metric.xgb_metric)
Use with LightGBM¶
model = lgb.train(params, train_data, feval=metric.lgb_metric)
Source code in src/jaxboost/metric/base.py
__call__ ¶
xgb_metric ¶
XGBoost-compatible metric function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predt
|
ndarray
|
Raw predictions from model |
required |
dtrain
|
Any
|
XGBoost DMatrix with labels |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, float]
|
(metric_name, metric_value) |
Source code in src/jaxboost/metric/base.py
lgb_metric ¶
LightGBM-compatible metric function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
ndarray
|
Raw predictions from model |
required |
eval_data
|
Any
|
LightGBM Dataset with labels |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, float, bool]
|
(metric_name, metric_value, is_higher_better) |
Source code in src/jaxboost/metric/base.py
make_metric¶
make_metric ¶
make_metric(name: str, transform: Callable[[ndarray], ndarray] | None = None, higher_is_better: bool = True) -> Callable
Decorator to create XGBoost/LightGBM compatible metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Metric name shown during training |
required |
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to transform raw predictions |
None
|
higher_is_better
|
bool
|
Whether higher values are better |
True
|
Example
@make_metric('my_accuracy', transform=lambda p: (p > 0.5).astype(int)) ... def my_accuracy(y_true, y_pred): ... return (y_true == y_pred).mean()
Use with XGBoost¶
model = xgb.train(params, dtrain, custom_metric=my_accuracy.xgb_metric)
Returns:
| Type | Description |
|---|---|
Callable
|
Decorated function that has .xgb_metric and .lgb_metric attributes |
Source code in src/jaxboost/metric/base.py
Ordinal Metrics¶
For ordered categorical outcomes (ratings, grades, severity levels).
qwk_metric¶
qwk_metric ¶
Create Quadratic Weighted Kappa metric for ordinal regression.
QWK penalizes predictions quadratically by distance from truth. Perfect agreement = 1, random agreement ≈ 0, worse than random < 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_classes
|
int
|
Number of ordinal classes |
required |
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to class labels. If None, predictions are rounded and clipped. |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object with .xgb_metric and .lgb_metric methods |
Example
With ordinal objective¶
ordinal = ordinal_logit(n_classes=6) qwk = qwk_metric(n_classes=6, transform=ordinal.predict)
model = xgb.train( ... {'disable_default_eval_metric': 1}, ... dtrain, obj=ordinal.xgb_objective, ... custom_metric=qwk.xgb_metric ... )
Source code in src/jaxboost/metric/ordinal.py
ordinal_mae_metric¶
ordinal_mae_metric ¶
Create Mean Absolute Error metric for ordinal regression.
Measures average distance between predicted and true class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_classes
|
int
|
Number of ordinal classes |
required |
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to class labels |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Example
mae = ordinal_mae_metric(n_classes=6) model = xgb.train(params, dtrain, custom_metric=mae.xgb_metric)
Source code in src/jaxboost/metric/ordinal.py
ordinal_accuracy_metric¶
ordinal_accuracy_metric ¶
ordinal_accuracy_metric(n_classes: int, transform: Callable[[ndarray], ndarray] | None = None) -> Metric
Create exact accuracy metric for ordinal regression.
Measures proportion of exactly correct predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_classes
|
int
|
Number of ordinal classes |
required |
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to class labels |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/ordinal.py
adjacent_accuracy_metric¶
adjacent_accuracy_metric ¶
adjacent_accuracy_metric(n_classes: int, transform: Callable[[ndarray], ndarray] | None = None) -> Metric
Create adjacent accuracy metric (within ±1) for ordinal regression.
Measures proportion of predictions within 1 class of truth. Useful when exact prediction is difficult but close is acceptable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_classes
|
int
|
Number of ordinal classes |
required |
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to class labels |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/ordinal.py
Classification Metrics¶
For binary classification problems.
auc_metric¶
auc_metric ¶
Create Area Under ROC Curve metric for binary classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to probabilities. If None, sigmoid is applied. |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object with .xgb_metric and .lgb_metric methods |
Example
from jaxboost.metric import auc_metric
model = xgb.train( ... {'disable_default_eval_metric': 1}, ... dtrain, obj=focal_loss.xgb_objective, ... custom_metric=auc_metric().xgb_metric ... )
Source code in src/jaxboost/metric/classification.py
log_loss_metric¶
log_loss_metric ¶
log_loss_metric(transform: Callable[[ndarray], ndarray] | None = None, eps: float = 1e-07) -> Metric
Create Log Loss (binary cross-entropy) metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to probabilities |
None
|
eps
|
float
|
Small value for numerical stability |
1e-07
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/classification.py
accuracy_metric¶
accuracy_metric ¶
accuracy_metric(threshold: float = 0.5, transform: Callable[[ndarray], ndarray] | None = None) -> Metric
Create accuracy metric for binary classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Classification threshold (default 0.5) |
0.5
|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to probabilities |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/classification.py
f1_metric¶
f1_metric ¶
Create F1 score metric for binary classification.
F1 = 2 * (precision * recall) / (precision + recall)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Classification threshold |
0.5
|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to probabilities |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/classification.py
precision_metric¶
precision_metric ¶
precision_metric(threshold: float = 0.5, transform: Callable[[ndarray], ndarray] | None = None) -> Metric
Create precision metric for binary classification.
Precision = TP / (TP + FP)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Classification threshold |
0.5
|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to probabilities |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/classification.py
recall_metric¶
recall_metric ¶
recall_metric(threshold: float = 0.5, transform: Callable[[ndarray], ndarray] | None = None) -> Metric
Create recall metric for binary classification.
Recall = TP / (TP + FN)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Classification threshold |
0.5
|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to convert raw predictions to probabilities |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/classification.py
Regression Metrics¶
For continuous target prediction.
mse_metric¶
mse_metric ¶
Create Mean Squared Error metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to transform raw predictions |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Example
model = xgb.train( ... {'disable_default_eval_metric': 1}, ... dtrain, obj=my_objective.xgb_objective, ... custom_metric=mse_metric().xgb_metric ... )
Source code in src/jaxboost/metric/regression.py
rmse_metric¶
rmse_metric ¶
Create Root Mean Squared Error metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to transform raw predictions |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/regression.py
mae_metric¶
mae_metric ¶
Create Mean Absolute Error metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to transform raw predictions |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/regression.py
r2_metric¶
r2_metric ¶
Create R² (coefficient of determination) metric.
R² = 1 - SS_res / SS_tot
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to transform raw predictions |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Source code in src/jaxboost/metric/regression.py
Bounded Regression Metrics¶
For proportion/rate prediction in [0, 1].
bounded_mse_metric¶
bounded_mse_metric ¶
Create MSE metric for bounded regression.
By default, applies sigmoid to transform logits to [0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable[[ndarray], ndarray] | None
|
Optional function to transform raw predictions to [0, 1]. If None, sigmoid is applied. |
None
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Example
For bounded regression with sigmoid link¶
model = xgb.train( ... {'disable_default_eval_metric': 1}, ... dtrain, obj=soft_ce.xgb_objective, ... custom_metric=bounded_mse_metric().xgb_metric ... )
Source code in src/jaxboost/metric/bounded.py
out_of_bounds_metric¶
out_of_bounds_metric ¶
Create metric to measure proportion of predictions outside valid bounds.
Useful for comparing bounded vs unbounded regression approaches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lower
|
float
|
Lower bound (default 0.0) |
0.0
|
upper
|
float
|
Upper bound (default 1.0) |
1.0
|
Returns:
| Type | Description |
|---|---|
Metric
|
Metric object |
Example
Check how many predictions fall outside [0, 1]¶
model = xgb.train( ... {'disable_default_eval_metric': 1}, ... dtrain, obj=mse.xgb_objective, ... custom_metric=out_of_bounds_metric().xgb_metric ... )
Source code in src/jaxboost/metric/bounded.py
XGBoost vs LightGBM Interface¶
| Aspect | XGBoost | LightGBM |
|---|---|---|
| Metric param | custom_metric= |
feval= |
| Metric method | .xgb_metric |
.lgb_metric |
| Return value | (name, value) |
(name, value, is_higher_better) |
| Disable default | 'disable_default_eval_metric': 1 |
'metric': 'None' |
XGBoost Example¶
model = xgb.train(
{'disable_default_eval_metric': 1},
dtrain,
obj=objective.xgb_objective,
custom_metric=metric.xgb_metric,
evals=[(dtest, 'test')]
)