Scale Pos Weight vs Balanced Class Weights in Gradient Boosting
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `compare_imbalance_weighting(X_train, y_train, X_eval, y_eval, max_iter=50, random_state=0)` for binary labels. Fit two `HistGradientBoostingClassifier` models: one with `scale_pos_weight`-style per-sample weights, one with `class_weight='balanced'`. Return a dict comparing their weights and positive-class recall on the eval set.
The function must meet these requirements:
- Both models use `learning_rate=0.1`, `max_leaf_nodes=15`, `early_stopping=False`, plus the given `max_iter` and `random_state`.
- `scale_pos_weight = n_neg / n_pos`. The `spw_weights` vector holds that value for positives and `1.0` for negatives, and it is passed as `sample_weight` to a model with `class_weight=None`.
- `balanced_weights` holds `n / (2 * n_class)` for each sample, where `n_class` is the training count of that sample's class. `weight_ratio = n / (2 * n_neg)`, so `balanced_weights == weight_ratio * spw_weights`.
- The returned keys are `scale_pos_weight`, `spw_weights`, `balanced_weights`, `weight_ratio`, `recall_spw`, `recall_balanced` (each recall a float: the fraction of eval positives predicted `1`), `spw_model` and `balanced_model`.
- Raise `ValueError` if the labels are not in {0, 1}, `y_train` lacks either class, or `y_eval` has no positives.
Example, for any `X_train` with these labels:
```python
y_train = [0, 0, 0, 0, 0, 0, 1, 1]
# scale_pos_weight -> 3.0
# spw_weights -> [1, 1, 1, 1, 1, 1, 3, 3]
# balanced_weights -> [2/3]*6 + [2.0, 2.0]
# weight_ratio -> 2/3
```
```hint Recall denominator
Positive-class recall looks only at the eval rows whose true label is 1. Check that your denominator counts those rows and not the rows predicted 1.
```
### What a Strong Answer Covers
- Correct class counts and both weight vectors, with the constant factor that links them.
- Two models configured identically apart from the weighting, with the `scale_pos_weight` weights passed as `sample_weight` and `class_weight=None`, so no weight is applied twice.
- Positive-class recall computed over eval positives only, and the three validation errors.
- Why the two recalls come out equal or very close, and which settings could make them differ.
### Follow-up Questions
- If you set `l2_regularization=1.0` on both models, which one is regularized more, and how would you make them equivalent again?
- Up-weighting positives inflates the predicted probabilities. How would you recover calibrated probabilities from either model?
- When would you move the decision threshold on an unweighted model instead of reweighting the classes?
Overview: Compare two class-imbalance weighting schemes for scikit-learn's HistGradientBoostingClassifier: scale_pos_weight style sample weights and balanced class weights. It tests computing both weight vectors, the constant factor that links them, positive-class recall on an eval set, and how weight scale affects the fitted trees.