v9.1.1
Menu

AdaBoost Weight Update

mediumFREEEnsemble MethodsMachine Learning 1

Implement one step of the AdaBoost weight update algorithm.

Given sample weights ww, predictions y^\hat{y}, and true labels yy:

  1. Compute the weighted error: ϵ=∑i:y^i≠yiwi∑wi\epsilon = \frac{\sum_{i: \hat{y}_i \neq y_i} w_i}{\sum w_i}
  2. Compute classifier weight: α=12ln⁡1−ϵϵ\alpha = \frac{1}{2} \ln\frac{1 - \epsilon}{\epsilon}
  3. Update sample weights: wi′=wi⋅e−α⋅yi⋅y^iw_i' = w_i \cdot e^{-\alpha \cdot y_i \cdot \hat{y}_i}
  4. Normalize: wi′′=wi′∑wj′w_i'' = \frac{w_i'}{\sum w_j'}

Labels are {−1,+1}\{-1, +1\}. If the classifier makes no mistakes (ϵ=0\epsilon = 0), clamp ϵ\epsilon to 10−1010^{-10} before computing α\alpha so the logarithm stays finite. Return a tuple (alpha, new_weights) with alpha and each weight rounded to 4 decimal places.

Example

Input
weights = [0.25, 0.25, 0.25, 0.25]
y_true = [1, -1, 1, -1]
y_pred = [1, -1, -1, -1]
Output
(0.5493, [0.1667, 0.1667, 0.5, 0.1667])
Reasoning
  • Inputs: four samples, each with weight 0.250.25 (total 11). True labels are +1,−1,+1,−1+1, -1, +1, -1 and the weak classifier predicted +1,−1,−1,−1+1, -1, -1, -1, so it agrees on samples 1, 2 and 4 and gets only sample 3 wrong.
  • Weighted error: only sample 3 counts, so ϵ=0.251=0.25\epsilon = \frac{0.25}{1} = 0.25.
  • Classifier weight: α=12ln⁡1−0.250.25=12ln⁡3≈0.5493\alpha = \frac{1}{2}\ln\frac{1 - 0.25}{0.25} = \frac{1}{2}\ln 3 \approx 0.5493, the first value of the output.
  • Reweighting: a correct sample has yiy^i=+1y_i \hat{y}_i = +1, so its weight shrinks to 0.25 e−0.5493=0.25/3≈0.14430.25\,e^{-0.5493} = 0.25/\sqrt{3} \approx 0.1443. The wrong one has yiy^i=−1y_i \hat{y}_i = -1 and grows to 0.25 e0.5493=0.253≈0.43300.25\,e^{0.5493} = 0.25\sqrt{3} \approx 0.4330.
  • Normalising: the total is 3(0.1443)+0.4330≈0.86603(0.1443) + 0.4330 \approx 0.8660, giving 0.1443/0.8660≈0.16670.1443/0.8660 \approx 0.1667 for the correct samples and 0.4330/0.8660=0.50.4330/0.8660 = 0.5 for sample 3: [0.1667, 0.1667, 0.5, 0.1667].
  • Notice that the misclassified sample now carries exactly half the total weight, so the next weak learner is pushed to focus on it.

Constraints

  • weights: list of current sample weights
  • y_true, y_pred: lists of labels in {-1, +1}
  • Return (alpha, new_weights) rounded to 4 decimal places
  • alpha is the classifier importance weight

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.