Boost decision stumps with AdaBoost and reweighting
AdaBoost turns many weak classifiers into a strong one. After each round it raises the weight of the points it got wrong, so the next weak learner focuses on them. Gradient boosted trees used in tabular ML grew out of this idea.
Write adaboost(X, y, T, X_test). X has shape (n, d), labels y are -1 or +1, X_test has shape (m, d). Use decision stumps h(x) = s if x[j] > t else -s, with s in {+1, -1}.
Algorithm:
1. Start with weights w_i = 1 / n.
2. For each of T rounds, search every stump: j from 0 to d - 1, t over the sorted unique values of X[:, j], and s = +1 then s = -1. Its weighted error is the sum of w_i over points it misclassifies. Keep the stump with the smallest error. On ties keep the first one found in that search order (treat errors within 1e-12 as tied).
3. Clip the error to [1e-10, 1 - 1e-10] and set alpha = 0.5 * ln((1 - err) / err).
4. Update w_i = w_i * exp(-alpha * y_i * h(x_i)), then divide by the sum so the weights add to 1.
Always run all T rounds. Predict each test point as the sign of sum(alpha * h(x)), with a sum of exactly 0 mapped to +1.
Return (alphas, preds): alphas is a list of T floats, preds is a list of m ints.
def adaboost(X, y, T, X_test)Understand the idea
AdaBoost builds a strong classifier from many weak ones, here one-split decision stumps. Each round picks the stump with the lowest weighted error, gives it a vote alpha that is larger when its error is smaller, then raises the weights of the points it got wrong so the next stump focuses on them. Like a teacher who gives extra practice on the questions the class keeps missing. Worked example: 4 points, each weight 0.25. The best stump misclassifies 1 point, so err = 0.25 and alpha = 0.5 ln(0.75 / 0.25) = 0.5 ln 3 = 0.549. Correct points are multiplied by exp(-0.549) = 0.577, the wrong one by exp(0.549) = 1.732. Before normalising: three correct points total 0.433, the wrong one 0.433. After dividing by the sum, the wrong point has weight 0.5 and each correct one 1/6. Rule: alpha = 0.5 ln((1 - err) / err) with err clipped; w_i *= exp(-alpha y_i h(x_i)), then renormalise; predict sign(sum alpha h(x)), with 0 mapped to +1. Gradient boosting grew from this idea.