Heart Disease Prediction in TensorFlow 2 | TensorFlow for Hackers (Part II)
Classify heart disease from patient data using a Neural Network in TensorFlow 2
TL;DR Build and train a Deep Neural Network for binary classification in TensorFlow 2. Use the model to predict the presence of heart disease from patient data.
Machine Learning is used to solve real-world problems in many areas, already. Medicine is no exception. While controversial, multiple models have been proposed and used with some success. Some notable projects by Google and others:
Today, we’re going to take a look at one specific area — heart disease prediction.
About 610,000 people die of heart disease in the United States every year — that’s 1 in every 4 deaths. Heart disease is the leading cause of death for both men and women. More than half of the deaths due to heart disease in 2009 were in men. — Heart Disease Facts & Statistics | cdc.gov
Please note, the model presented here is very limited and in no way applicable for real-world situations. Our dataset is extremely small, conclusions made here are in no way generalizable. Heart disease prediction is a vastly more complex problem than depicted in this writing.
Here is the plan:
- Explore patient data
- Data preprocessing
- Create your Neural Network in TensorFlow 2
- Train the model
- Predict heart disease from patient data
Our data comes from this dataset. It contains 303 patient records. Each record contains 14 attributes.
How many of the patient records indicate heart disease?
That looks like a pretty well-distributed dataset, considering the number of rows.
Let’s have a look at how heart disease affects different genders:
Here is a Pearson correlation heatmap between the features:
How disease presence is affected by
thalach (“Maximum Heart Rate”) vs
Looks like maximum heart rate can be very predictive for the presence of a disease, regardless of age.
How different types of chest pain affect the presence of heart disease:
Having chest pain might not be indicative of heart disease.
Our data contains a mixture of categorical and numerical data. Let’s use TensorFlow`s Feature Columns.
Feature columns allow you to bridge/process the raw data in your dataset to fit your model input data requirements. Furthermore, you can separate the model building process from the data preprocessing. Let’s have a look:
Apart from the numerical features, we’re putting patient
age into discrete ranges (buckets). Furthermore,
slope are categorical and we map them to such.
Next up, lets turn the pandas DataFrame into a TensorFlow Dataset:
And split the data into training and testing:
Let’s build a binary classifier using Deep Neural Network in TensorFlow:
Our model uses the feature columns we’ve created in the preprocessing step. Note that, we’re no longer required to specify the input layer size.
We also use the Dropout layer between 2 dense layers. Our output layer contains 2 neurons, since we are building a binary classifier.
Our loss function is binary cross-entropy defined by:
where y is binary indicator if the predicted class is correct for the current observation and p is the predicted probability.
Here is a sample of the training process:
0s 42ms/step - loss: 0.3018 - accuracy: 0.8430 - val_loss: 0.4012 - val_accuracy: 0.8689
0s 42ms/step - loss: 0.2882 - accuracy: 0.8547 - val_loss: 0.3436 - val_accuracy: 0.8689
0s 42ms/step - loss: 0.2889 - accuracy: 0.8732 - val_loss: 0.3368 - val_accuracy: 0.8689
0s 42ms/step - loss: 0.2964 - accuracy: 0.8386 - val_loss: 0.3537 - val_accuracy: 0.8770
0s 43ms/step - loss: 0.3062 - accuracy: 0.8282 - val_loss: 0.4110 - val_accuracy: 0.8607
0s 43ms/step - loss: 0.2685 - accuracy: 0.8821 - val_loss: 0.3669 - val_accuracy: 0.8852
Accuracy on the test set:
0s 24ms/step - loss: 0.3669 - accuracy: 0.8852
So, we have ~88% accuracy on the test set.
Predicting Heart Disease
Now that we have a model with some good accuracy on the test set, let’s try to predict heart disease based on the features in our dataset.
Since we’re interested in making binary decisions, we’re taking the maximum probability of the output layer.
precision recall f1-score support
0 0.59 0.66 0.62 29
1 0.66 0.59 0.62 32
micro avg 0.62 0.62 0.62 61
macro avg 0.62 0.62 0.62 61
weighted avg 0.63 0.62 0.62 61
Regardless of the accuracy, you can see that the precision, recall and f1-score of our model are not that high. Let’s take a look at the confusion matrix:
Our model looks a bit confused. Can you improve on it?
You did it! You made a binary classifier using Deep Neural Network with TensorFlow and used it to predict heart disease from patient data.
Next, we’ll have a look at what TensorFlow 2 has in store for us, when applied to computer vision.
Originally published at www.curiousily.com.