There are various kinds of Machine Learning algorithms, and each one of them has unique applications. In this article, we’ll take a look at one of the most popular and useful ML algorithms, the Decision Tree algorithm. We’ve discussed an example of the Decision tree in R to help you get familiar with its usage. Let’s get started.
What is a Decision Tree Algorithm?
A Decision Tree is a kind of supervised machine learning algorithm that has a root node and leaf nodes. Every node represents a feature, and the links between the nodes show the decision. Every leaf represents a result.
Suppose you want to go to the market to buy vegetables. You have two choices: either you go, or you don’t. If you don’t go, you won’t get the vegetables, but if you do, you’ll have to get to the market, which leads to another section of choice. A decision tree works just like this.
Decision Trees Applications
Here are some applications of decision trees:
Businesses can use decision trees to enhance the accuracy of their promotional campaigns by observing the performance of their competitors’ products and services. Decision trees can help in audience segmentation and support businesses in producing better-targeted advertisements that have higher conversion rates.
Retention of Customers:
Companies use decision trees for customer retention through analyzing their behaviors and releasing new offers or products to suit those behaviors. By using decision tree models, companies can figure out the satisfaction levels of their customers as well.
Diagnosis of Diseases and Ailments:
Decision trees can help physicians and medical professionals in identifying patients that are at a higher risk of developing serious ( or preventable) conditions such as diabetes or dementia. The ability of decision trees to narrow down possibilities according to specific variables is quite helpful in such cases.
Detection of Frauds:
Companies can prevent fraud by using decision trees to identify fraudulent behavior beforehand. It can save companies a lot of resources, including time and money.
Advantages and Disadvantages of Decision Trees
Advantages of Decision Tree Algorithm:
The following are the main advantages of using a decision tree in R:
- Understanding the results is easier than other models. You can have the technical team program your decision tree model, so it works faster, and you can apply it to new instances. Its calculations have inclusion tests according to an instance, which is a qualitative or a quantitative model.
- It is non-parametric. The independent variables present in our problem don’t have to follow any specific probability distributions due to this reason. You can have collinear variables. Whether they are discriminating or not, it doesn’t have an impact on your decision tree because it doesn’t have to choose those variables.
- They are capable of working with missing values. CHAID puts all the missing values in a category, which you can merge with another one or keep separate from others.
- Extreme individual values (such as outliers) don’t have much effect on the decision trees. You can isolate them in small nodes so that they don’t affect the entire classification.
- It gives you a great visual representation of a decision-making process. Every branch of a decision tree stands for the factors that can affect your decisions, and you get to see a bigger picture. You can use decision trees to improve communication in your team.
- CART trees can handle all variable types directly, including qualitative, continuous, and discrete variables.
Disadvantages of Decision Tree Algorithm
- It doesn’t analyze all the independent variables simultaneously. Instead, it evaluates them sequentially. Due to this, the tree never revises the division of a node at any level, which can cause bias in the tree’s choices.
- Modifying even a single variable can affect the entire tree if it’s close to the top. There are ways to solve this problem. For example, you can construct the tree on multiple samples and aggregate them according to a mean (or vote); this is called resampling. However, it leads to another set of problems as it reduces the readability of the model by making it more complex. So, through resampling, you can get rid of the best qualities of decision trees. Why is it a problem? Suppose one variable has all the qualities of a particular group, but it also has the quality according to which the tree splits. In this case, the tree would put it in the wrong class just because it has that important quality.
- All the nodes of a specific level in a decision tree depend on the nodes in their previous levels. In other words, how you define the nodes on level ‘n +1’ depends entirely on your definition for the nodes on the level ‘n.’ If your definition at level ‘n’ is wrong, all the subsequent levels and the nodes present in those levels would also be wrong.
Decision Tree in R (Example)
You’ll need rpart to build a decision tree in R. We use rpart for classification. In R, you build a decision tree on the basis of a recursive partitioning algorithm that generates a decision, and along with it, regression trees. It has two steps:
- First, it’ll identify a variable that splits the data into two separate groups in the best way possible.
- Second, it’ll repeat the process in the previous step on every subgroup until those groups reach a particular size or if it can’t make improvements in those subgroups anymore.
We have the following data as an example:
In the above data, you have the time and acceleration of a bike. We have to predict its acceleration according to the time. We’ll do so by doing the following:
Then load the data:
Now, we’ll create a scatter plot:
Once, we’ve done that, and we’ll create the tree:
1mct <- rpart(accel ~ times, data=bike)
Our final step is to plot the graph:
We now have a perfectly working model of the Decision tree in R. You can find more similar tutorials on our blog.
If you’re interested to learn more about decision trees, machine learning, check out IIIT-B & upGrad’s PG Diploma in Machine Learning & AI which is designed for working professionals and offers 450+ hours of rigorous training, 30+ case studies & assignments, IIIT-B Alumni status, 5+ practical hands-on capstone projects & job assistance with top firms.