Skip to content

lightgbm/sample-weight-without-eval-weight

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

Passing sample_weight to LightGBM estimator fit() weights training gradients and Hessians, while validation sets (eval_X and eval_y, or eval_set) are scored unweighted unless eval_sample_weight 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_X=(X_val,),
eval_y=(y_val,),
)
model.fit(
X_train,
y_train,
sample_weight=w_train,
eval_X=(X_val,),
eval_y=(y_val,),
eval_sample_weight=[w_val],
)

Calls to fit() on a LightGBM scikit-learn estimator (LGBMRegressor, LGBMClassifier, LGBMRanker, or LGBMModel) that pass a non-None sample_weight together with a validation set (eval_X, eval_y, or eval_set) without a non-None eval_sample_weight.

Pass eval_sample_weight=[w_val] (one weight array per validation set in the same order) so validation loss and early stopping match the weighted objective.

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

See all rules in the LightGBM pack.