The Wayback Machine - https://web.archive.org/web/20240710123755/https://www.geeksforgeeks.org/heart-disease-prediction-using-ann/
Open In App

Heart Disease Prediction using ANN

Last Updated : 11 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


Code: Importing Dataset




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


Data Description:




data.describe()


Code: Check for null values




data.isnull().any()



Assign Dependent and Independent variable




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


Code : Split data into Train and Test dataset




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


Code: Scale the data.




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



Code: Building the Model




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'] )


Code : Fitting the Model




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



Code : Performing prediction and rescaling




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


Code: Confusion Matrix




cm = confusion_matrix(y_test,y_pred)
cm


Code: Accuracy




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



We will get accuracy approximately around 85%.



Previous Article
Next Article

Similar Reads

Heart Disease Prediction using Support Vector Machine
Heart disease is a significant health concern worldwide, and early detection plays a crucial role in effective treatment and prevention. Machine learning algorithms, such as Support Vector Machines (SVM), have shown promising results in predicting heart disease based on patient data. SVM stands for Support Vector Machine are a set of supervised lea
4 min read
ML | Heart Disease Prediction Using Logistic Regression
World Health Organization has estimated that four out of five cardiovascular disease (CVD) deaths are due to heart attacks. This whole research intends to pinpoint the ratio of patients who possess a good chance of being affected by CVD and also to predict the overall risk using Logistic Regression. What is Logistic Regression?Logistic Regression i
4 min read
Heart Disease Prediction Using Logistic Regression in R
Machine learning can effectively identify patterns in data, providing valuable insights from this data. This article explores one of these machine learning techniques called Logistic regression and how it can analyze the key patient details and determine the probability of heart disease based on patient health data in R Programming Language. Logist
13 min read
Machine Learning Heart Disease Prediction Project in Django
Introduction-In this article, we will implement a Machine Learning Heart disease Prediction Project using the Django framework using Python. Here, we will convert the code of the heart diseases prediction into a web form with the help of the Django framework basically we will create a form by using the Django framework and add the dataset of heart
9 min read
Parkinson Disease Prediction using Machine Learning - Python
Parkinson's disease is a progressive disorder that affects the nervous system and the parts of the body controlled by the nerves. Symptoms are also not that sound to be noticeable. Signs of stiffening, tremors, and slowing of movements may be signs of Parkinson's disease. But there is no ascertain way to tell whether a person has Parkinson's diseas
6 min read
Disease Prediction Using Machine Learning
This article aims to implement a robust machine-learning model that can efficiently predict the disease of a human, based on the symptoms that he/she possesses. Let us look into how we can approach this machine-learning problem: Approach: Gathering the Data: Data preparation is the primary step for any machine learning problem. We will be using a d
10 min read
Introduction to ANN (Artificial Neural Networks) | Set 3 (Hybrid Systems)
Prerequisites: Genetic algorithms, Artificial Neural Networks, Fuzzy Logic Hybrid systems: A Hybrid system is an intelligent system that is framed by combining at least two intelligent technologies like Fuzzy Logic, Neural networks, Genetic algorithms, reinforcement learning, etc. The combination of different techniques in one computational model m
4 min read
ANN - Implementation of Self Organizing Neural Network (SONN) from Scratch
Prerequisite: ANN | Self Organizing Neural Network (SONN) Learning Algorithm To implement a SONN, here are some essential consideration- Construct a Self Organizing Neural Network (SONN) or Kohonen Network with 100 neurons arranged in a 2-dimensional matrix with 10 rows and 10 columns Train the network with 1500 2-dimensional input vectors randomly
4 min read
ANN - Bidirectional Associative Memory (BAM)
Bidirectional Associative Memory (BAM) is a supervised learning model in Artificial Neural Network. This is hetero-associative memory, for an input pattern, it returns another pattern which is potentially of a different size. This phenomenon is very similar to the human brain. Human memory is necessarily associative. It uses a chain of mental assoc
2 min read
ANN - Self Organizing Neural Network (SONN)
Self Organizing Neural Network (SONN) is an unsupervised learning model in Artificial Neural Network termed as Self-Organizing Feature Maps or Kohonen Maps. These feature maps are the generated two-dimensional discretized form of an input space during the model training (based on competitive learning). This phenomenon is very similar to biological
2 min read