📘
AdaBoost Weight Update
MediumEnsemble Methods
Implement one step of the AdaBoost weight update algorithm.
Given sample weights w, predictions y^, and true labels y:
- Compute the weighted error: ϵ=∑wi∑i:y^i=yiwi
- Compute classifier weight: α=21lnϵ1−ϵ
- Update sample weights: wi′=wi⋅e−α⋅yi⋅y^i
- Normalize: wi′′=∑wj′wi′
Labels are {−1,+1}. 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:
- First, we calculate the weighted error ϵ=∑wi∑i:y^i=yiwi. For the given input, y^i=yi for i=2,3, so ϵ=10.25+0.25=0.5.
- Then, we compute the classifier weight: α=21lnϵ1−ϵ=21ln0.51−0.5=21ln0.50.5=21ln(1)=0 is incorrect due to the properties of ln(1), we should instead get α=21ln0.50.5=0 is a special case. However, given ϵ=0.5, α=21ln0.51−0.5=21ln(1)=0 is not the case here, we actually calculate α using ϵ in the formula which results in α=21ln0.50.5. But since ϵ1−ϵ=0.50.5=1, α=21ln(1)=0 would be the case if ϵ was not exactly 0.5, for ϵ=0.5, α=0 would not be the correct calculation. Let's correct that: given ϵ=0.5, α should actually be calculated with the given ϵ which leads to α=21ln0.51−0.5. The issue here is that 0.51−0.5=1, and $\
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
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.