Heart Disease Detector Web App

 Yes, you heard that right. We are back with more Machine Learning content and another web app (Yup, machine learning is definitely very cool). This app is similar to our first web app i.e. Diabetes Detector. This is another disease detection web app which detects if you have any cardiovascular diseases (CVD) based on certain features such as age, resting blood pressure, cholestrol, maximum heart rate achieved, ST depression induced by exercise and number of major vessels coloured by flouroscopy. ST depression refers to a finding on an electrocardiogram, wherein the trace in the ST segment is abnormally low below the baseline. Fluoroscopy is an imaging technique that uses X-rays to obtain real-time moving images of the interior of an object. This app is another example of the use of machine learning and data science in the field of medicine which I find very innovative. 

This web app is based on machine learning and uses the Random Forest Classifier algorithm. The web app uses interactive visual and graphical interpretations to display the outcome and compare the input parameters given by the user. The sidebar sliders help in changing the values of the parametres for determination of the result. The graphs compare the values of the patient with others ( both with patients having heart diseases and not having heart diseases). It also provides the accuracy of the result. This is just a learning project based on one particular dataset so please do not depend on it to actually know if you have a heart disease or not. It might still be a false positive or false negative. A doctor is still the best fit for the determination of such diseases. 

We have deployed the app using Streamlit. It is an open source framework that allows data science teams to deploy web apps fairly easily. It’s one of the best hosting services I’ve used and it’s great for quick and easy deployment of web apps. The app is coded in python. 

This web app helped build upon my experience and comfort over machine learning and its vast applications. It is one of our many projects in the field of computer science and specifically data science. We hope to deploy more articles in the future and share them with you. Please feel free to add on and contribute to the project and don’t hesitate to drop any suggestions. The link for the web app is: https://share.streamlit.io/pranav-coder2005/heartdisease/main/app.py  


Disclaimer: This is just a learning project based on one particular dataset so please do not depend on it to actually know if you have any cardiovascular disease or not. It might still be a false positive or false negative. A doctor is still the best fit for the determination of such diseases.

The awareness month for heart health in the United States is February. National wear red day is observed on the first Friday of February of each year to increase awareness about heart diseases. World Heart Federation, World Health Organization and many other such organizations run campaigns dedicated to the prevention, diagnosis and control of cardiovascular diseases.The World Heart Federation is a nongovernmental organization based in Geneva, Switzerland, formed in 1972 In 1978 the International Society of Cardiology merged with the International Cardiology Federation to form the International Society and Federation of Cardiology. The federation hosts the World Congress of Cardiology. World Heart Day” was founded in 2000 to inform people around the globe that heart disease and stroke are the world’s leading causes of death. World Heart Day is celebrated every year on 29 September. On that note, let’s increase cardiovascular health awareness and make the world much healthier. 

Explanation of the Code and how you can make this yourself !

Here, I am going to go through the code in a very concise and simple manner so that people with even minimal experience in programming or data science can follow along and benefit it. This app has been coded in python and has been deployed on streamlit as mentioned before. I’ve also used the Random Forest Classifier Algorithm for this particular problem. 

Alright so lets finally get started. First up I’ve imported the python packages / libraries that I’ve used for this app. More information for them is available on the project template of SkillTools. 

import streamlit as st
import pandas as pd
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import plotly.figure_factory as ff
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import seaborn as sns
from PIL import Image

After this I have included a slight description of the app as a string which includes the dataset resource and the developers. I have also given due credits to the creators of the dataset. After which we need to feed in our dataset and define some headings to that the users can know what this is.

df = pd.read_csv(r'heart.csv')
st.title('Heart Disease Detector')
st.sidebar.header('Patient Data')
st.subheader('Training Dataset')
st.write(df.describe())

After this we need to train and test our data. For the purpose of this app, I’ve used the test size and train size as 90% and 10% respectively.

x = df.drop(['Outcome'], axis = 1)
y = df.iloc[:, -1]
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size = 0.9, random_state = 0)

Once we’re done with this, we need to define the user report and the user report data depending on the various parameters given in the training dataset. For this particular dataset the parameters are Age, Resting Blood Pressure, Cholestrol, Maximum Heart Rate Achieved, ST Depression Induced by Exercise and Number of vessels coloured by Flouroscopy. We also need to mention the range of values of these parameters so that the user can change them using the sliders in the sidebar.

