Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconArtificial Intelligencebreadcumb forward arrow iconHow to Create Perfect Decision Tree | Decision Tree Algorithm [With Examples]

How to Create Perfect Decision Tree | Decision Tree Algorithm [With Examples]

Last updated:
30th Mar, 2020
Views
Read Time
5 Mins
share image icon
In this article
Chevron in toc
View All
How to Create Perfect Decision Tree | Decision Tree Algorithm [With Examples]

If you were wondering ‘how to create a decision tree’ or ‘can I create a decision tree in Java,’ you’ve come to the right place. In this article, we’ll find answers to such questions as we’ll be discussing decision trees in detail. You’ll find out what they are, why they are so popular, and how you can create one of them. 

Before you create a decision tree, you must be familiar with several other topics such as Linear Regression and algorithms. 

Top Machine Learning and AI Courses Online

What is a Decision Tree?

A decision tree gives you a map of all the possible outcomes of particular selections. It can help you plan out the future actions under different scenarios according to different choices. You can compare those possible outcomes on the basis of their probabilities and costs.

Ads of upGrad blog

As the name suggests, a decision tree shows a graph resembling a tree. It is a model of decisions, along with the outcomes and consequences of every one of them. Its ultimate goal is to help you perform classification correctly while going through the lowest number of choices possible. 

Trending Machine Learning Skills

Enrol for the Machine Learning Course from the World’s top Universities. Earn Masters, Executive PGP, or Advanced Certificate Programs to fast-track your career.

You can represent boolean functions by using decision trees as well. Each leaf node of a decision tree is a class label, and the internal nodes of the tree show the attributes. They begin with one node and then branch off into all the possibilities. Every one of those branches leads to more nodes that represent other possible consequences. You can create a decision tree in Java.  

A decision tree has various kinds of nodes:

  • Decision Nodes
  • Chance Nodes
  • End Nodes

The end nodes reflect the final result of a decision path while the chance nodes show the chances of particular outcomes. The decision nodes indicate the decision you’ll make that would lead to the possible results. You can use decision trees to map out algorithmic predictions as well as to make informal decisions. 

Now that you’re familiar with what a decision tree is, we should focus on digging a little deeper and understand why it’s prevalent. Let’s dive in. 

Applications of Decision Tree

Here are some applications of decision trees so you can see how prevalent they are:

  • Banks use them to classify their loan applications
  • Finance professionals use decision trees for option pricing
  • Categorizing exam papers according to the level of expertise of the candidates
  • Choosing whether to accept or reject a job offer
  • Making essential business decisions such as whether a company should modify its product or not.

You must’ve used decision trees yourself in making various choices in your life. Just come up with a few scenarios where you had to make an intricate decision. 

Advantages of Decision Tree

There are many advantages to using a decision tree. Here are they:

  • Decision trees produce rules that you can understand easily. You wouldn’t have difficulty conveying those rules to other systems.
  • They can handle both categorical as well as continuous variables
  • A decision tree will give you a simple indication of the importance of every field. You can easily make predictions (or classifications) according to the same.
  • Decision trees also perform feature selection implicitly that helps you with data exploration. 

Learn more: Artificial Intelligence Algorithms

Disadvantages of Decision Tree

Everything has its flaws, and decision trees are no exception. Here are some problems with using them:

  • Decision trees aren’t useful for performing estimation tasks. That’s because such jobs require the prediction of a continuous attribute’s value, and decision trees aren’t good at that.
  • Computationally, decision trees are more expensive than other options. It’ll cost you a lot to train a decision tree model as well in comparison to others. The pruning algorithms you’d use in making decision trees are also quite expensive as they require to build many sub-trees. 
  • If you have a high number of classes examples but a low number of training examples, your decision trees wouldn’t be much accurate, and their chances of containing errors would be significantly high. 

How to Create a Decision Tree

Let’s create a decision tree on whether a person would buy a computer or not. In this case, we’d have two classes, ‘Yes’ and ‘No.’ The first class refers to the people who would buy a computer, while the second refers to those who wouldn’t. First, we’ll calculate Information Gain and Entropy for these classes. 

Once we’ve calculated the Entropy of these classes, we’ll focus on information gain. We can classify the values of Entropy like this:

If Entropy is 0, it means the data is pure (homogenous)

