Supervised Learning: Ensemble Learning and AdaBoost
Tags: machinelearning, supervisedlearning
Published: 2019-01-27 21:35:27

---
title: "Supervised Learning: Ensemble Learning and AdaBoost"
published: true
description: Better together - how bootstrapping samples of data can work better than the entire dataset, and how to boost it even further, and faster
tags: Machine Learning, Supervised Learning
---

*This is the 6th in a series of class notes as I go through the [Georgia Tech/Udacity Machine Learning course](https://www.udacity.com/course/machine-learning--ud262). The class textbook is [Machine Learning by Tom Mitchell](https://www.cs.ubbcluj.ro/~gabis/ml/ml-books/McGrawHill%20-%20Machine%20Learning%20-Tom%20Mitchell.pdf).*

## Intuition

The purpose of [ensemble learning](https://en.wikipedia.org/wiki/Ensemble_learning) is to learn weights for a bunch of features that have some unknown relationship to whatever you are trying to classify.

For example, classifying email into spam/not spam could use some factors like the mention of Viagra or cryptocurrency or money from Nigeria. Ensemble models take all these little factors and learn the weights in order to spit out the broader result.

This can be helpful if, for example, the fundamental thing we are trying to classify presents in different ways, for example financial spam is different from NSFW spam. In other words, some rules are better for some **subsets** of the data than others. So naturally some rules will work better than others depending on context, however at the end of the day we just want an overall rule to throw out spam. So our ensemble learning in this case would learn something close to OR logic. If the rules have some contingent dependencies, i.e. the mail is spam if Rule 1 and Rule 5 are both active, then the ensemble learning approximates AND logic. And so on.

I've portrayed the rules here as something consciously picked. But to go one level deeper, we should **apply learners to subsets of data** and let it pick the rules.

## Bagging/Bootstrap Aggregation

Picking subsets is actually a bigger assumption than it seems. How we pick and separate subsets is up for debate, and whether we should even pick a subset at all assumes that the data is separable that way.

Picking data uniformly randomly actually works pretty well (see [random forests](https://en.wikipedia.org/wiki/Random_forest) vs the decision trees we have already studied). Taking the standard [UCI Housing Data](https://archive.ics.uci.edu/ml/machine-learning-databases/housing/) example, taking a simple average ensemble of 3rd order polynomials from 5 random subsets of 5 data points, beats a 3rd order polynomial trained over the whole thing. ("beats" here means it does better in the out of sample test/validation set). The intuition here is better generalization/less overfitting by significantly reducing the impact of noise since noise is likely less prevalent in a random subset. This is called **Bagging/Bootstrap Aggregation**.

However, you do tend to lose interrelationships that you might have picked up on otherwise, and the rules from each subset have correspondingly lower statistical power because of the smaller sample size.

## Boosting/AdaBoost

Instead of uniformly randomly picking subsets, we should focus each round of subset-picking on the "hardest" examples. 

Instead of combining multiple rules by simple average, we can use a "weighted" mean.

To define what we mean by "hard" and how we "weight", we need to revisit the definition of error. Typically this is the number of wrong classifications (in the discrete classification use case) or the square of departures from the model (in the continuous regression use case). However, some errors don't happen often, and therefore we don't get much opportunity to train on them. The dominance of some states can lead machine learning to overlook important, rarer states. So if 99% of your email is spam, then a simple spam classifier could just learn to classify ALL your mail as spam and have a pretty good 1% error rate.

So we should, in our ensemble learning, try to progressively focus on the examples that are "harder" to get right as compared to the subsets of data that are well taken care of.

Focusing on the classification case, this means modifying each round's distribution focusing on where the model disagrees with our training data (aka the "hardest examples"). Formally, you can represent like this:

![https://sw-yx.tinytake.com/media/951a43?filename=1548621831322_27-01-2019-10-43-44.png&sub_type=thumbnail_preview&type=attachment&width=509&height=248&&salt=MzI2MTczMl85NzcxNTg3](https://sw-yx.tinytake.com/media/951a43?filename=1548621831322_27-01-2019-10-43-44.png&sub_type=thumbnail_preview&type=attachment&width=509&height=248&&salt=MzI2MTczMl85NzcxNTg3)

Where `y` and `h(x)` represent training data and model, and are either +1 or -1. So multiplying them together with a constant `alpha` term lets us raise (or **boost**) the importance of `Dt` by a positive exponent if they **disagree**, and lower it if they **agree**. For a fuller explanation of the adaptive boosting (AdaBoost) algorithm, [read Robert Schapire's overview paper here.](http://www.cis.upenn.edu/~mkearns/teaching/COLT/schapire.pdf) ([alt link](https://www.cs.princeton.edu/courses/archive/spring07/cos424/papers/boosting-survey.pdf))

The final hypothesis sums up the results of all the models trained so far and just uses the sign of the result.

## Weak Learners

A **weak learner** is a learner that will do better than chance when it tries to learn your hypothesis rules on any data distribution. This means that it will have some level of information gain (as we defined in [our Decision Trees chapter](https://dev.to/swyx/machine-learning-classification-learning--decision-trees-1mbh)), as compared to chance which has no gain. This includes Decision Trees, Perceptrons, and any other classification/regression model you can image. This makes Boosting a meta-machine learning algorithm built on top of existing algorithms.

The emphasis **on any data distribution** is important - it means that your rules need to separate the possible space of states such that it makes it possible for a weak learner to do better than chance *under any distribution*. ([see this answer for more](https://www.youtube.com/watch?time_continue=187&v=lj-IO4uuVR8)) So despite the name, this is a fairly important condition, however it is weak enough for our use.

So boosting relies on weak learners at each step of the way as a hypothesis. They don't need to cleanly slice the dataset in half each time. They just need to do better than random at classifying some part of the dataset - after which, we can further refine and change the distribution to focus on the remaining disagreements. AdaBoost is thus an algorithm for constructing a ”strong” classifier as linear
combination of “simple” “weak” classifiers.

The actual **boosting** - upweighting of disagreements - doesn't actually even have to happen for this method to work, but it does speed up the convergence by disproportionately focusing on disagreements.

We have done a lot of theory here - so please check this visual example to make sure you nail the underlying intuition in boosting and hypothesis forming.

{% youtube u1MXf5N3wYU %}

As you can see here, the power of ensemble learning to compose very simple rules together to map complex spaces is useful. In some way this is analogous to binary search reducing an O(N) problem to an O(log N) problem - by slicing up the dataset progressively each time, we ensure that the final error rate - stuff we cannot classify wrong - is small, and can almost always be improved (more than random) by adding one more layer.

## Boosting rarely Overfits

Overfitting is a constant worry in models (we've discussed many times before) - where training error continues to go down but the test/validation error starts going up. Curiously enough, with Boosting models this doesn't happen. We'll discuss more in the next chapter after our SVM discussion, but Schapire's various talks and papers ([here](http://media.nips.cc/Conferences/2007/Tutorials/Slides/schapire-NIPS-07-tutorial.pdf) and [here](http://rob.schapire.net/papers/explaining-adaboost.pdf)) discuss some theoretical backing for this.

## Next in our series

Schapire has also extended Boosting to the Multi-class case, which has unique challenges, [in this lecture.](https://www.youtube.com/watch?v=L6BlpGnCYVg)

Further notes on this topic [can be found here](https://storage.googleapis.com/supplemental_media/udacityu/367378584/Intro%20to%20Boosting.pdf), with [a specific slide deck on AdaBoost here](https://github.com/pushkar/4641/raw/master/downloads/adaboost_matas.pdf).

Hopefully that was a good introduction to Ensemble Learning and AdaBoost. I am planning more primers and would love your feedback and questions on:


- [Overview](https://dev.to/swyx/machine-learning-an-overview-216n)
- Supervised Learning
    - [Decision Trees](https://dev.to/swyx/machine-learning-classification-learning--decision-trees-1mbh)
    - [Regression](https://dev.to/swyx/supervised-learning-regression-4d17)
    - [Neural Networks](https://dev.to/swyx/supervised-learning-neural-networks-mpo)
    - [Instance Based Learning (K Nearest Neighbors)](https://dev.to/swyx/supervised-learning-instance-based-learning-and-k-nearest-neighbors-kge)
    - [Ensemble Learning (AdaBoost)](https://dev.to/swyx/supervised-learning-ensemble-learning-lim)
    - [Kernel Methods & SVMs](https://dev.to/swyx/supervised-learning-support-vector-machines-3mgk)
    - [Computational Learning Theory](https://dev.to/swyx/supervised-learning-computational-learning-theory-160h)
    - [VC Dimensions](https://dev.to/swyx/supervised-learning-vc-dimensions-10b)
    - [Bayesian Learning](https://dev.to/swyx/supervised-learning-bayesian-learning-403l)
    - [Bayesian Inference](https://dev.to/swyx/supervised-learning-bayesian-inference-4l72)
- Unsupervised Learning
    - [Randomized Optimization](https://dev.to/swyx/unsupervised-learning-randomized-optimization-4c1i)
    - [Information Theory](https://dev.to/swyx/unsupervised-learning-information-theory-recap-4iem)
    - Clustering - week of Feb 25
    - Feature Selection - week of Mar 4
    - Feature Transformation - week of Mar 11
- Reinforcement Learning
    - Markov Decision Processes - week of Mar 25
    - "True" RL - week of Apr 1
    - Game Theory - week of Apr 15