def user_report():
  age = st.sidebar.slider('Age', 0,90, 55 )
  trestbps = st.sidebar.slider('Resting Blood Pressure', 60,200, 126 )
  chol = st.sidebar.slider('Cholestrol', 100,600, 330 )
  thalach = st.sidebar.slider('Maximum Heart Rate Achieved', 60,250, 146 )
  oldpeak = st.sidebar.slider('ST Depression Induced by Exercise', 0.0,7.5, 2.50 )
  ca = st.sidebar.slider('Number of major vessels coloured by Flouroscopy', 0,5, 2 )
  
  

  user_report_data = {
      'age':age,
      'trestbps':trestbps,
      'chol':chol,
      'thalach':thalach,
      'oldpeak':oldpeak,
      'ca':ca,
     
    
         
  }
  report_data = pd.DataFrame(user_report_data, index=[0])
  return report_data





user_data = user_report()
st.subheader('Patient Data')
st.write(user_data)

Now here’s the part that we run the Random Forest Classifier Algorithm, fit the data and run the model based on the input dataset.

rf  = RandomForestClassifier()
rf.fit(x_train, y_train)
user_result = rf.predict(user_data)

Now we finally come to my most favourite part of these web apps: Visualizations. I have been experimenting a lot with a number of visualization libraries but some of them really stand out for me and I use them often in my apps. So here as a convention I’ve used blue colour for patients without cardiological diseases and the colour red for patients with cardiological diseases.

st.title('Graphical Patient Report')




if user_result[0]==0:
  color = 'blue'
else:
  color = 'red'

We start off with Resting Blood Pressure and code in its visualizations. Here I’ve basically plotted a seaborn scatterplot with age on the x axis and the values of the Resting Blood Pressure parameter on the y axis. I have used the purple palette and have scaled the axes according to the data. A value of 0 represents a healthy case whereas a value of 1 represents an unhealthy case.

st.header('Resting Blood Pressure Value Graph (Yours vs Others)')
fig_trestbps = plt.figure()
ax3 = sns.scatterplot(x = 'Age', y = 'Resting Blood Pressure', data = df, hue = 'Outcome' , palette='Purples')
ax4 = sns.scatterplot(x = user_data['age'], y = user_data['trestbps'], s = 150, color = color)
plt.xticks(np.arange(0,100,5))
plt.yticks(np.arange(60,220,10))
plt.title('0 - Healthy & 1 - Unhealthy')
st.pyplot(fig_trestbps)

Now that we are done with one parameter, we can very easily do this same for the other parameters as well. Just replace the above code snippet with that of the other parameters and you are set to go. I will leave this as an exercise for you’ll and if you have any queries regarding it, please do ask. After completing the visualizations for all the parameters, we are finally ready to display the outcome and the prediction. I have given the outcome in the form of a user report.

st.subheader('Your Report: ')
output=''
if user_result[0]==0:
  output = 'Congratulations, you do not have any heart diseases.'
else:
  output = 'Unfortunately, it is likely that you may be having a heart disease.'
st.title(output)

Next, I have duly given the dataset credits to the respective owners and authorities in charge of this dataset and have adhered to its license which is Creative Commons Attribution 4.0 International (CC BY 4.0) in this case. I have also mentioned where I received the dataset from (UCI Machine Learning Repository) and have cited the original creators of this dataset for their commendable work. To cap up this web app, I’ve given a disclaimer that I give for all my BioTechnology and medical applications of data science that this is an application based on one particular dataset so we cannot use it universally. I have also attached the logo of Skillocity at the end. So that’s it from this web app and I’ll see you soon with another fun application of Machine Learning / Data Science and give some interesting insights. Hasta pronto !

Heart Disease Detector Web App8 min read

3 thoughts on “Heart Disease Detector Web App8 min read

  1. Yeah I have tried multiple classification problems based upon open source datasets, I'll keep uploading as and when they're ready for deployment. I'll also look to work upon some self made datasets for deep learning and computer vision in the future

Leave a Reply

Your email address will not be published.

Scroll to top