If Entropy is 1, it means the data is impure (half-divided)

Let’s suppose our Entropy is impure. Then we’ll split the information gain on age. This way, our data will show how many people of a specific age bracket will buy this product and how many won’t. We can calculate the information gain for multiple attributes. But in our example, we found that the information gain is highest for ‘Age’ and the lowest for ‘Income.’ So, we’ll go with that. 

Here are the classification rules for this decision tree:

If someone’s age is less than 30 and if that person isn’t a student, they won’t buy the product so:

Age (<30) ^ student(no) = NO

But if someone whose age is less than 30 and is a student, they would buy the product:

Age (<30)^ student(yes) = YES

Know more about: Top 9 Machine Learning Libraries You Should Know About

Now, if their age lies between 31 and 40, they would surely buy the product:

Age(31…40) = YES

A person with age higher than 40 and a high credit rating wouldn’t buy:

Age(>40)^ credit_rating(high) = NO

On the other hand, if a person who is older than 40 but has an average credit rating, he or she would buy the product:

Age(>40)^ credit_rating(normal) = YES

By following these steps, you’d be able to create the perfect decision tree without any difficulty

Ads of upGrad blog

Popular AI and ML Blogs & Free Courses

Conclusion

Now you must know how to create a decision tree. You can learn a whole lot more about decision trees and the relevant algorithms in our machine learning course. We’re sure you’d get to enhance your knowledge there as you’ll get to learn how you can create a decision tree in Java, how you can use them in real-life, and more. 

If you’re interested to learn more about 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.

Profile

Kechit Goyal

Blog Author
Experienced Developer, Team Player and a Leader with a demonstrated history of working in startups. Strong engineering professional with a Bachelor of Technology (BTech) focused in Computer Science from Indian Institute of Technology, Delhi.
Get Free Consultation

Selectcaret down icon
Select Area of interestcaret down icon
Select Work Experiencecaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Popular Machine Learning Course

Explore Free Courses

Suggested Blogs

45+ Best Machine Learning Project Ideas For Beginners [2024]
330074
Summary: In this Article, you will learn Stock Prices Predictor Sports Predictor Develop A Sentiment Analyzer Enhance Healthcare Prepare ML Algorith
Read More

by Jaideep Khare

21 May 2024

Top 15 IoT Interview Questions &#038; Answers 2024 – For Beginners &#038; Experienced
64874
These days, the minute you indulge in any technology-oriented discussion, interview questions on cloud computing come up in some form or the other. Th
Read More

by Kechit Goyal

19 May 2024

40 Best IoT Project Ideas &#038; Topics For Beginners 2024 [Latest]
765879
In this article, you will learn the 40Exciting IoT Project Ideas & Topics. Take a glimpse at the project ideas listed below. Best Simple IoT Proje
Read More

by Kechit Goyal

19 May 2024

Top 22 Artificial Intelligence Project Ideas &#038; Topics for Beginners [2024]
415605
In this article, you will learn the 22 AI project ideas & Topics. Take a glimpse below. Best AI Project Ideas & Topics Predict Housing Price
Read More

by Pavan Vadapalli

18 May 2024

Image Segmentation Techniques [Step By Step Implementation]
64093
What do you see first when you look at your selfie? Your face, right? You can spot your face because your brain is capable of identifying your face an
Read More

by Pavan Vadapalli

16 May 2024

6 Types of Regression Models in Machine Learning You Should Know About
283595
Introduction Linear regression and logistic regression are two types of regression analysis techniques that are used to solve the regression problem
Read More

by Pavan Vadapalli

16 May 2024

How to Make a Chatbot in Python Step By Step [With Source Code]
31514
Creating a chatbot in Python is an essential skill for modern developers looking to enhance user interaction and automate responses within application
Read More

by Kechit Goyal

13 May 2024

Artificial Intelligence course fees
5841
Artificial intelligence (AI) was one of the most used words in 2023, which emphasizes how important and widespread this technology has become. If you
Read More

by venkatesh Rajanala

29 Feb 2024

Artificial Intelligence in Banking 2024: Examples &#038; Challenges
6724
Introduction Millennials and their changing preferences have led to a wide-scale disruption of daily processes in many industries and a simultaneous g
Read More

by Pavan Vadapalli

27 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon