Skip to content

xgboost/sample-weight-without-eval-weight

Pack Severity Fix Versions
XGBoost warning none, edit by hand All versions

Passing sample_weight to XGBoost estimator fit() weights training gradients and Hessians, while eval_set entries are scored unweighted unless sample_weight_eval_set is also passed. Early stopping and validation curves then optimize a different loss than training.

model.fit(
X_train,
y_train,
sample_weight=w_train,
eval_set=[(X_val, y_val)],
)
model.fit(
X_train,
y_train,
sample_weight=w_train,
eval_set=[(X_val, y_val)],
sample_weight_eval_set=[w_val],
)

Calls to fit() on an XGBoost scikit-learn estimator (XGBRegressor, XGBClassifier, XGBRanker, XGBRFRegressor, XGBRFClassifier, or XGBModel) that pass a non-None sample_weight together with a non-None eval_set without a non-None sample_weight_eval_set.

Pass sample_weight_eval_set=[w_val] (one weight array per eval_set tuple in the same order) so validation metrics and early stopping match the weighted training objective.

fit() passes sample_weight for training without sample_weight_eval_set, so validation loss and early stopping are evaluated unweighted. Pass sample_weight_eval_set with one weight array per eval_set entry.

See all rules in the XGBoost pack.