Heart Disease Prediction using ANN

Deep Learning is a technology of which mimics a human brain in the sense that it consists of multiple neurons with multiple layers like a human brain. The network so formed consists of an input layer, an output layer, and one or more hidden layers. The network tries to learn from the data that is fed into it and then performs predictions accordingly. The most basic type of neural network is the ANN (Artificial Neural Network). The ANN does not have any special structure, it just comprises of multiple neural layers to be used for prediction.
Let’s build a model that predicts whether a person has heart disease or not by using ANN.

About the data:
In the dataset, we have 13 columns in which we are given different attributes such as sex, age, cholesterol level, etc. and we are given a target column which tells us whether that person has heart disease or not. We will keep all the columns as independent variables other than the target column because it will be our dependent variable. We will build an ANN which will predict whether a person has heart disease or not given other attributes of the person.

You can find the dataset here heart disease dataset

Code: Importing Libraries

filter_none

edit
close

play_arrow

link
brightness_4
code

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import confusion_matrix

chevron_right


Code: Importing Dataset



filter_none

edit
close

play_arrow

link
brightness_4
code

data = pd.read_csv('heart.csv')
data.head()

chevron_right


Data Description:

filter_none

edit
close

play_arrow

link
brightness_4
code

data.describe()

chevron_right


Code: Check for null values

filter_none

edit
close

play_arrow

link
brightness_4
code

data.isnull().any()

chevron_right



Assign Dependent and Independent variable

filter_none

edit
close

play_arrow

link
brightness_4
code

X = data.iloc[:,:13].values
y = data["target"].values

chevron_right


Code : Split data into Train and Test dataset

filter_none

edit
close

play_arrow

link
brightness_4
code

X_train,X_test,y_train, y_test = train_test_split(X,y,test_size = 0.3 , random_state = 0 )

chevron_right


Code: Scale the data.



filter_none

edit
close

play_arrow

link
brightness_4
code

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

chevron_right



Code: Building the Model

filter_none

edit
close

play_arrow

link
brightness_4
code

classifier = Sequential()
classifier.add(Dense(activation = "relu", input_dim = 13
                     units = 8, kernel_initializer = "uniform"))
classifier.add(Dense(activation = "relu", units = 14
                     kernel_initializer = "uniform"))
classifier.add(Dense(activation = "sigmoid", units = 1
                     kernel_initializer = "uniform"))
classifier.compile(optimizer = 'adam' , loss = 'binary_crossentropy'
                   metrics = ['accuracy'] )

chevron_right


Code : Fitting the Model

filter_none

edit
close

play_arrow

link
brightness_4
code

classifier.fit(X_train , y_train , batch_size = 8 ,epochs = 100  )

chevron_right



Code : Performing prediction and rescaling

filter_none

edit
close

play_arrow

link
brightness_4
code

y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)

chevron_right


Code: Confusion Matrix

filter_none

edit
close

play_arrow

link
brightness_4
code

cm = confusion_matrix(y_test,y_pred)
cm

chevron_right


Code: Accuracy

filter_none

edit
close

play_arrow

link
brightness_4
code

accuracy = (cm[0][0]+cm[1][1])/(cm[0][1] + cm[1][0] +cm[0][0] +cm[1][1])
print(accuracy*100)

chevron_right



We will get accuracy approximately around 85%.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.