0Pricing
Machine Learning Academy · Lección

Intuición sobre Boosting: corrección secuencial de errores

Simule manualmente tres rondas de gradient boosting con un conjunto de datos pequeño, ajustando cada árbol nuevo a los residuos del ensemble acumulado.

Intuición sobre Boosting: corrección secuencial de errores es una lección gratuita de Machine Learning Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Machine Learning Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Machine Learning Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Boosting vs Bagging: A Key Distinction

Bagging trains many models in parallel on random subsets of data and combines them, reducing variance. Boosting trains models sequentially: each new model focuses on correcting the mistakes made by the previous ensemble. While bagging reduces variance, boosting primarily reduces bias — it can turn many weak, underfitting models into a powerful learner. This sequential error-correction strategy is the foundation of gradient boosting, AdaBoost, and XGBoost.

The Idea of Sequential Error Correction

Imagine you are predicting house prices. Your first model predicts everything around the mean and makes large errors. Instead of discarding it, you train a second model specifically on the residuals (the errors: actual − predicted) of the first model. The second model learns the patterns the first missed. A third model then corrects what the second still got wrong. Each new model makes a small improvement on the accumulated ensemble's current error. The final prediction is the sum of all models' predictions.

Boosting by Hand: Three Rounds

Consider a tiny regression dataset with 5 examples. Start with a model that predicts the mean. Compute residuals. Train a tree on residuals. Add a fraction of its predictions (the learning rate) to the current ensemble. Compute new residuals. Repeat. Each round, the residuals should shrink if the trees capture some pattern. After enough rounds, the residuals approach zero for the training set. This is the exact algorithm gradient boosting implements for regression with mean squared error loss.

import numpy as np
# Tiny example: manual 3-round gradient boosting for regression
y = np.array([3.0, 5.0, 2.0, 8.0, 1.0])
pred = np.full(5, y.mean())  # Round 0: predict the mean
print('Round 0 pred:', pred, 'Residuals:', y - pred)
# Round 1: suppose our tree predicts half the residual
residuals_1 = y - pred
pred += 0.5 * residuals_1  # learning_rate=0.5
print('Round 1 pred:', pred, 'Residuals:', np.round(y - pred, 2))
# Round 2
residuals_2 = y - pred
pred += 0.5 * residuals_2
print('Round 2 pred:', np.round(pred, 2), 'Residuals:', np.round(y - pred, 3))

The Learning Rate (Shrinkage)

The learning rate (often called shrinkage) scales down each tree's contribution before adding it to the ensemble. Instead of fully correcting the residual in one step (learning rate = 1.0), boosting takes a small step (e.g., 0.1) and lets subsequent trees correct what remains. A smaller learning rate requires more trees to achieve the same fit but generalises better because no single tree dominates. The trade-off: very small learning rates need very large n_estimators, increasing training time.

Gradient Descent in Function Space

Gradient boosting is named after the connection to gradient descent. In standard gradient descent, we update parameters by moving in the direction that reduces the loss. In gradient boosting, we update the prediction function by fitting a new tree to the negative gradient of the loss with respect to the current predictions. For mean squared error loss, the negative gradient is simply the residual (actual − predicted). For other losses (log-loss, absolute error), it computes the appropriate pseudo-residuals.

GradientBoostingClassifier in scikit-learn

scikit-learn provides GradientBoostingClassifier which implements the original Friedman gradient boosting algorithm. Key parameters: n_estimators (number of trees), learning_rate (shrinkage), max_depth (depth of each tree — shallow trees are preferred, typically 3-5), and subsample (fraction of training data for each tree, adding stochastic element that improves generalisation). The combination of small learning rate, many shallow trees, and subsampling is the standard recipe.

from sklearn.ensemble import GradientBoostingClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score

X, y = load_breast_cancer(return_X_y=True)
gb = GradientBoostingClassifier(
    n_estimators=200,
    learning_rate=0.1,
    max_depth=3,
    subsample=0.8,
    random_state=42
)
scores = cross_val_score(gb, X, y, cv=5)
print('GradientBoosting CV:', round(scores.mean(), 4))

Overfitting in Boosting

Unlike bagging, boosting can overfit with enough estimators, especially at a high learning rate. As you add more rounds, the training loss keeps decreasing but the validation loss eventually rises. The solution is: (1) use early stopping — monitor validation loss and stop when it stops improving; (2) use a small learning rate (0.01-0.1) with many trees rather than a large rate with few; (3) regularise individual trees with shallow depth and minimum samples per leaf. These techniques are all exposed in XGBoost and LightGBM's rich hyperparameter sets.

