Lesson 4
Gradient Boosting in Machine Learning
Lesson Introduction

Hello there! Today, we're going to explore Gradient Boosting, a powerful technique that improves the accuracy of machine learning models. Our goal is to understand what Gradient Boosting is, how it works, and how to use it with a real example in Python. By the end, you'll know how to implement Gradient Boosting and apply it to a dataset.

What is Gradient Boosting?

Gradient Boosting is an ensemble technique that combines multiple weak learners, usually decision trees, to form a stronger, more accurate model. Unlike Bagging and Random Forests, which create models independently, Gradient Boosting builds models sequentially. Each new model aims to correct errors made by the previous ones.

Imagine baking a cake. The first cake might not be perfect — maybe too dry or not sweet enough. The next time, you make changes to improve it based on previous errors. Over time, you get closer to perfection. This is how Gradient Boosting works.

Here's a step-by-step explanation of Gradient Boosting:

  1. Start with an initial model: This can be a simple model like a single decision tree.
  2. Calculate errors: Find out where the initial model makes mistakes.
  3. Build the next model: Create a new model that focuses on correcting the errors from the initial model using gradients.
  4. Combine models: Add the new model to the existing ones to create a stronger model.
  5. Repeat: Continue this process until desired accuracy is achieved or a specified number of models is built.

Consider tuning a musical instrument. Initially, it may be out of tune. By fine-tuning each string separately, you reduce the overall error (or off-tune sound) until the instrument sounds perfect.

Gradient Boosting vs. AdaBoost

Gradient Boosting and AdaBoost are both boosting techniques but they differ in their approach to combining weak learners.

  • AdaBoost: Each subsequent model focuses more on the instances that previous models misclassified. It assigns weights to instances, increasing weights for those that are hard to classify.
  • Gradient Boosting: Each subsequent model tries to minimize the loss function (usually the residual error) directly through gradient descent. It builds the new learner in the direction that reduces the error of the whole ensemble.
Loading and Preparing the Dataset

Before we dive into coding, let's understand why datasets are crucial. A good dataset allows us to train and test our machine learning models effectively. We'll use the load_digits function from scikit-learn, which provides a real-world dataset for digit classification (0 to 9) from images.

Python
1from sklearn.datasets import load_digits 2 3# Load real dataset 4X, y = load_digits(return_X_y=True)

We need to split this dataset into training and testing sets to evaluate our model properly. Here's how we do it in Python:

Python
1from sklearn.model_selection import train_test_split 2 3# Split dataset 4X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

While this task is usually solved using deep learning, namely Convolutional Neural Networks, we can also approach it using simpler classifiers, including GradientBoostingClassifier and AdaBoostClassifier.

Training and Testing a Gradient Boosting Classifier

Now let's train a Gradient Boosting model using GradientBoostingClassifier from scikit-learn. Here's the basic code to train our model:

Python
1from sklearn.ensemble import GradientBoostingClassifier 2from sklearn.metrics import accuracy_score 3 4# Train a gradient boosting classifier 5gb_clf = GradientBoostingClassifier(n_estimators=100, random_state=42) 6gb_clf.fit(X_train, y_train) 7 8# Evaluate the model 9y_pred = gb_clf.predict(X_test) 10accuracy = accuracy_score(y_pred, y_test) 11print(f"Accuracy on test data: {accuracy:.2f}") # Accuracy on test data: 0.97

In this code:

  • GradientBoostingClassifier is the model we're using.
  • n_estimators=100 means we'll build 100 weak learners (decision trees).
  • random_state=42 ensures reproducibility.
  • fit(X_train, y_train) trains the model on our training data.
  • predict(X_test) generates predictions on the test data.
  • Lastly, we calculate the accuracy using the accuracy_score function.

The n_estimators parameter is crucial because it determines the number of boosting stages, or how many times we refine our model. If set too low, our model might not be accurate enough (underfitting). If set too high, our model might become too complex (overfitting).

Comparing Gradient Boosting to AdaBoost and RandomForest

Let's compare GradientBoosting, AdaBoost, and RandomForest classifiers using the same dataset and parameters:

Python
1from sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier 2 3# Train an AdaBoost classifier 4ada_clf = AdaBoostClassifier(n_estimators=100, random_state=42, algorithm='SAMME') 5ada_clf.fit(X_train, y_train) 6y_pred_ada = ada_clf.predict(X_test) 7accuracy_ada = accuracy_score(y_pred_ada, y_test) 8print(f"Accuracy for AdaBoost on test data: {accuracy_ada:.2f}") # Accuracy for AdaBoost on test data: 0.83 9 10# Train a Random Forest classifier 11rf_clf = RandomForestClassifier(n_estimators=100, random_state=42) 12rf_clf.fit(X_train, y_train) 13y_pred_rf = rf_clf.predict(X_test) 14accuracy_rf = accuracy_score(y_pred_rf, y_test) 15print(f"Accuracy for RandomForest on test data: {accuracy_rf:.2f}") # Accuracy for RandomForest on test data: 0.97

We train all models on the same data and compare their accuracies on the testing set. Main conclusions:

  • The GradientBoostingClassifier outperforms the AdaBoostClassifier on this dataset
  • The GradientBoostingClassifier shows comparable performance to the RandomForestClassifier
  • The GradientBoostingClassifier is a candidate to be the main model for this task
Lesson Summary

Well done! In this lesson, we learned about Gradient Boosting. We covered what it is, how it works, and how to implement it in Python using a real dataset.

To recap:

  • Gradient Boosting builds models sequentially to correct errors from previous models.
  • We used the load_digits dataset to train and test our Gradient Boosting model.
  • The GradientBoostingClassifier from scikit-learn allowed us to easily implement this technique.
  • We compared Gradient Boosting with AdaBoost and RandomForest to see how they perform on the same dataset.

Now that we've covered the theory, it's time for some hands-on practice. You'll apply these concepts to new datasets and fine-tune model parameters to see how Gradient Boosting can improve model performance.

Ready to get started? Let's move to the practice session!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.