DEV Community

Cover image for How Decision Trees Work: Real Demo and Simple Explanation
Thabasvini
Thabasvini

Posted on

How Decision Trees Work: Real Demo and Simple Explanation

Do you ever ask yourself how machines make decisions like humans?

Let’s break down one of the most beginner-friendly algorithms in machine learning,

Wanna know what’s that, ta-da “Decision Tress”, with a real working demo using Python and scikit-learn.

What Is a Decision Tree?

A decision tree is a flowchart-like structure where each internal node represents a question based on a feature, each branch represents an answer, and each leaf node represents an outcome or class label.

(Deos the definition overwhelm you?)

Here is the simplified version,

*Machines ask yes/no questions ----> (just like we do)----> to make decisions. *

Real Life Analogy

Decision Tree Flowchart Example: Should I Go Out

That’s a decision tree for you to visualize.

The Sample Use Case

Table showing sample data with columns: Age, Income, Will Buy

Let's Build It in Python

Import Libraries

from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt

Prepare the Data

X = [[25, 50000], [35, 60000], [45, 80000], [22, 30000]]
y = [0, 1, 1, 0]  # 0 = No Buy, 1 = Buy

Train the Model

clf = DecisionTreeClassifier()
clf.fit(X, y)

Visualize the Tree

plt.figure(figsize=(8, 5))
plot_tree(clf, feature_names=["Age", "Income"], class_names=["No", "Yes"], filled=True)
plt.show()

Enter fullscreen mode Exit fullscreen mode

Output:

Decision Tree Output Visual

How Does It Decide?

Well, thats the cool one here,the decision tree uses something called Gini Impurity to choose the best split,

  1. If a node has all “Yes” or all “No”, it’s pure (Gini = 0).

  2. The more mixed it is, the higher the impurity.

So, the takeaway algorithm keeps splitting where impurity is lowest.

Try making a prediction using the above code!

print(clf.predict([[40, 70000]]))
Enter fullscreen mode Exit fullscreen mode

The output will be [1] //means they will buy.

Where Are Decision Trees Used?

  • Credit card approval

  • Customer churn prediction

  • Recommender systems

  • Medical diagnosis

  • E-commerce decision engines

okay so finally, Decision Trees are powerful, visual, and easy to understand.

If you’re learning ML or just want to understand how AI “thinks”, start with Decision Trees.

And if you want to see a live demo in action,Here's a quick youtube video me explaining How decision tress actually work,

🙌 Follow me for more beginner-friendly AI & ML breakdowns!
🚀 Need help building real AI/ML solutions? Check out SiteEncoders, we build it for you.

Top comments (1)

Collapse
 
zainab_imran_05f3b5d6877e profile image
Zainab Imran

very helpful