Heart Attack Prediction Using Machine Learning
Introduction
A simple project based on a Heart Attack Prediction System has been developed to identify the risk of heart disease using machine learning and an easy-to-use web application. Heart disease remains one of the top causes of death globally, and timely prediction can save countless lives. Traditional diagnosis often requires multiple clinical tests and expert consultation, which can be time-consuming and expensive.
This project uses predictive analytics to estimate the likelihood of a heart attack based on patient health records. By inputting medical details such as age, cholesterol, blood pressure, heart rate, and lifestyle factors, the system instantly calculates the probability of a heart attack. With a focus on accessibility and real-time predictions, this project demonstrates how technology and healthcare can come together to support early diagnosis and preventive care.
For students and developers, this project serves as a great example of how machine learning models can be trained, tested, and deployed in an interactive web interface — turning theory into a working healthcare application.
Project Overview
The Heart Attack Prediction System is a complete end-to-end solution that combines data science, machine learning, and web development.
It consists of the following components:
-
Web-based Interface (Streamlit): A user-friendly platform where patients or doctors can enter health parameters.
-
Machine Learning Models: Predictive models trained on medical datasets to estimate heart attack risk.
-
Real-Time Predictions: Users receive immediate risk scores after filling in their details.
The system is designed not only for academic purposes but also as a prototype for healthcare organizations, showing how AI can assist in critical decision-making.

Dataset
The project uses a dataset (hf.csv) containing medical records of patients. The key features include:
- Age
- Anaemia
- Creatinine Phosphokinase
- Diabetes
- Ejection Fraction
- High Blood Pressure
- Platelet Count
- Serum Creatinine
- Serum Sodium
- Sex
- Smoking
- Time (Follow-up period)
- DEATH_EVENT (Target variable indicating survival or fatality)
Model Training
The heart.py script processes the dataset and trains different machine learning models. The steps include:
- Data Preprocessing: The dataset is loaded, and features (
X) and target variable (y) are separated. - Data Splitting: The dataset is split into training and testing sets using
train_test_split. - Model Training: The project implements multiple ML algorithms, including:
- Decision Tree Classifier
- Random Forest Classifier
- Evaluation: Models are tested on unseen data, and accuracy scores are calculated.
- Model Serialization: The best-performing model is saved as a Pickle (
.pkl) file for later use.
Web Application (Streamlit)
The app.py script powers the web-based frontend using Streamlit. Key features:
- User-friendly UI: Patients or doctors can input health parameters.
- Model Integration: The trained model (
hf1.pkl) is loaded to make real-time predictions. - Automated Browser Launch: The app opens automatically in the browser for easy access.
Installation and Usage
Prerequisites
Ensure you have Python installed and install the required dependencies using:
pip install -r requirements.txt
Running the Application
Run the following command to start the Streamlit app:
streamlit run app.py
The application will launch in your default web browser.
How It Works
- User inputs medical data (e.g., age, diabetes status, blood pressure, etc.).
- The system processes the input and applies the trained model.
- A prediction result is displayed, indicating the risk level of a heart attack.
Why This Matters
- Early Detection: Helps identify at-risk individuals before serious complications.
- AI-Powered Insights: Provides an objective analysis based on historical data.
- User-Friendly: Accessible to medical professionals and patients.
Future Enhancements
To improve this project, we could:
- Integrate Deep Learning models for higher accuracy.
- Expand the dataset for better generalization.
- Develop a mobile-friendly version for broader accessibility.
- Add data visualization features for better interpretability.
This Heart Attack Prediction System is a step towards AI-driven preventive healthcare. By leveraging machine learning and Streamlit, this project provides an easy-to-use solution for early risk assessment. With continuous improvements, it could become a valuable tool for predictive analytics in healthcare.
Â



Post Comment