How I Built a Cardiovascular Disease Detector using Machine Learning and FastAPI

okellodaniel

Daniel Okello

Posted on November 21, 2024

How I Built a Cardiovascular Disease Detector using Machine Learning and FastAPI

Introduction

Cardiovascular diseases are among the leading causes of death in the world. However, a lot can be done if there is early detection. Traditional methods of screening for the condition may be expensive and beyond the reach of many people. Inspired by such challenges, the need to develop a simple yet effective machine-learning solution that will predict the chance of cardiovascular disease using basic health indicators motivated me to create the Cardio Vascular Disease Detector.

In this article, I will share the journey of building this project, from cleaning messy data to deploying a prediction API in the cloud. Whether you're interested in machine learning, API development, or healthcare innovation, I hope you will find something herein that will be useful.

The Problem

Early detection of cardiovascular diseases isn't always easy. Given the millions at risk, scalable health systems are in dire need of solutions to help prioritize potential cases well in time. This is where machine learning comes in-by looking at patterns in health data, a determination of disease risks can be made efficiently at scale.

The Solution

The Cardio Vascular Disease Detector is my attempt at filling this gap. It uses a machine-learning model to predict whether a person is at risk of cardiovascular disease based on input data such as cholesterol levels, blood pressure, and age. What's more, the model is accessed through a lightweight API, making it easy to integrate into other tools or systems.

How It Works

The process is simple:

  1. Post Data: The user provides input data, such as age, cholesterol level, and gender, to the API in JSON format.
  2. Validate Input: The API checks to ensure the data is present and in the right format.
  3. Predict: Given the input to the model, it returns:
    • The probability of the prediction.
  4. Get Results: The API sends back the prediction, making it easy to act on the insights.

This flow is designed to be fast, efficient, and user-friendly.

What's Under the Hood?

Here's the tech stack powering the project:

  • Machine Learning: I chose XGBoost because it supports the most complex patterns of data efficiently. From the trial of various algorithms, XGBoost was the best performer.
  • Backend Framework: FastAPI was pretty much a no-brainer due to its light weight, speed, and ease of setup. Plus, its support for Pydantic means input validation is not a headache.
  • Containerization: Docker assures that the project environment is going to stay consistent, whether local or in production.
  • Deployment: I used Fly.io to deploy the API; it's simple and scalable.

How I Built It

This project came into being in a series of steps:

  1. Exploring the Data
    I began working with the Kaggle Cardiovascular Disease Dataset containing 70,000 health records. I visualized this dataset to find the most important features influencing CVD risks, such as cholesterol level and blood pressure.

  2. Training the Model
    Cleaning the data, encoding categorical variables, and eventually training an XGBoost model, with some tuning of its hyperparameters, yielded a highly accurate model in predicting disease risk.

  3. Building API
    Once the model was ready, I had to expose it as a RESTful API using FastAPI to serve predictions to users with minimum overhead.

  4. **API Deployment
    I Dockerized the project, making it run reliably across different environments. I used Fly.io to deploy my API and expose it to users worldwide.

Challenges Faced

Building this project wasn’t without its hurdles. Cleaning the dataset took longer than expected due to inconsistencies in the data. Tuning the model for optimal performance also required patience and experimentation. Finally, deploying the app involved learning the nuances of Docker and Fly.io. But each challenge taught me something new, and the end result was worth the effort.

Why It Matters

This example is a little more than a technical exercise; it's also one of how machine learning can make a real difference in people's lives. This tool, by predicting the risks for CVD, can enable healthcare professionals to identify those at high risk early and plan timely interventions.

What's Next?

There’s still room for improvement. For example, adding more features or integrating with real-world medical systems could make the tool even more impactful. But for now, I’m proud of what this project represents: a simple yet effective way to use technology for good.

Try It Yourself

If you are interested in the code, or would like to create your own, please have a look at the project on GitHub. You will find everything there from the scripts of data pre-processing to the implementation in FastAPI.

Final Words
Let me know your thoughts on this, or share your experiences about machine learning projects you have going on in the comments below!

💖 💪 🙅 🚩
okellodaniel
Daniel Okello

Posted on November 21, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related