📖 Introduction
Decision Trees are powerful supervised machine learning algorithms used for both classification and regression tasks. They make predictions by recursively splitting data into smaller subsets based on feature values, creating a tree-like structure of decisions that is easy to interpret and visualize.
Information
🎯 Objectives of Decision Trees
- Classify observations into predefined categories.
- Predict continuous numerical values.
- Discover decision rules from data.
- Model nonlinear relationships.
- Provide interpretable machine learning models.
🌳 Structure of a Decision Tree
| Component | Description |
|---|---|
| Root Node | Starting point containing the complete dataset. |
| Decision Node | Internal node where data is split based on a feature. |
| Branch | Represents the outcome of a decision. |
| Leaf Node | Final node containing the prediction. |
| Subtree | A smaller tree originating from a decision node. |
⚙️ How Decision Trees Work
Collect and preprocess the training dataset.
Select the best feature for splitting the data.
Create child nodes based on the selected split.
Repeat the process recursively for each subset.
Stop when a stopping criterion is satisfied.
Use leaf nodes to make predictions.
🌲 Decision Tree Workflow
📊 Splitting Criteria
Entropy
Entropy measures the amount of uncertainty or disorder in a dataset. Lower entropy indicates purer groups.
Information Gain
Information Gain measures how much uncertainty is reduced after splitting on a feature.
Gini Impurity
Gini Impurity measures the probability of incorrectly classifying a randomly selected sample.
Variance Reduction
Regression trees select splits that maximize the reduction in target variance.
📈 Classification vs Regression Trees
| Aspect | Classification Tree | Regression Tree |
|---|---|---|
| Output | Class Label | Continuous Value |
| Split Criterion | Entropy or Gini | Variance Reduction |
| Prediction | Majority Class | Average Value |
🎛️ Important Hyperparameters
| Hyperparameter | Purpose |
|---|---|
| max_depth | Maximum depth of the tree. |
| min_samples_split | Minimum samples required to split a node. |
| min_samples_leaf | Minimum samples required in a leaf node. |
| criterion | Split quality measure (Gini, Entropy, or Squared Error). |
| max_features | Maximum number of features evaluated at each split. |
✂️ Tree Pruning
Large Decision Trees often memorize training data and become overly complex. Pruning reduces unnecessary branches, improving the model's ability to generalize.
- Limit maximum tree depth.
- Increase minimum samples per split.
- Stop splitting early.
- Build the complete tree.
- Remove branches that do not improve validation performance.
- Produces smaller and more generalizable trees.
📊 Evaluation Metrics
- Accuracy
- Precision
- Recall
- F1-Score
- ROC-AUC
- Mean Absolute Error (MAE)
- Mean Squared Error (MSE)
- Root Mean Squared Error (RMSE)
- R² Score
⚖️ Advantages and Limitations
- Easy to understand and visualize.
- Handles numerical and categorical data.
- Requires little data preprocessing.
- Captures nonlinear relationships.
- Provides feature importance information.
- Prone to overfitting without pruning.
- Small data changes may produce different trees.
- Can become biased toward dominant classes.
- May not perform as well as ensemble methods.
🌍 Real-World Applications
| Application | Purpose |
|---|---|
| 🏦 Loan Approval | Predict loan eligibility. |
| 🏥 Medical Diagnosis | Assist disease classification. |
| 📧 Spam Detection | Classify emails. |
| 🛒 Customer Segmentation | Group customers based on behavior. |
| 🌾 Agriculture | Predict crop diseases and yield. |
| 🏠 House Price Prediction | Estimate property values. |
💻 Practical Example
Decision Tree Classification Using Scikit-learn
from sklearn.tree import DecisionTreeClassifier
import numpy as np
# Sample data
X = np.array([[1], [2], [3], [4], [5], [6]])
y = np.array([0, 0, 0, 1, 1, 1])
# Create Decision Tree model
model = DecisionTreeClassifier(
criterion="gini",
max_depth=3,
random_state=42
)
# Train model
model.fit(X, y)
# Predict
prediction = model.predict([[3.5]])
print("Predicted Class:", prediction[0])⚠️ Common Mistakes
- Allowing the tree to grow too deep without pruning.
- Ignoring class imbalance during training.
- Using a single tree for highly complex datasets where ensemble methods may perform better.
- Not tuning important hyperparameters.
- Evaluating performance only on the training dataset.