Saptarshi Dutta — Updated On June 8th, 2022
Classification Intermediate Machine Learning Python

This article was published as a part of the Data Science Blogathon.

Introduction on Heart Disease Prediction

This article is about exploring the potential of machine learning in understanding key indicators of Heart Disease Prediction. Heart disease puts an enormous burden on the global healthcare system. If the problem is attended to on time, many adverse events could be prevented. Machine learning holds promise in diagnosing the possibility of adverse cardiac outcomes in high-risk individuals.

In this article, we shall be making use of the “personal key indicators of heart disease” dataset to identify individuals at risk for heart disease. The dataset comprises an annual survey done by the CDC (Center of disease control), the USA of 400000 adults related to their health status. The dataset can be downloaded from the link below

EDA (Exploratory Data Analysis)

For any analysis, we need to have data. So, at the outset, we shall import data. Importing of data starts by following lines of code

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

In the above lines of code, we have imported numpy and pandas libraries respectively. Then, we imported the matplotlib library. This is useful for interactive visualizations in python. Seaborn is another data visualization library that is more suited for handling pandas dataframe. The next step would be to create a dataframe. Pandas dataframe is a 2-dimensional tabular structure with rows and columns.


The file is in the name heart_disease and the above line of code uploads data from the external sources. ‘read_csv’ enables us to read csv files. The code df.head() displays the top 5 rows of the dataset as follows

EDA (Exploratory Data Analysis)

Next, we need to find the information in the dataset. This can be done by using the info() function. The information would comprise range index, data columns, non-null count, memory usage, and datatypes. The following are the line of code and the output
EDA (Exploratory Data Analysis)

Next, we check for null values in pandas dataframe with the help of isnull() function. The function isnull().sum() displays the missing values in the dataset. The line of code and the output are

EDA (Exploratory Data Analysis)

From the output, it can be seen that there are no missing values in the dataset. Next, we would like to get the data type of each column in the form of a series. The line of code and the output are as follows

obj_list = df.select_dtypes(include='object').columns

Here, we have created an object list including object datatype.

Next, we would encode target labels with values 0 and n-classes-1. This would transform the labels into the form of numbers that can be easily read by the machine. For this purpose, we would import LabelEncoder module from sklearn.preprocessing library. Following are the lines of code and the outputs

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for obj in obj_list:
        df[obj] = le.fit_transform(df[obj].astype(str))
EDA (Exploratory Data Analysis) 2

From the output, it can be seen that labels have been transformed into numeric forms. Next, we would like to check the relationship between each parameter with the target which is heart disease. This will be achieved by using the correlation function. Correlation measures the strength of the relation between 2 datasets with a positive correlation showing strong dependency of one variable on other and a negative correlation showing an inverse relationship. The range of correlation ranges between -1 and +1. The lines of code and the output are as follow


From the above output, let us take some variables and analyze with respect to our target, i.e. Heart Disease. BMI=0.051803, smoking=0.107764, Alcohol drinking = -0.032080, Stroke = 0.196835, Physical Health = 0.170721, Mental Health = 0.028591, Age category = 0.233432, Diabetic = 0.168553, Physical activity = -0.100030, General Health = -0.011062, and Sleep time = 0.008327.
It can be seen that physical activity and general health are associated with reduced adverse cardiac outcomes whereas stroke, diabetes, smoking, and physical health status are relative positively associated with adverse cardiac outcomes.

Model Development

We shall proceed to label the inputs and the output. Here, ‘HeartDisease’ is the target variable and whether the individual concerned will have heart disease or not shall be determined based on inputs provided. The first step of model development would be to perform a train-test split by importing the train_test_split method from the sklearn library. X would have independent variables, and y would have the target variable. 30% of the dataset would be the testing data. For a deterministic train-test split, random_state is set to an integer value. So, lines of code are

X = df.drop('HeartDisease', axis=1).values
y = df['HeartDisease'].values

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

The above line of code would split the data into train and test datasets to measure the model generalization to predict unseen data of the model. Now, we shall develop the model with the help of 2 different algorithms viz. kNN, Logistic Regression, and Random Forest classifier.


kNN works by selecting the number k of the neighbors followed by a calculation of Euclidean distance. Then, the number of data points is counted in each category and the new data points are assigned to that category. Let’s look at the lines of code and the respective output in the form of accuracy

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier().fit(X_train, y_train)
knn.score(X_test, y_test)

Logistic Regression

From sklearn.linear_model we shall be importing the logistic regression module and then fit training data. Let’s look at the lines of code and the respective output in the form of accuracy

from sklearn.linear_model import LogisticRegression
lr = LogisticRegression().fit(X_train, y_train)
lr.score(X_test, y_test)

Random Forest Classifier

From sklearn.ensemble we shall be importing logistic random forest classifier module and then fit training data. Let’s look at the lines of code and the respective output in the form of accuracy

from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier().fit(X_train, y_train)
rf.score(X_test, y_test)

In all the above cases, we have used np.random.seed() to get the, most random numbers for each run. Through accuracy score, we have evaluated the performance of various models.

Model Evaluation

Though we have evaluated the models with the help of an accuracy score, the confusion matrix would enable us to further understand the misclassification aspect of the model development process. The confusion matrix is depicted below

       Predicted: Yes        Predicted: No               Total
Actual: Yes             a (T.P)            b (F.N)          a+b (Actual yes)
Actual: No             c (F.P)            d (T.N)          c+d (Actual No)
Total        a+c (Predicted yes)         b+d (Predicted No)             a+b+c+d
Here, T.P = True Positive, F.N = False Negative, F.P = False Positive, T.N = True Negative

Confusion matrix through KNN, Logistic Regression, and Random Forest Classifier
The lines of code and their respective outputs for the confusion matrix concerning the three algorithms are as follows

Logistic Regression

from sklearn.metrics import confusion_matrix


from sklearn.metrics import confusion_matrix

Random Forest Classifier

from sklearn.metrics import confusion_matrix

Conclusion on Heart Disease Prediction

Among the classifiers we can see that KNN had an accuracy of 90.53%, logistic regression had an accuracy of 91.45%, and random forest classifier had an accuracy rate of 90.55%. Further evaluation of the model entails generating the confusion matrix to understand the pattern of classification. In the case of logistic regression, 87,745 out of 95,939 samples were correctly classified whereas it is 86,863 for KNN, and 86,873 for the random forest classifier. Though all the algorithms performed to the tune of the accuracy of more than 90%, logistic regression was the most accurate one.

Key takeaways on Heart Disease Prediction

  • KNN, logistic regression and random forest classifiers are capable of segregating individuals into high and low-risk categories with a high accuracy rate.
  • A confusion matrix is a key tool to understand the misclassification aspect of the model.
  • High blood pressure, high cholesterol, smoking, diabetic status, and physical activity are important factors that cause heart disease.
  • Machine learning enables the detection of patterns from the data for the prediction of the patient’s condition.

Thank you for giving your valuable time to read this article!

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

About the Author

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

2 thoughts on "Machine Learning for Heart Disease Prediction"

Roma says: September 19, 2022 at 3:28 pm
what a good piece of knowledge. good article about heart health. Reply
soumitha says: May 23, 2023 at 4:21 pm
can i get this dataset Reply

Leave a Reply Your email address will not be published. Required fields are marked *