Decision Tree is one of the most powerful and easy-to-understand algorithms in Machine Learning. It is widely used for both classification and regression problems. What makes a Decision Tree special is that it tries to mimic the human way of making decisions — breaking problems into smaller choices and reaching a conclusion step-by-step.
In this complete guide, you will learn everything about the Decision Tree algorithm:
A Decision Tree is a supervised machine learning algorithm used for both classification (categorical output) and regression (continuous output).
It works by continuously splitting the dataset based on feature values, forming a tree-like structure. Each internal node represents a decision, each branch represents an outcome, and each leaf node represents the final prediction.
Suppose you want to decide whether a customer will buy a product. The tree may ask questions like:
The final decision is based on answers to these questions.
A Decision Tree follows a simple but powerful process:
Gini measures how “pure” or “impure” a node is. If all samples belong to the same class, impurity = 0.
Entropy measures randomness in data. Lower entropy = better split.
Measures how much a split reduces entropy. Higher Information Gain = better split.
A regression tree splits data to minimize prediction error for continuous values.
Let’s take a simple classification problem:
Goal: Predict whether a customer will buy a product.
Features:
The Decision Tree may create splits like:
This sequence of decisions leads to the final classification: Buy (1) or Not Buy (0).
👉 The solution to most limitations: Random Forest A combination of multiple Decision Trees (ensemble learning).
from sklearn.tree import DecisionTreeClassifier
# Sample dataset
X = [[25, 50000], [40, 60000], [35, 30000], [20, 20000]]
y = [1, 1, 0, 0] # 1 = will buy, 0 = won’t buy
model = DecisionTreeClassifier()
model.fit(X, y)
print(model.predict([[30, 40000]])) # Predict for new customer
This program trains a Decision Tree using two features (age, income) and predicts whether a new customer will buy the product.
💬 Tap ❤ for more ML algorithms!
At CodeMyFYP, we help students learn Machine Learning, AI, Full-Stack Development, and build Final Year Projects with step-by-step guidance.
🌐 Website: www.codemyfyp.com
📞 Contact: 9483808379
📍 Location: Bengaluru, Karnataka
💼 Industry: IT Services & Consulting
🚀 Learn ML one algorithm at a time with CodeMyFYP!
Keywords: decision tree algorithm • gini impurity • entropy • information gain • classification tree • regression tree • machine learning decision tree • supervised learning algorithm • sklearn DecisionTreeClassifier • ML tutorial for beginners • CodeMyFYP