No history yet

Ensemble Methods

The Wisdom of Crowds

When facing a tough decision, you might ask several friends for their opinion instead of relying on just one. The combined wisdom of the group is often more reliable than a single perspective. Machine learning uses a similar strategy called ensemble methods.

The basic idea is to train multiple models, often called "weak learners," and combine their predictions to create a single, powerful "strong learner." This approach doesn't just improve accuracy; it also makes the final model more robust and less prone to making mistakes on new, unseen data. By blending the outputs of different models, we can effectively cancel out their individual errors, reducing both bias and variance.

Ensemble methods are the maestros of collaboration in supervised learning, orchestrating multiple models to enhance predictive performance and produce robust classifiers.

Simple Combinations

The most straightforward way to combine models is by letting them vote. A Voting Classifier takes predictions from several different models and chooses the outcome that gets the most votes. It's like a jury reaching a verdict; the majority opinion becomes the final decision. This is called "hard voting."

There's also "soft voting," where instead of just a final vote, each model gives a probability for each possible outcome. We then average these probabilities, and the outcome with the highest average probability wins. Soft voting often performs better because it considers how confident each model is in its prediction.

For regression problems, where we're predicting a number instead of a category, a similar technique is Model Averaging. The final prediction is simply the average of the predictions from all the individual models. We can also use a weighted average, giving more influence to models that have proven to be more accurate.

Parallel Training with Bagging

Another approach is to train models in parallel, each on a slightly different version of the data. This is the idea behind Bagging, which is short for Bootstrap Aggregating.

The process works like this: first, we create many random subsets of the original training data. These subsets are created through a process called bootstrapping, which means we sample the data with replacement. As a result, some data points might appear multiple times in one sample, while others might not appear at all.

Next, we train a separate model on each of these subsets. Because each model sees a slightly different dataset, each one learns slightly different patterns. Finally, we aggregate their predictions. For classification, this means taking a majority vote; for regression, we average the results. Bagging is especially effective at reducing a model's variance, making it great for complex models like deep decision trees that are prone to overfitting.

Lesson image

A very popular and powerful implementation of bagging is the Random Forest algorithm. As the name suggests, it's an ensemble of many decision trees.

Random Forests introduce an extra layer of randomness on top of bagging. Not only is each tree trained on a random sample of the data, but at each split point within a tree, only a random subset of features is considered. This forces the trees to be different from one another and prevents a few dominant features from controlling the predictions of every tree. This decorrelation of the trees makes the combined model even more powerful and less likely to overfit.

Sequential Learning with Boosting

Unlike bagging, boosting trains models sequentially. Each new model in the sequence focuses on correcting the mistakes made by the models that came before it. Think of it like studying for an exam. After your first practice test, you focus your next study session on the topics you got wrong. You repeat this process, with each session targeting your remaining weaknesses.

In boosting, the first model is trained on the data, and its errors are identified. The next model is then trained with an emphasis on the data points that the first model misclassified. This process continues, with each successive model paying more attention to the lingering errors. The final prediction is a weighted sum of all the models' predictions, where better-performing models are typically given more weight.

The primary goal of boosting is to reduce bias. By sequentially focusing on errors, the ensemble slowly builds a strong model that captures complex patterns in the data.

Gradient Boosting Machines (GBM) are a popular family of boosting algorithms. Instead of just tweaking the weights of misclassified data points, new models are fit to the residual errors of the previous ensemble. Each new tree literally tries to predict the errors of the models that came before it, gradually refining the overall prediction.

Several highly optimized implementations of gradient boosting have become famous in machine learning competitions and real-world applications:

  • XGBoost (Extreme Gradient Boosting): Known for its speed and performance. It includes regularization to prevent overfitting and handles missing data automatically.

  • LightGBM (Light Gradient Boosting Machine): Even faster than XGBoost. It grows trees leaf-wise rather than level-wise, which often leads to quicker convergence and better accuracy, especially on large datasets.

  • CatBoost (Categorical Boosting): Excels at handling categorical features. It uses a special technique to encode categorical variables that avoids some common pitfalls and often leads to better results without extensive feature engineering.

Advanced Ensembling with Stacking

Stacking takes ensembling a step further. Instead of using a simple function like voting or averaging to combine predictions, stacking uses another machine learning model to figure out the best way to combine them.

The process involves two levels:

  1. Level 0: Several base models are trained on the full training dataset. These can be different types of models (e.g., a Random Forest, a Support Vector Machine, and an XGBoost model).

  2. Level 1: The predictions from these base models are then used as input features to train a new model, called a meta-model or blender. This meta-model learns how to best combine the outputs of the base models. For example, it might learn that the Random Forest is very reliable for certain types of data points, while the XGBoost model is better for others.

Stacking is a powerful technique that can find the optimal combination of models, often leading to state-of-the-art performance.

Ready to test your knowledge? Let's see how well you've grasped these concepts.

Quiz Questions 1/6

What is the primary goal of using ensemble methods in machine learning?

Quiz Questions 2/6

In a Voting Classifier, what is the difference between 'hard voting' and 'soft voting'?

Ensemble methods are a cornerstone of modern machine learning. By combining the strengths of multiple models, they provide a reliable way to boost predictive accuracy and build robust systems.