Ujwal Watgule
3 min readJul 29, 2023

--

Machine Learning — Decision Tree — Part I

Decision Tree is a popular and intuitive machine learning algorithm used for both classification and regression tasks. It is widely used in various fields due to its simplicity, interpretability, and ability to handle both numerical and categorical data. Decision Trees are considered the building blocks of more advanced ensemble methods like Random Forests and Gradient Boosting Trees.

In Simple Terms: -

Imagine you are playing a guessing game with your friend. Your friend thinks of an animal, and you have to guess which animal it is by asking yes-or-no questions. To help you make the best guess, you try to ask questions that can split the animals into different groups.

For example, you might start by asking, “Is it a mammal?” If your friend says yes, you know that the animal is in the mammal group, and if your friend says no, you know it’s not a mammal, so it must be in a different group.

Next, you might ask, “Does it have wings?” If your friend says yes, you can narrow down the choices to animals with wings. If your friend says no, you know it’s not a animal with wings, so it must be in a different group.

You keep asking these questions, and with each answer, you split the animals into smaller and smaller groups until you finally reach a single animal that matches all the answers. That’s your guess!

A Decision Tree works in a similar way. Instead of guessing animals, it guesses things like whether an email is spam or not, or whether a fruit is an apple or an orange. It does this by asking questions about the features of the thing it’s trying to guess.

For example, if it’s trying to decide whether an email is spam, it might start by asking, “Does the email contain certain words commonly found in spam emails?” If the answer is yes, it knows the email might be spam. If the answer is no, it knows it’s probably not spam.

Then it might ask, “Does the email come from a sender you know?” If the answer is yes, it might be less likely to be spam. If the answer is no, it might be more likely to be spam.

The Decision Tree keeps asking these questions, and at each step, it divides the emails into groups based on the answers. Eventually, it reaches a point where it can confidently say whether the email is spam or not.

So, just like you ask questions to guess an animal, the Decision Tree asks questions about features to make decisions about things like spam emails or different types of fruits. It’s like playing a fun guessing game, but the Decision Tree does it really fast and can help us make smart choices!

Basic Idea:

The decision tree algorithm creates a tree-like model of decisions and their possible consequences. It recursively splits the dataset into subsets based on the most significant attribute (feature) at each step. The process continues until the subsets either contain only one class (in the case of classification) or reach a certain size or depth (in the case of regression).

How it Work? :

Feature Selection: The algorithm starts by selecting the best attribute (feature) from the dataset to make the first decision. The goal is to choose the attribute that best separates the data based on the target variable (for classification) or minimizes the mean squared error (for regression).

Splitting: The selected attribute is used to split the data into subsets (child nodes) based on the attribute’s possible values. Each subset represents a branch of the tree.

Recursive Process: The process of selecting the best attribute and splitting the data is repeated for each child node (subset). This creates a tree-like structure with nodes representing decisions and branches representing the possible outcomes.

Leaf Nodes: The process continues until a stopping criterion is met, such as all data in a node belonging to the same class (for classification) or reaching a specified depth (for regression). The terminal nodes of the tree are called leaf nodes, and they represent the final predicted class (for classification) or the predicted value (for regression).

In next part, we’ll take one real world example for Decision Tree implementation to understand its technical aspects.

References:

1. https://en.wikipedia.org/wiki/Decision_tree_learning

2. https://wiki.pathmind.com/decision-tree

3. https://scikit-learn.org/stable/modules/tree.html

--

--

Ujwal Watgule
0 Followers

IT Professional with interests in Software Development, Machine Learning, Data Science, Software Application Architecture.