devarena logo
Reading Time: 7 minutes


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

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 attene, many adversts could be prevented. Machine learning holds promise in diagnosing the possibility of adverse cardiac outcomes in high-risk individuals.

In this artannual 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.

df=pd.read_csv('heart_disease.csv')
df.head()

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

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

df.info()
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

df.isnull().sum()
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
obj_list

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))
df.head()
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

correlation=df.corr()
correlation

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
np.random.seed(41)
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

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
np.random.seed(41)
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
np.random.seed(41)
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
prediction=lr.predict(X_test)
prediction
confusion_matrix=confusion_matrix(y_test,prediction)
confusion_matrix

KNN

from sklearn.metrics import confusion_matrix
prediction=knn.predict(X_test)
prediction
confusion_matrix=confusion_matrix(y_test,prediction)
confusion_matrix

Random Forest Classifier

from sklearn.metrics import confusion_matrix
prediction=rf.predict(X_test)
prediction
confusion_matrix=confusion_matrix(y_test,prediction)
confusion_matrix

Conclusion on Heart Disease Prediction

Among the erating 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.



Source link

Spread the Word!