Skip to content

About

Heart disease prediction using Decision Tree, Bagging, and Random Forest with ensemble learning and performance comparison.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

❤️ Heart Disease Prediction using Bagging and Random Forest

📌 Project Overview

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.


🎯 Objectives

  • 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

Dataset: Heart Failure Prediction Dataset

  • Samples: 918
  • Features: 11
  • Target: HeartDisease

Features

  • Age
  • Sex
  • ChestPainType
  • RestingBP
  • Cholesterol
  • FastingBS
  • RestingECG
  • MaxHR
  • ExerciseAngina
  • Oldpeak
  • ST_Slope

Target:

  • HeartDisease
    • 1 → Heart Disease
    • 0 → No Heart Disease

🛠 Technologies Used

  • Python
  • Pandas
  • NumPy
  • Matplotlib
  • Scikit-learn
  • Jupyter Notebook

📊 Exploratory Data Analysis

The project includes:

  • Dataset overview
  • Missing value analysis
  • Duplicate checking
  • Target class distribution
  • Histograms
  • Boxplots
  • Correlation heatmap
  • Correlation analysis with target variable

⚙️ Data Preprocessing

The following preprocessing steps were performed:

  • One-Hot Encoding
  • Train-Test Split (80:20)
  • Stratified Sampling

🤖 Machine Learning Models

1. Decision Tree

A single Decision Tree was trained as the baseline model.

2. Bagging Classifier

Bagging combines multiple Decision Trees trained on different bootstrap samples to improve prediction stability and reduce variance.

3. Random Forest

Random Forest extends Bagging by introducing random feature selection at each split, improving model diversity and reducing overfitting.


📈 Model Performance

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%

📉 Model Evaluation

The models were evaluated using:

  • Accuracy
  • Precision
  • Recall
  • F1 Score
  • ROC-AUC
  • Confusion Matrix
  • ROC Curves
  • Feature Importance
  • Stratified 5-Fold Cross-Validation

🌟 Feature Importance

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

📁 Project Structure

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

📚 Machine Learning Workflow

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

🏆 Results

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.


🚀 Future Improvements

  • Hyperparameter tuning using GridSearchCV
  • Streamlit web application
  • Model deployment
  • Explainability with SHAP
  • Testing additional ensemble algorithms (XGBoost, AdaBoost, Gradient Boosting)

👨‍💻 Author

Oli Bakala

Software Engineering Graduate | AI & Data Science Enthusiast


⭐ If you found this project useful, consider giving it a star!

About

Heart disease prediction using Decision Tree, Bagging, and Random Forest with ensemble learning and performance comparison.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages