- Supervised Learning
-
Overview
- Introduction to Supervised Learning
- Linear Regression and Its Applications
- Logistic Regression for Classification
- Decision Trees and Random Forests
- Support Vector Machines (SVM)
- K-Nearest Neighbors (KNN) Algorithm
- Naïve Bayes Classifier
- Gradient Boosting (XGBoost, LightGBM)
- Overfitting and Underfitting in Models
- Bias-Variance Tradeoff
Decision Trees and Random Forests
Add to BookmarkDecision Trees and Random Forests are powerful supervised learning algorithms used for both classification and regression tasks. They are easy to understand, interpret, and visualize, making them popular choices for real-world problems.
What is a Decision Tree?
A Decision Tree is a flowchart-like tree structure where:
- Each internal node represents a feature (attribute).
- Each branch represents a decision rule.
- Each leaf node represents an output label (class or value).
The tree splits the data recursively based on the feature that provides the best separation using metrics like Gini Index or Information Gain (based on entropy).
Example Use Cases
- Credit scoring
- Medical diagnosis
- Customer segmentation
- Fraud detection
Building a Simple Decision Tree in Python
We’ll classify whether a person will buy a product based on age and income.
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
# Sample dataset
data = {
'Age': [25, 30, 45, 35, 22],
'Income': [50000, 60000, 80000, 120000, 30000],
'Buy': ['No', 'No', 'Yes', 'Yes', 'No']
}
df = pd.DataFrame(data)
# Feature and target
X = df[['Age', 'Income']]
y = df['Buy']
# Create and train model
model = DecisionTreeClassifier()
model.fit(X, y)
# Predict with column names to match training data
new_data = pd.DataFrame([[28, 55000]], columns=['Age', 'Income'])
print(model.predict(new_data))Output-
['No']Visualizing the Tree
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
plot_tree(model, feature_names=['Age', 'Income'], class_names=['No', 'Yes'], filled=True)
plt.show()What is a Random Forest?
A Random Forest is an ensemble of decision trees. It builds multiple decision trees using random subsets of the data and features, then averages their predictions (regression) or uses majority voting (classification).
This reduces overfitting and improves accuracy and generalization.
Python Example: Random Forest Classifier
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
# Create and train Random Forest model
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X, y)
# Prediction with column names to avoid warning
new_data = pd.DataFrame([[28, 55000]], columns=['Age', 'Income'])
print(rf_model.predict(new_data))Output-
['No']Advantages and Disadvantages
| Decision Trees | Random Forests |
|---|---|
| Easy to interpret and visualize | More accurate than single trees |
| Can overfit easily | Resistant to overfitting |
| Fast training on small data | Slower and more resource-intensive |
Evaluation Metrics
Use the same metrics as other classifiers:
- Accuracy
- Precision, Recall, F1 Score
- Confusion Matrix
from sklearn.metrics import accuracy_score, classification_report
y_pred = model.predict(X)
print("Accuracy:", accuracy_score(y, y_pred))
print(classification_report(y, y_pred))Output -
Accuracy: 1.0
precision recall f1-score support
No 1.00 1.00 1.00 3
Yes 1.00 1.00 1.00 2
accuracy 1.00 5
macro avg 1.00 1.00 1.00 5
weighted avg 1.00 1.00 1.00 5Tips for Beginners
- Decision Trees are great for understanding basic concepts of supervised learning.
- Use them for initial modeling and understanding feature importance.
Tips for Professionals
- Tune hyperparameters like
max_depth,min_samples_split, andn_estimatorsto improve performance. - Use
RandomForestClassifierwith feature importance (model.feature_importances_) to rank inputs. - Combine with grid search or cross-validation for better models.
Summary
- Decision Trees are intuitive and easy to use for both regression and classification.
- Random Forests enhance the performance by building multiple trees and averaging predictions.
- Together, they form a robust part of any machine learning toolkit.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Python Challenging Programming Exercises Part 1
- Google’s Core Update in May 2020: What You Need to Know
- AI in Cybersecurity: The Future of Digital Protection
- How to Install Tableau and Power BI on Ubuntu Using VirtualBox
- AI Agents & Autonomous Systems – The Future of Self-Driven Intelligence
- Python Challenging Programming Exercises Part 2
- What is YII? and How to Install it?
- Big Data: The Future of Data-Driven Decision Making
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- Robotics & AI – How AI is Powering Modern Robotics
- Python Challenging Programming Exercises Part 3
- Top 15 Recommended SEO Tools
- Why to learn Digital Marketing?
- Types of Numbers in Python
- Datasets for Natural Language Processing
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset


