Cardiovascular Disease Prediction Using KNN Algorithm
By: Cibhi Baskar
Tags: Heart Disease, Machine Learning, Data Science, Analytics

Heart disease is a term covering any disorder of the heart. Unlike a cardiovascular disease, which describes problems with the blood vessels and circulatory system as well as the heart. Heart disease refers to issues and deformities in the heart itself. Cardiovascular disease generally refers to conditions that involve narrowed or blocked blood vessels that can lead to a heart attack, chest pain (angina) or stroke. Other heart conditions, such as those that affect your heart’s muscle, valves or rhythm, also are considered forms of heart disease.

Prediction of cardiovascular disease is regarded as one of the most important subjects in the section of clinical data analysis. I will be applying Machine Learning algorithm, which proves to be effective for classifying whether a person is suffering from cardiovascular disease or not, using one of the most used Cleveland Heart Disease Dataset from the UCI Repository.

THE DATA

The dataset which I took consist of 303 individuals data related to heart disease. This dataset consist of 13 different features/attributes which I will be using it to predict whether the individual has the heart disease or not.

Cleveland Heart Disease Dataset from the UCI Repository.

Dataset Features:

1. Age: displays the age of the individual.

2. Sex: displays the gender of the individual using the following format :

1 = male

0 = female

3. Chest-pain type: displays the type of chest-pain experienced by the individual using the following format :

1 = typical angina

2 = atypical angina

3 = non — angina pain

4 = asymptotic

4. Resting Blood Pressure: displays the resting blood pressure value of an individual in mmHg (unit)

5. Serum Cholesterol: displays the serum cholesterol in mg/dl (unit)

6. Fasting Blood Sugar: compares the fasting blood sugar value of an individual with 120mg/dl.

If fasting blood sugar > 120mg/dl then : 1 (true)

else : 0 (false)

7. Resting ECG : displays resting electrocardiographic results

0 = normal

1 = having ST-T wave abnormality

2 = left ventricular hypertrophy

8. Max heart rate achieved: displays the max heart rate achieved by an individual.

9. Exercise induced angina :

1 = yes

0 = no

10. ST depression induced by exercise relative to rest: displays the value which is an integer or float.

11. Peak exercise ST segment :

1 = up sloping

2 = flat

3 = down sloping

12. Number of major vessels (0–3) colored by fluoroscopy: displays the value as integer or float.

13. Thal : displays the thalassemia :

3 = normal

6 = fixed defect

7 = reversible defect

There is an “Target” variable which displays whether the individual is suffering from the disease or not: (0 = absence and 1 = present)

PROGRAMMING LANGUAGE DESCRIPTION

Python is one of the most popular high-level languages used by data scientists and software developers alike for data science tasks. It can be used to predict outcomes, automate tasks, streamline processes, and offer business intelligence insights. There are quite a few open-source libraries that make Python data tasks much easier.

The Python libraries and streamlined processes make data analysis and machine learning to a whole new level. There are so many different libraries in python which helps the user to make their work effective and efficient. The Python libraries are also called as ‘Modules’.

For developing the cardiovascular disease prediction model, I used Pandas, NumPy, Matplotlib and Scikit-learn modules.

Pandas is an open-source library, specifically developed for data science and analysis. It is built upon the Numpy (to handle numeric data in tabular form) package and has inbuilt data structures to ease up the process of data munging/wrangling, aka data manipulation.

is an open-source library, specifically developed for data science and analysis. It is built upon the Numpy (to handle numeric data in tabular form) package and has inbuilt data structures to ease up the process of data munging/wrangling, aka data manipulation. NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of routines for processing those arrays. Using NumPy, mathematical and logical operations on arrays can be performed.

which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of routines for processing those arrays. Using NumPy, mathematical and logical operations on arrays can be performed. Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays.

is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays. Scikit-learn is probably the most useful library for machine learning in Python. The sklearn library contains a lot of efficient tools for machine learning and statistical modelling including classification, regression, clustering and dimension reduction.

DATA ANALYSIS

