Decision Trees in Machine Learning


Introduction
Decision Trees are one of the most intuitive and interpretable algorithms in Supervised Learning. They are used for both Regression and Classification tasks and form the building blocks of powerful ensemble methods like Random Forests and Gradient Boosting.
1. What is a Decision Tree?
A Decision Tree is a tree-like model of decisions and their possible consequences. It breaks down a dataset into smaller subsets while incrementally developing an associated decision tree. The result is a tree with decision nodes and leaf nodes.
1.1 Decision Nodes and Leaf Nodes:
Decision Nodes: Internal nodes that test a feature.
Leaf Nodes: Terminal nodes that provide the final output (class label or value).
1.2 Why Use Decision Trees?
Easy to understand and interpret.
Can handle both numerical and categorical data.
Requires minimal data preparation (no need for feature scaling).
1.3 Example Use Cases:
Medical Diagnosis: Classifying patients as high or low risk.
Credit Risk Analysis: Predicting loan defaults.
Customer Segmentation: Grouping customers based on buying behavior.
2. How Does a Decision Tree Work?
Decision tree uses the tree representation to solve the problem in which each leaf node corresponds to a class label and attributes are represented on the internal node of the tree. We can represent any boolean function on discrete attributes using the decision tree.
Decision Trees recursively split the dataset based on the feature that results in the highest information gain or Gini reduction.
2.1 Splitting Criteria:
Classification: Gini Index or Entropy is used to evaluate splits.
Regression: Mean Squared Error (MSE) or Mean Absolute Error (MAE) is used.
2.2 Recursive Partitioning:
The tree is grown by splitting the data at each node.
Splits are chosen to maximize the reduction in impurity.
This process continues until one of the stopping criteria is met:
Maximum depth is reached.
Minimum samples per leaf node is satisfied.
No further information gain is achieved.
2.3 Decision Path:
A data point follows a path from the root node to a leaf node based on the feature values, leading to a predicted output.
3. Types of Decision Trees
Classification Tree: Used when the target variable is categorical.
Regression Tree: Used when the target variable is continuous.
4. Mathematical Background
4.1 Impurity Measures:
To decide the best feature to split on, Decision Trees use impurity measures:
4.1.1 Gini Index (for Classification):
Gini Index is a metric to measure how often a randomly chosen element would be incorrectly identified. It means an attribute with a lower Gini index should be preferred.
C = Number of classes
pi = Proportion of samples belonging to class i
4.1.2 Entropy (for Classification):
Entropy is the measure of uncertainty of a random variable, it characterizes the impurity of an arbitrary collection of examples. The higher the entropy more the information content.
4.2 Information Gain:
Information Gain tells us how useful a question (or feature) is for splitting data into groups. It measures how much the uncertainty decreases after the split. A good question will create clearer groups, and the feature with the highest Information Gain is chosen to make the decision.
Information Gain is the reduction in impurity achieved by a split:
Nk = Number of samples in the kth child node
N = Total number of samples in the parent node
5. Advantages and Disadvantages
5.1 Advantages:
Easy to visualize and interpret.
Handles both numerical and categorical data.
Requires little data preprocessing (no feature scaling or normalization).
Can model non-linear relationships.
5.2 Disadvantages:
Overfitting: Decision Trees can easily overfit, especially on noisy data.
Instability: A small change in data can lead to a completely different tree.
Bias towards dominant classes: Unbalanced datasets may cause biased trees.
Lack of smoothness: Decision boundaries are axis-aligned and not smooth.
6. Pruning in Decision Trees
Pruning is used to avoid overfitting by reducing the size of the tree:
Pre-Pruning: Stops the tree growth early (e.g., setting max depth or minimum samples per leaf).
Post-Pruning: Grows the tree fully and then removes nodes that do not provide additional information.
7. Implementation in Python
Here’s how to implement a Decision Tree using Scikit-learn:
# Importing libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn import tree
import matplotlib.pyplot as plt
# Load Dataset
data = pd.read_csv('/path/to/the/dataset') # Use any suitable dataset
data = pd.get_dummies(data, columns=['Gender'], drop_first=True)
X = data.iloc[:, :-1].values # Features
y = data.iloc[:, -1].values # Target Variable
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create Decision Tree model
model = DecisionTreeClassifier(criterion='gini', max_depth=3, random_state=42)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate Model
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
# Visualize Decision Tree
plt.figure(figsize=(15,10))
tree.plot_tree(model, filled=True, feature_names=data.columns[:-1], class_names=['Class 0', 'Class 1'])
plt.show()
8. Real-world Applications
Healthcare: Diagnosing diseases based on symptoms and test results.
Finance: Credit scoring and risk assessment.
Marketing: Customer segmentation and targeting.
Manufacturing: Quality control and defect prediction.
9. Tips for Better Performance
Pruning: Use pre-pruning (e.g., max depth, min samples per leaf) or post-pruning to avoid overfitting.
Feature Selection: Select the most important features to reduce overfitting and improve interpretability.
Ensemble Methods: Use Decision Trees as base learners in Random Forests or Gradient Boosting for better performance.
Hyperparameter Tuning: Optimize hyperparameters using Grid Search or Random Search.
10. Conclusion
Decision Trees are a powerful and interpretable model suitable for both classification and regression tasks. However, they are prone to overfitting and instability. They are the foundation of powerful ensemble methods like Random Forests and Gradient Boosting, which overcome these limitations.
10.1 Key Takeaways:
Decision Trees are easy to interpret and visualize.
They can handle both numerical and categorical data.
Prone to overfitting and require pruning or ensemble methods for better performance.
Form the basis of powerful models like Random Forests and Gradient Boosting.
Subscribe to my newsletter
Read articles from Tushar Pant directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
