This project compares the performance of three tree-based machine learning algorithms for predicting heart disease:
- 🌳 Decision Tree
- 🎒 Bagging Classifier
- 🌲 Random Forest
The objective is to demonstrate how ensemble learning, particularly Bagging and Random Forest, improves prediction accuracy and generalization compared to a single Decision Tree.
The models were trained and evaluated using the Heart Failure Prediction Dataset from Kaggle.
- Perform Exploratory Data Analysis (EDA)
- Preprocess medical data for machine learning
- Train a Decision Tree classifier
- Apply Bagging Ensemble Learning
- Train a Random Forest classifier
- Compare model performance using multiple evaluation metrics
- Analyze feature importance
- Evaluate models using Stratified 5-Fold Cross-Validation
Dataset: Heart Failure Prediction Dataset
- Samples: 918
- Features: 11
- Target: HeartDisease
- Age
- Sex
- ChestPainType
- RestingBP
- Cholesterol
- FastingBS
- RestingECG
- MaxHR
- ExerciseAngina
- Oldpeak
- ST_Slope
Target:
- HeartDisease
- 1 → Heart Disease
- 0 → No Heart Disease
- Python
- Pandas
- NumPy
- Matplotlib
- Scikit-learn
- Jupyter Notebook
The project includes:
- Dataset overview
- Missing value analysis
- Duplicate checking
- Target class distribution
- Histograms
- Boxplots
- Correlation heatmap
- Correlation analysis with target variable
The following preprocessing steps were performed:
- One-Hot Encoding
- Train-Test Split (80:20)
- Stratified Sampling
A single Decision Tree was trained as the baseline model.
Bagging combines multiple Decision Trees trained on different bootstrap samples to improve prediction stability and reduce variance.
Random Forest extends Bagging by introducing random feature selection at each split, improving model diversity and reducing overfitting.
| Model | Accuracy | Precision | Recall | F1 Score | ROC-AUC |
|---|---|---|---|---|---|
| Decision Tree | 78.26% | 78.18% | 84.31% | 81.13% | 77.52% |
| Bagging Classifier | 85.33% | 85.71% | 88.24% | 86.96% | 91.32% |
| Random Forest | 87.50% | 88.35% | 89.22% | 88.78% | 93.14% |
The models were evaluated using:
- Accuracy
- Precision
- Recall
- F1 Score
- ROC-AUC
- Confusion Matrix
- ROC Curves
- Feature Importance
- Stratified 5-Fold Cross-Validation
The Random Forest model identified the following features as the most influential:
| Rank | Feature |
|---|---|
| 1 | ST_Slope_Up |
| 2 | MaxHR |
| 3 | ST_Slope_Flat |
| 4 | Cholesterol |
| 5 | Oldpeak |
| 6 | ExerciseAngina |
| 7 | Age |
| 8 | RestingBP |
| 9 | ChestPainType_ATA |
| 10 | Sex_M |
Heart-Disease-Bagging-RandomForest/
│
├── data/
│ └── heart.csv
│
├── notebook/
│ └── heart_disease_bagging_random_forest.ipynb
│
├── images/
│ ├── correlation_heatmap.png
│ ├── roc_curve.png
│ ├── confusion_matrix.png
│ └── feature_importance.png
│
├── results/
│ ├── model_comparison.csv
│ └── feature_importance.csv
│
├── requirements.txt
│
├── README.md
│
└── LICENSE
Load Dataset
│
▼
Data Exploration
│
▼
Exploratory Data Analysis
│
▼
Data Preprocessing
│
▼
One-Hot Encoding
│
▼
Train-Test Split
│
▼
Decision Tree
│
▼
Bagging Classifier
│
▼
Random Forest
│
▼
Model Evaluation
│
▼
Feature Importance
│
▼
Cross Validation
│
▼
Final Comparison
Among the three evaluated algorithms, Random Forest achieved the best overall performance.
Best Model: 🌲 Random Forest
- Accuracy: 87.50%
- Precision: 88.35%
- Recall: 89.22%
- F1 Score: 88.78%
- ROC-AUC: 93.14%
The results demonstrate that ensemble learning significantly improves prediction performance compared to a single Decision Tree.
- Hyperparameter tuning using GridSearchCV
- Streamlit web application
- Model deployment
- Explainability with SHAP
- Testing additional ensemble algorithms (XGBoost, AdaBoost, Gradient Boosting)
Oli Bakala
Software Engineering Graduate | AI & Data Science Enthusiast