0Pricing
Machine Learning Academy · Ders

Boosting Sezgisi: Sıralı Hata Düzeltme

Küçük bir veri kümesinde gradyan artırmanın üç turunu elle benzetin ve her yeni ağacı o ana kadarki topluluğun artıklarına uydurun.

Boosting Sezgisi: Sıralı Hata Düzeltme, CoddyKit'te ücretsiz bir Machine Learning Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Machine Learning Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Machine Learning Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Boosting vs Bagging: A Key Distinction

Bagging, rastgele veri alt kümeleri üzerinde modelleri paralel olarak eğitir ve varyansı azaltarak bunları birleştirir. Boosting ise modelleri ardışık olarak eğitir: Her yeni model, önceki topluluğun yaptığı hataları düzeltmeye odaklanır. Bagging varyansı azaltırken, boosting öncelikle yanlılığı (bias) azaltır; birçok zayıf, yetersiz öğrenen (underfitting) modeli güçlü bir öğreniciye dönüştürebilir. Bu ardışık hata düzeltme stratejisi, gradient boosting, AdaBoost ve XGBoost'un temelini oluşturur.

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.

Sıkça Sorulan Sorular

“Boosting Sezgisi: Sıralı Hata Düzeltme” dersi ücretsiz mi?

Evet — “Boosting Sezgisi: Sıralı Hata Düzeltme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Machine Learning Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Machine Learning Academy kursu toplamda 4 dersten oluşur.

“Boosting Sezgisi: Sıralı Hata Düzeltme” dersinde ne öğreneceğim?

Küçük bir veri kümesinde gradyan artırmanın üç turunu elle benzetin ve her yeni ağacı o ana kadarki topluluğun artıklarına uydurun. Machine Learning Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Machine Learning Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Machine Learning Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.

“Boosting Sezgisi: Sıralı Hata Düzeltme” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Machine Learning Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Machine Learning Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Boosting Sezgisi: Sıralı Hata Düzeltme
  2. XGBoost: Düzenlileştirme, Erken Durdurma ve Özellik Önem Derecesi
  3. LightGBM: Yaprak Bazlı Büyüme ve Hız Avantajları
  4. Temel Hiperparametreler: Öğrenme Oranı, n_estimators ve max_depth
← Machine Learning Academy Sayfasına Dön