Discrete AdaBoost with Decision Stumps
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `fit_adaboost(X, y, n_rounds, random_state=0)` for labels in {-1, +1}. Start from uniform weights and, each round, fit `DecisionTreeClassifier(max_depth=1, random_state=random_state)` with `sample_weight=w`, compute the weighted error `err`, set `alpha = 0.5 * ln((1 - err) / err)`, update `w *= exp(-alpha * y * pred)` and normalise. Return `(weight_history, alphas, stumps)`. Also implement `predict_adaboost(stumps, alphas, X)`, which returns the sign of the weighted vote.
The functions must follow these rules:
- `weight_history` is an ndarray of shape `(n_learners + 1, n)`. Row 0 is uniform and row t+1 holds the weights after accepting learner t. `alphas` is an ndarray of shape `(n_learners,)`. `stumps` is a list.
- If `err >= 0.5`, discard that stump (no alpha, no history row) and stop.
- If `err <= 1e-10`, clip `err` to `1e-10` when computing alpha, keep the stump, append its row, then stop.
- In prediction, a score `>= 0` maps to `+1`, so an ensemble with no stumps predicts all `+1`. Return an int array.
Example:
```python
X = [[0], [1], [2], [3], [4]]
y = [-1, -1, 1, 1, -1]
weight_history, alphas, stumps = fit_adaboost(X, y, n_rounds=1)
alphas # [0.6931] ln(2), err = 0.2
weight_history # [[0.2, 0.2, 0.2, 0.2, 0.2],
# [0.125, 0.125, 0.125, 0.125, 0.5]]
predict_adaboost(stumps, alphas, X) # [-1, -1, 1, 1, 1]
```
```hint Stopping order
Decide whether a stump is accepted before you touch alpha, the weights or the history. The two stopping rules differ in whether the current stump is kept.
```
```hint Which samples grow
Look at the value of `y * pred` on a correct prediction and on a mistake, and at what that does to the exponent in the update.
```
### Constraints & Assumptions
- `y` values are in {-1, +1}. `X` is a 2-D numeric array with `len(X) == len(y)`.
- `n_rounds >= 1`. Early stopping can return fewer learners than `n_rounds`.
### What a Strong Answer Covers
- A training loop that fits a weighted stump, computes the weighted error over the misclassified samples and turns it into alpha.
- Both early-stopping rules in the right order: a stump at or above 0.5 error is dropped without a history row, and a near-perfect stump is clipped, kept and recorded before the loop stops.
- The multiplicative weight update with renormalisation, and a history array with exactly one row per accepted learner plus the uniform starting row.
- Prediction as the sign of the alpha-weighted vote, with a score of 0 and the empty ensemble both mapping to +1, returned as ints.
- Why misclassified samples gain weight, plus the time and space cost of training.
### Follow-up Questions
- A stump with weighted error 0.7 is worse than chance. What would happen if you kept it with the alpha the formula gives?
- Show that `alpha = 0.5 * ln((1 - err) / err)` minimises the weighted exponential loss for a fixed weak learner.
- How do your alphas and sample weights compare with scikit-learn's `AdaBoostClassifier` using SAMME on two classes?
- How does AdaBoost behave when some training labels are wrong, and what would you change to limit the damage?
Overview: Implement discrete AdaBoost for binary labels with depth-1 decision trees as weak learners, recording the sample weights after every round and stopping early on weak or perfect stumps. It tests the weighted error, the vote weight formula, the exponential reweighting step and a sign-based ensemble prediction.