AdaBoost: The Original Boosting Algorithm

AdaBoost (Adaptive Boosting) was the first practical boosting algorithm. Instead of fitting trees on residuals, it assigns sample weights: misclassified examples get higher weight in the next round, so subsequent trees focus on the hard cases. Each tree's contribution to the final vote is also weighted by its accuracy — better trees get more say. AdaBoost is implemented in scikit-learn as AdaBoostClassifier and is historically important, but gradient boosting (and XGBoost) has largely superseded it in practice.

from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score

X, y = load_breast_cancer(return_X_y=True)
ada = AdaBoostClassifier(
    estimator=DecisionTreeClassifier(max_depth=1),  # stumps
    n_estimators=200,
    learning_rate=0.5,
    random_state=42
)
print('AdaBoost CV:', round(cross_val_score(ada, X, y, cv=5).mean(), 4))

Stochastic Gradient Boosting

Setting subsample < 1.0 in gradient boosting introduces randomness: each tree is trained on a random subset of the training data (without replacement). This stochastic element reduces correlation between trees and often improves generalisation — much like the dropout idea in neural networks. A typical value is subsample=0.8. When subsample is less than 1, scikit-learn also computes an out-of-bag estimate of the improvement at each round, accessible via oob_improvement_.

When Boosting Beats Random Forest

Boosting typically outperforms random forests on clean tabular data where the signal-to-noise ratio is high and there are complex feature interactions. The sequential error-correction mechanism lets boosting squeeze out almost all predictable signal. However, boosting is more sensitive to noisy data and outliers — since each round focuses on hard examples, noise can pull the model in wrong directions. Random forests are more robust to label noise. For noisy, messy real-world data, the choice depends on the specific dataset.

from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score

X, y = load_breast_cancer(return_X_y=True)
gb = GradientBoostingClassifier(n_estimators=200, learning_rate=0.1, max_depth=3, random_state=42)
rf = RandomForestClassifier(n_estimators=200, random_state=42)
print('Gradient Boosting:', round(cross_val_score(gb, X, y, cv=5).mean(), 4))
print('Random Forest:    ', round(cross_val_score(rf, X, y, cv=5).mean(), 4))

Boosting for Regression Tasks

Gradient boosting is equally powerful for regression. GradientBoostingRegressor minimises a regression loss by default (squared error), but also supports absolute error (loss='absolute_error', robust to outliers) and Huber loss (loss='huber', combining squared for small errors and absolute for large ones). For house price prediction, energy forecasting, and similar tasks, gradient boosting consistently outperforms linear regression and often matches or beats neural networks on tabular data without the need for GPU hardware.

from sklearn.ensemble import GradientBoostingRegressor
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import cross_val_score
import numpy as np

X, y = fetch_california_housing(return_X_y=True)
gbr = GradientBoostingRegressor(n_estimators=200, learning_rate=0.1, max_depth=4,
                                  subsample=0.8, random_state=42)
rmse = np.sqrt(-cross_val_score(gbr, X, y, scoring='neg_mean_squared_error', cv=3).mean())
print('GBR Regression RMSE:', round(rmse, 4))

Quick Check

Test your understanding of Boosting and Sequential Error Correction from this lesson.

Lesson Recap

In this lesson you learned: boosting trains models sequentially, each correcting the residual errors of the previous ensemble, the learning rate scales each tree's contribution and controls overfitting, and gradient boosting minimises a differentiable loss by fitting trees to the negative gradient. Next up we explore XGBoost's regularisation and early stopping features.

Preguntas frecuentes

¿La lección «Intuición sobre Boosting: corrección secuencial de errores» es gratis?

Sí — el texto completo de «Intuición sobre Boosting: corrección secuencial de errores» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Machine Learning Academy, actualiza a CoddyKit PRO. El curso de Machine Learning Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Intuición sobre Boosting: corrección secuencial de errores»?

Simule manualmente tres rondas de gradient boosting con un conjunto de datos pequeño, ajustando cada árbol nuevo a los residuos del ensemble acumulado. Practicas Machine Learning Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Machine Learning Academy?

No se requiere experiencia previa. Machine Learning Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «Intuición sobre Boosting: corrección secuencial de errores»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Machine Learning Academy?

Sí. Cada lección de Machine Learning Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Intuición sobre Boosting: corrección secuencial de errores
  2. XGBoost: regularización, parada temprana e importancia de las características
  3. LightGBM: crecimiento hoja a hoja y ventajas de velocidad
  4. Hiperparámetros clave: learning rate, n_estimators y max_depth
← Volver a Machine Learning Academy