I used Jupyter Notebook to do the analysis and develop the ML model. The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Before starting the analysis part we need to import necessary libraries to the notebook. We need to process the data (Data Pre-processing) to make it more effective for developing the Machine Learning model. Data pre-processing is an important step in the data mining process. The phrase “garbage in, garbage out” is particularly applicable to data mining and machine learning projects.

Pandas library is more effective for data manipulation process. Using the dropna() function, we can remove/drop the missing values present in the dataset. Using the describe() function we can see the statistical information from the dataset.

Dependent and independent variables are variables in Machine Learning modelling. Independent variables are controlled input, which is denoted by ‘X’. Dependent variables represent the output or outcome resulting from altering these inputs, which is denoted by ‘y’.

Independent variable (X) — All the features present in the dataset except the ‘target’ column is my independent variable. These features will be used as the input to the ML algorithm. Dependent variable (y) — ‘Target’ will be the output variable.

Independent variable (X)

Dependent variable (y)

Imported the Scikit-learn module to normalize the independent variable data. Data Normalization is the process of structuring a relational data in accordance with a series of so-called normal forms in order to reduce data redundancy and improve data integrity.

To perform Data Visualization, I used the Matplotlib library to plot the histogram and visualize the Cholesterol Level of the individuals.

ALGORITHM (K-NEAREST NEIGHBOR)

The k-nearest neighbors (KNN) algorithm is a simple, easy-to-implement supervised machine learning algorithm that can be used to solve both classification and regression problems. A supervised machine learning algorithm (as opposed to an unsupervised machine learning algorithm) is one that relies on labeled input data to learn a function that produces an appropriate output when given new unlabeled data.

A classification problem has a discrete value as its output. Here, the dependent variable ‘Target’ is the output. The KNN algorithm assumes that similar things exist in close proximity. In other words, similar things are near to each other. To find the distance we use the Euclidean-distance formula to calculate the similarity/distance.

Here, the ‘k’ in k-nearest neighbor is the number of neighbors that our model take into consideration to predict the outcome.

Train-Test Split

Using the train_test_split function, split the dataset into training and testing set. Training set will be used to train the ML model, whereas the testing set will be used to check how well our model performs in “unseen data” or also known as real-world data.

The KNN Algorithm

Initialize K number of neighbors that our model need to take into consideration to predict the outcome.

When the model gets an input (real-world data) as an query, it will predict whether the individual has the heart disease or not by calculating the distance between this new unseen data and all other records in which our model is trained.

After calculating the distance/similarity, sort the examples from shortest to longest (ascending order) by the distances.

Pick the first K entries from the sorted collection and get the labels of the selected K entries.

For regression, the algorithm returns the “mean” and if classification, the algorithm returns the “mode” of the K labels.

Calculate the model accuracy score, to check how well the model performs in the real world.

KNN algorithm

MODEL EVALUATION

Model Evaluation is an integral part of the model development process. It helps to find the best model that represents our data and how well the chosen model will work in the future. The KNN algorithm successfully predicts the classification problem with accuracy score of 87.85. To evaluate the model further, I performed the model evaluation process using the confusion matrix and calculated the F1-Score.

Confusion matrix is the most preferred model evaluation metric for any classification algorithm. The Confusion matrix will show the true positives, true negatives, false positives and false negatives. It is also extremely useful for measuring Recall, Precision and F1-Score.

True Positive (TP): You predicted positive and it’s true.

You predicted positive and it’s true. True Negative (TN): You predicted negative and it’s true.

You predicted negative and it’s true. False Positive (Type 1 Error): You predicted positive and it’s false.

You predicted positive and it’s false. False Negative (Type 2 Error): You predicted negative and it’s false.

Confusion Matrix (TP, TN, FP, FN)

CONCLUSION

Manually determining the odds of cardiovascular disease based on risk factors can be hard. Using Machine learning techniques we can predict the outcome with the help of existing data. But still, we can’t trust the machine always. As you can see from this prediction, we got some percentage of “False positives and False negatives”. The only way to prevent cardiovascular disease is to stay healthy.

Dataset from UCI Machine Learning Repository.