What is a Decision Tree in Machine Learning?
Tags: computerscience
Published: 2019-06-26 20:38:30

---
title: What is a Decision Tree in Machine Learning?
published: true
tags: computerscience
canonical_url: https://hackernoon.com/what-is-a-decision-tree-in-machine-learning-15ce51dc445d
---

![](https://cdn-images-1.medium.com/max/1024/1*ofMfrqFMfpaTH_RMvI_RQA.jpeg)

Decision trees, one of the simplest and yet most useful Machine Learning structures. Decision trees, as the name implies, are [trees](https://medium.com/brandons-computer-science-notes/trees-the-data-structure-e3cb5aabfee9)of decisions.

**An updated version of this article can be found here:**

[What is a Decision Tree in Machine Learning?](https://skerritt.blog/what-is-a-decision-tree-in-machine-learning/)

![Image result for decision tree](https://cdn-images-1.medium.com/proxy/0*Yclq0kqMAwCQcIV_.jpg)<figcaption>Taken from <a href="https://becominghuman.ai/understanding-decision-trees-43032111380f">here</a></figcaption>

You have a question, usually a yes or no (binary; 2 options) question with two branches (yes and no) leading out of the tree. You can get more options than 2, but for this article, we’re only using 2 options.

Trees are weird in computer science. Instead of growing from a root upwards, they grow downwards. Think of it as an upside down tree.

The top-most item, in this example, “Am I hungry?” is called the **_root_**. It’s where everything starts from. **_Branches_** are what we call each line. A **_leaf_** is everything that isn’t the root or a branch.

Trees are important in machine learning as not only do they let us visualise an algorithm, but they are a type of machine learning. Take this algorithm as an example.

![](https://cdn-images-1.medium.com/max/360/0*LFepwBvXOWkxezDX)<figcaption>Taken from <a href="https://www.wikiwand.com/en/Decision_tree_learning">here</a></figcaption>

This algorithm predicts the probability that a passenger will survive on the Titanic.

“_sibsp_” is the number of spouses or siblings aboard the ship. The figures under each leaf show the probability of survival.

With machine learning trees, the bold text is a condition. It’s not data, it’s a question. The branches are still called branches. The leaves are “ **_decisions_** ”. The tree has decided whether someone would have survived or died.

This type of tree is a **classification** tree. I talk more about classification [here](https://hackernoon.com/absolute-fundamentals-of-machine-learning-dca5deee78df). In short; we want to classify each person on the ship as more likely to die or to have survived.

In real life, decision trees aren’t always as easy. Take a look at this photo, and brace yourself. I’ll try to describe as much of it as I can.

![](https://cdn-images-1.medium.com/max/1024/1*e_2uJcDN5hKQM9V2InylVA.png)<figcaption>😴 — Me when I have to look at this image</figcaption>

This is a decision tree. It wants to answer the question “can I eat this mushroom?”

![](https://cdn-images-1.medium.com/max/640/0*eYlru012PubUH5qD.JPG)<figcaption>Taken from <a href="https://www.wikiwand.com/en/Mushroom">here</a></figcaption>

Given an image of a mushroom (like on the left) we want to find out if it’s edible.

You see those things at the top which look like variable assignments? Those are if statements. Let’s take a look at one of those.

_“odor = a: e (400.0)”_

If the smell (_odor_) of the mushroom is “_a_” for almond, then it is edible (_e_) and we are _400.0_ points confident that it is edible. Each of these statements is a  **feature**.

Features are just attributes of an object. The features of a bike are: it has wheels, it has handlebars etc.

We do this on and on until we reach a point where the odor is neutral (_n_) at which point we start to check more **features** of the mushroom.

### Making decision trees using a formal language

Okay, We can draw them but how do we write decision trees? There’s a nice notation for that.

Let’s jump right into an example.

![](https://cdn-images-1.medium.com/max/635/1*eyHyrMqFuaJCEbFiuzGIdg.png)<figcaption>Sorry for the blurry formula. It’s a problem with screen shotting LaTeX 😢</figcaption>

The fancy little “ **_^_** ” means “_and_”. It’s some fancy mathematical notation. For more notation like this, check out this [other article](https://medium.com/brandons-computer-science-notes/mathematical-logic-f53f9c60d8d9) I wrote. In this notation, when we don’t see anything connecting 2 items (like _x2_ and _x5_) we assume it is “_and_”. We want a decision tree that returns **True** when both _x2_ and _x5_ are true.

![](https://cdn-images-1.medium.com/max/611/1*TMJZmOcSn53PReeaxfeBGw.png)

Okay, let’s see another one.

![](https://cdn-images-1.medium.com/max/449/1*LtuzvXS1JjJ0Ab8e5KT7GQ.png)

This one features a lot more logic symbols. You might want to check out this [other article](https://medium.com/brandons-computer-science-notes/mathematical-logic-f53f9c60d8d9) I wrote. Okay, the “∨” symbol means “_or_” and the “¬” means “_not_”.

![](https://cdn-images-1.medium.com/max/329/1*DS4VcU4U3SDkUomS5jknmA.png)<figcaption>Notice how the X1 decision becomes True if X1 is <strong>not </strong>true. This is because of the “not” symbol before it in the formal notation.</figcaption>

### Splitting candidates in the tree

Decision trees are made by taking data from the root node and splitting the data into parts.

![](https://cdn-images-1.medium.com/max/360/0*LFepwBvXOWkxezDX)<figcaption>Taken from <a href="https://www.wikiwand.com/en/Decision_tree_learning">here</a></figcaption>

Taking the Titanic example from earlier, we split the data so that it makes the most sense and is in alignment with the data we have.

One of the problems with decision trees is the question “_what is the best way to split the data?_” Sometimes you’ll instinctively know, other times you’ll need an algorithm

We want to design a function which when given a dataset will split the data accordingly.

If we have numerical features we can split it based on the data we see. There are many different ways of splitting. We can sort all the values in the dataset and decide the split thresholds between instances of different classes. We can also cut them straight down the middle. There are too many splitting algorithms to discuss here. So instead we’ll go through a simple algorithm.

```
(1, a), (2, b), (1, c), (0, b), (3, b)
```

So we have 3 classes (a, b, c). The first thing we do is put them into different categories.

```
{(0, b)}, {(1, a), (1, c)}, {(2, b)}, {(3, b)}
```

Now we have 4 different _sets_. For more on set theory, [click here](https://medium.com/brandons-computer-science-notes/a-primer-on-set-theory-746cd0b13d13).

Let’s just pick some arbitrary numbers here. We’ll split them like so:

```
Split 1 \<= 0.5
Split 2 \<= 1.5 but \> 0.5
Split 3 \> 1.5
```

![](https://cdn-images-1.medium.com/max/750/1*6imRsG1-PADkjrYaYtzBgg.png)

We now have a decision tree split up. If we didn’t split the data up, the tree wouldn’t look much like a tree. Imagine what the tree might look like if our split was “_all data less than 3_”. Everything would be there! It wouldn’t be very tree-like.

### Occam's razor

![](https://cdn-images-1.medium.com/max/638/0*hmAFTUepd_9u3P4x.jpeg)<figcaption>Image of William of Ockham, from <a href="https://www.wikiwand.com/en/William_of_Ockham">here</a>.</figcaption>

Occam's razor is a philosophy attributed to William of Ockham in the 14th century. In short, the quote is:

> “When you have two competing theories that make exactly the same predictions, the simpler one is the better one.”

We can use this principle in machine learning, especially when deciding when to split up decision trees.

> “The simplest tree that classifies the training instances accurcately will work well on previously unseen instances.”

The simplest tree will often be the best tree, so long as all other possible trees make the same results.

### Finding the best splits
<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FE0Z06zqqlm7jSG6hV2%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Frupaulsdragrace-episode-10-rupauls-drag-race-E0Z06zqqlm7jSG6hV2&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FE0Z06zqqlm7jSG6hV2%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/c64b915b31c5127612c47df30f48761b/href">https://medium.com/media/c64b915b31c5127612c47df30f48761b/href</a></iframe>

Trying to find and return the smallest possible decision tree that accurately classifies the training set is very very hard. In fact, it’s an [NP-hard](https://www.wikiwand.com/en/NP-hardness) problem.

Instead, we’ll try to approximate the best result instead of getting the best result. We’re going to talk a lot about probability and statistics, if you want to know more about probability and statistics [click here](https://medium.com/brandons-computer-science-notes/an-introduction-to-probability-statistics-3f5630824411).

What we want is information that explicitly splits the data into two. We don’t want something that can include both male and females, we want purity. One singular class for each split.

This measure of purity is called information. It represents the expected amount of information that would be needed to specify whether a new instance should be classified as the left or right split.

To find the best splits, we must first learn a few interesting things.

### Expected Value

This part talks about random variables. For more on random variables, check out [this article](https://medium.com/brandons-computer-science-notes/an-introduction-to-probability-statistics-3f5630824411) on statistics & probability I wrote.

The expected value is exactly what it sounds like, what do you expect the value to be? You can use this to work out the average score of a dice roll over 6 rolls, or anything relating to probability where it has a value property.

Suppose we’re counting types of bikes, and we have 4 bikes. We assign a code to each bike like so:

![](https://cdn-images-1.medium.com/max/391/1*abbFixm6lr_ENDq5OuVFsw.png)

For every bike, we give it a number. For every coding, we can see we use 2 bits. Either 0 or 1. For the expected value, not only do we need the value for the variable but the probability. Each bike has equal probability. So each bike has a 25% chance of appearing.

Calculating the expected value we multiply the probability by 2 bits, which gets us:

![](https://cdn-images-1.medium.com/max/624/1*COX5CcPoUAawmFXqd_27WQ.png)

What if the probability wasn’t equal?

![](https://cdn-images-1.medium.com/max/856/1*2BN_KfFV2guDUfs89gBSLA.png)

What we need to do is to multiply the number of bits by the probability

![](https://cdn-images-1.medium.com/max/1024/1*wHvOPbsGOQYbRuMh-A_Ybg.png)

### Entropy

This measure of _purity_ is called the [**information**](https://en.wikipedia.org/wiki/Information_theory). It represents the [expected](https://en.wikipedia.org/wiki/Expected_value) amount of [information](https://en.wikipedia.org/wiki/Self-information) that would be needed to specify whether a new instance (first-name) should be classified as male or female, given the example that reached the node. We calculate it based on the number of male and female classes at the node.

Remember earlier when we talked about purity? Entropy is a measure of impurity. It’s how uncertain something is. The formula for entropy is:

![](https://cdn-images-1.medium.com/max/630/1*c_3RiTHigg36ry1XOTiQwg.png)

Entropy is trying to give a number to how uncertain something is.

You can also have conditional entropy, which looks like this:

![](https://cdn-images-1.medium.com/max/548/1*GJo4JB5Jv8i5OWKct9OJZA.png)

#### Information Gain Example

Let’s show this using an example.

![](https://cdn-images-1.medium.com/max/922/1*uMkQDAal44EIn2257ypZUw.png)

What’s the information gain of splitting on Humidity?

![](https://cdn-images-1.medium.com/max/374/1*kppGR115DWCB7CGCNG2k0A.png)<figcaption>An example of splitting on humidity</figcaption>

We have 9+ and 5-. What does that mean? That means in the table we have 9 features where data is positive and 5 where it’s no. So go down the PlayTennis table and count 9 times for positive (Yes) and 5 times for negative (No).

Now we want to find out the information gain of humidity. If humidity is high, we look at the data and count how many yes’s for humidity high. So when humidity is high, we have 3+ and 4-. 3 positives and 4 negatives.

![](https://cdn-images-1.medium.com/max/864/1*FP039Ozp4LvQISAfgF1Rmw.png)<figcaption>D indicates the specific sample, D.</figcaption>

![](https://cdn-images-1.medium.com/max/922/1*uMkQDAal44EIn2257ypZUw.png)

The information gain is the gap between uncertainty. We have 14 sets of data in total, The denominator is always 14. Now we just calculate them using the formula. The information gain of playing tennis (yes) when the humidity is high is:

![](https://cdn-images-1.medium.com/max/988/1*7exXE5m7ACI5wWMGv4OKug.png)<figcaption>3 yes’s and 4 no’s</figcaption>

And the information gain of playing tennis when the humidity is normal is:

![](https://cdn-images-1.medium.com/max/993/1*0SACBDQmRdlC64HO8wYjAg.png)<figcaption>6 yes’s and 1 no.</figcaption>

This isn’t how likely something is to happen, it’s just how much information we gain from this. We use information gain when we want to split something. In the below example we want to find out whether it is better to split on humidity or wind.

![](https://cdn-images-1.medium.com/max/865/1*j3b8QX69jtM7fTfsniP1ow.png)

Now we know what the information gain on each split is using entropy, we apply the information gain formula.

![](https://cdn-images-1.medium.com/max/589/1*f6WKkGsiJVULpmGWmRTTmQ.png)

The information gain on splitting by humidity amongst our sample, D, is 0.151.

If we use the same formula for entropy in the wind part, we get these results:

![](https://cdn-images-1.medium.com/max/472/1*rNuDYRWkgn5QbKD27rDDLQ.png)

And if we put them into the information gain formula we get:

![](https://cdn-images-1.medium.com/max/524/1*fs8BNCd10a-3d90E8AiYuQ.png)

It is better to split on humidity rather than wind as humidity has a higher information gain.

### Definition of accuracy

What we want to do is to check how accurate a machine learning model is.

![](https://cdn-images-1.medium.com/max/859/1*nQGqgAMF-PfSxHchOoTIWQ.png)

M(x) means given a sample, X, we give the predicted classification. The label. lx is actually the true label. So this sample has already been labeled so we know the true label. This set of samples shows that these are correctly labeled.

What we do is feed the algorithm a sample set where we already know the classification of every single item in that sample set. We then measure how many times the machine learning algorithm was right.

### Overfitting with noisy data

Look at the below example. We have this formula and noisy data.

![](https://cdn-images-1.medium.com/max/822/1*YfenrQToBs03KizsRUDm4A.png)

Noisy data means that the data isn’t correct. Our formula is X1 and X2 = True. Our noisy data is True and False = True, which is wrong.

The x3, x4, x5 are all additional features. We don’t care about them, but this is just an example to show that sometimes we have many additional features in a machine learning model which we don’t care about.

We build a decision tree that can match the training data perfectly.

![](https://cdn-images-1.medium.com/max/384/1*X7wC32mD2NEODDl0ME2jzw.png)

The accuracy is

![](https://cdn-images-1.medium.com/max/525/1*BIzo-gvZI24GTCCCj0sT1w.png)

The problem is that it matches the training data perfectly, 100% but because of the noisy data it doesn’t perform very well on the true data. That one small error makes a larger decision tree and causes it to not perform as well in the real world.

If we build a decision tree that works well with the true data, we’ll get this:

![](https://cdn-images-1.medium.com/max/1024/1*QrPJtfJ1LIJttuCa42itWg.png)

Even though it performs worse in the training set, due to not worrying about noisy data it performs perfectly with real-world data.

Let’s see another example of overfitting.

### Overfitting with noise-free data

![](https://cdn-images-1.medium.com/max/912/1*_CtD1GdnMkecKDPLe6_LEQ.png)

Here are the probabilities for each one:

![](https://cdn-images-1.medium.com/max/307/1*n-B1Cz6fO01G7zbVg2X9vw.png)

![](https://cdn-images-1.medium.com/max/289/1*1AylUEp77wSf-Y_ohZqskw.png)

There’s a 50% chance that the resultant, _x3_, is True. There’s a 0.66% chance that the resultant, _Y_, is True.

For our first model let’s have a quick look.

![](https://cdn-images-1.medium.com/max/379/1*Vs0Dbag9ScC1uYApUqdFBw.png)

The accuracy is:

![](https://cdn-images-1.medium.com/max/548/1*c4hLm7KbonQorvCDSweTUg.png)

It’s good on training data, but on real world data (D\_true) it doesn’t perform as well. From this, we can tell that overfitting has occurred.

### Preventing overfitting

The reason for overfitting is because the training model is trying to fit as well as possible over the training data, even if there is noise within the data. The first suggestion is to try and reduce noise in your data.

Another possibility is that there is no noise, but the training data is small resulting in a difference from the true sample. More data would work.

It’s hard to give an exact idea of how to prevent overfitting as it differs from model to model.

#### Did you like this article? Connect with me on Social Media to discuss all things computer science related 😁

[Twitter](https://twitter.com/brandon_skerrit)| [Instagram](http://instagram.com/brandon.codes)| [LinkedIn](https://www.linkedin.com/in/brandonls/)

Don’t forget to click that 👏clap👏 button to show your appreciation!

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F885736-2%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F885736-2%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/e77f714c4eed7299da2734e0c415149d/href">https://medium.com/media/e77f714c4eed7299da2734e0c415149d/href</a></iframe>

**I didn’t get paid for writing this article. If you want to support me or enjoyed this article, feel free to buy me a cup of tea or something below 😍✨**

- [Pay Brandon Skerritt using PayPal.Me](https://www.paypal.me/brandonskerritt)
- [Pay Brandon instantly through Monzo.me](https://monzo.me/brandonskerritt)

* * *