Diabetes Prediction Using Machine Learning
Diabetes Prediction Using Machine Learning
To
Submitted by
Sanju Bhaskar & Bhaskar Roy
Certificate from the Mentor
This is to certify that SANJU BHASKAR & BHASKAR ROY has successfully completed the
project titled ‘DIABETES PREDICTION USING MACHINE LEARNING’ under my
supervision during the period from January to April which is in partial fulfillment of
requirements for the award of the Bachelors in Computer Application and submitted to
GITARAM INSTITUTE OF MANAGEMENT.
____________________
Signature of the Mentor
Date:
ACKNOWLEDGEMENT
I take this opportunity to express my deep gratitude and sincerest thanks to my project mentor
Mr. Mahendra Dutta for giving his most valuable suggestion, helpful guidance and
encouragement in the execution of this project work.
I would like to give a special mention to my colleagues. Last but not the least I am grateful to
all the faculty members of Ardent Computech Private Limited for their support.
Table of Contents
1. Abstract 5
2. Introduction 5
3. AI, Goals of AI 6
8. Tools Used 13
9. Codes 16
12. Bibliography 28
Abstract
The remarkable advances in biotechnology and health sciences have led to a significant production of
data, such as high throughput genetic data and clinical information, generated from large Electronic
Health Records (EHRs). To this end, application of machine learning and data mining methods in
biosciences is presently, more than ever before, vital and indispensable in efforts to transform
intelligently all available information into valuable knowledge. Diabetes mellitus (DM) is defined as
a group of metabolic disorders exerting significant pressure on human health worldwide. Extensive
research in all aspects of diabetes (diagnosis, etiopathophysiology, therapy, etc.) has led to the
generation of huge amounts of data. The aim of the present study is to conduct a systematic review of
the applications of machine learning, data mining techniques and tools in the field of diabetes
research with respect to a) Prediction and Diagnosis, b) Diabetic Complications, c) Genetic
Background and Environment, and e) Health Care and Management with the first category appearing
to be the most popular. Logistic Regression is a well-suited algorithm for Diabetes prediction and
came up with better accuracy and other performance indices as compared with other models.
Introduction
5
Databases: Python provides interfaces to all major commercial databases.
GUI Programming: Python supports GUI applications that can be created and ported to many
system calls, libraries, and windows systems, such as Windows MFC, Macintosh, and the Window
system of Unix.
Apart from the above-mentioned features, Python has a big list of good features, few are listed below:
• It supports functional and structured programming methods as well as OOP. It can be used as
a scripting language or can be compiled to byte code for building large applications.
• It provides very high-level dynamic datatypes and supports dynamic type checking.
• It supports automatic garbage collections.
• It can be easily integrated with C, C++, COM, ActiveX, CORBA and JAVA.
Artificial Intelligence
According to the father of Artificial Intelligence, John McCarthy, it is “The science and engineering
of making intelligent machines, especially intelligent computer programs”.
Goals of AI
To Create Expert Systems − The systems which exhibit intelligent behaviour, learn, demonstrate,
explain, and advice its users.
6
To Implement Human Intelligence in Machines − Creating systems that understand, think, learn, and
behave like humans.
Applications of AI
Classification of AI
Deep Learning is a subset of machine learning. Usually, when people use the term deep learning,
they are referring to deep artificial neural networks, and somewhat less frequently to deep
reinforcement learning.
Deep learning is a class of machine learning algorithms that:
• Use a cascade of multiple layers of nonlinear processing units for feature extraction and
transformation. Each successive layer uses the output from the previous layer as input.
• Learn in supervised (e.g., classification) and/or unsupervised (e.g., pattern analysis) manners.
• Learn multiple levels of representations that correspond to different levels of abstraction; the
levels form a hierarchy of concepts.
7
• Use some form of gradient descent for training via backpropagation.
Artificial Neural Networks (ANNs) or Connectionist Systems are computing systems inspired by the
biological neural networks that constitute animal brains. Such systems learn (progressively improve
performance on) tasks by considering examples, generally without task-specific programming. ANN
is based on a collection of connected units or nodes called artificial neurons (analogous to biological
neurons in an animal brain). Each connection between artificial neurons can transmit a signal from
one to another.
Machine learning is a field of computer science that gives computers the ability to learn without
being explicitly programmed.
Evolved from the study of pattern recognition and computational learning theory in artificial
intelligence, machine learning explores the study and construction of algorithms that can learn from
and make predictions on data.
Machine Learning is a field of computer science that gives computers the ability to learn without
being explicitly programmed.
8
Arthur Samuel, an American pioneer in the field of computer gaming and artificial intelligence,
coined the term "Machine Learning" in 1959 while at IBM. Evolved from the study of pattern
recognition and computational learning theory in artificial intelligence, machine learning explores the
study and construction of algorithms that can learn from and make predictions on data
Machine learning tasks are typically classified into two broad categories, depending on whether there
is a learning "signal" or "feedback" available to a learning system.
Supervised Learning is the machine learning task of inferring a function from labelled training data.[1]
The training data consist of a set of training examples. In supervised learning, each example is a pair
consisting of an input object (typically a vector) and a desired output value.
A supervised learning algorithm analyses the training data and produces an inferred function, which
can be used for mapping new examples. An optimal scenario will allow for the algorithm to correctly
determine the class labels for unseen instances. This requires the learning algorithm to generalize
from the training data to unseen situations in a "reasonable" way.
Unsupervised Learning is the machine learning task of inferring a function to describe hidden
structure from "unlabelled" data (a classification or categorization is not included in the
observations). Since the examples given to the learner are unlabelled, there is no evaluation of the
accuracy of the structure that is output by the relevant algorithm—which is one way of distinguishing
unsupervised learning from supervised learning and reinforcement learning.
A central case of unsupervised learning is the problem of density estimation in statistics, though
unsupervised learning encompasses many other problems (and solutions) involving summarizing and
explaining key features of the data.
In statistical modelling, regression analysis is a set of statistical processes for estimating the
relationships among variables. It includes many techniques for modelling and analysing several
variables, when the focus is on the relationship between a dependent variable and one or more
independent variables (or 'predictors'). More specifically, regression analysis helps one understand
how the typical value of the dependent variable (or 'criterion variable') changes when any one of the
independent variables is varied, while the other independent variables are held fixed.
Regression analysis is widely used for prediction and forecasting, where its use has substantial
overlap with the field of machine learning. Regression analysis is also used to understand which
among the independent variables are related to the dependent variable, and to explore the forms of
these relationships. In restricted circumstances, regression analysis can be used to infer casual
relationships between the independent and dependent variables. However, this can lead to illusions or
false relationships, so caution is advisable.
Linear Regression is a linear approach for modelling the relationship between a scalar dependent
variable y and one or more explanatory variables (or independent variables) denoted X. The case of
one explanatory variable is called simple linear regression. For more than one explanatory variable,
the process is called multiple linear regression.
In linear regression, the relationships are modelled using linear predictor functions whose unknown
model parameters are estimated from the data. Such models are called linear models.
Logistic Regression, or logit regression, or logit model is a regression model where the dependent
variable (DV) is categorical. This article covers the case of a binary dependent variable—that is,
where the output can take only two values, "0" and "1", which represent outcomes such as pass/fail,
9
win/lose, alive/dead or healthy/sick. Cases where the dependent variable has more than two outcome
categories may be analysed in multinomial logistic regression, or, if the multiple categories are
ordered, in ordinal logistic regression. In the terminology of economics, logistic regression is an
example of a qualitative response/discrete choice model.
Polynomial regression is a form of regression analysis in which the relationship between the
independent variable x and the dependent variable y is modelled as an nth degree polynomial in x.
Polynomial regression fits a nonlinear relationship between the value of x and the corresponding
conditional mean of y, denoted E (y | x), and has been used to describe nonlinear phenomena such as
the growth rate of tissues, the distribution of carbon isotopes in lake sediments, and the progression
of disease epidemics.
Although polynomial regression fits a nonlinear model to the data, as a statistical estimation problem
it is linear, in the sense that the regression function E (y | x) is linear in the unknown parameters that
are estimated from the data.
Problem Definition
Diabetes is a common chronic disease and poses a great threat to human health. The characteristic of
diabetes is that the blood glucose is higher than the normal level, which is caused by defective insulin
secretion or its impaired biological effects, or both. Diabetes can lead to chronic damage and
dysfunction of various tissues, especially eyes, kidneys, heart, blood vessels and nerves. Hence, apart
from medicinal solutions some Data Science solution needs to be integrated for resolving the death
causing issue. This analysis aims to observe which features are most helpful in predicting diabetes
and to see general trends that may aid us in model selection and hyper parameter selection. The goal
is to classify whether the person is diabetic or not. To achieve this, machine learning classification
methods have been used to fit a function that can predict the discrete class of new input.
Diabetes is a common chronic disease and poses a great threat to human health. The characteristic of
diabetes is that the blood glucose is higher than the normal level, which is caused by defective insulin
secretion or its impaired biological effects, or both (Lonappan et al., 2007). Diabetes can lead to
chronic damage and dysfunction of various tissues, especially eyes, kidneys, heart, blood vessels and
nerves (Krasteva et al., 2011). Diabetes can be divided into two categories, type 1 diabetes (T1D) and
type 2 diabetes (T2D). Patients with type 1 diabetes are normally younger, mostly less than 30 years
old. The typical clinical symptoms are increased thirst and frequent urination, high blood glucose
levels (Iancu et al., 2008). This type of diabetes cannot be cured effectively with oral medications
alone and the patients are required insulin therapy. Type 2 diabetes occurs more commonly in
middle-aged and elderly people, which is often associated with the occurrence of obesity,
hypertension, dyslipidemia, arteriosclerosis, and other diseases (Robertson et al., 2011).
With the development of living standards, diabetes is increasingly common in people’s daily life.
Therefore, how to quickly and accurately diagnose and analyze diabetes is a topic worthy studying.
In medicine, the diagnosis of diabetes is according to fasting blood glucose, glucose tolerance, and
10
random blood glucose levels (Iancu et al., 2008; Cox and Edelman, 2009; American Diabetes
Association, 2012). The earlier diagnosis is obtained, the much easier we can control it. Machine
learning can help people make a preliminary judgment about diabetes mellitus according to their
daily physical examination data, and it can serve as a reference for doctors (Lee and Kim, 2016;
Alghamdi et al., 2017; Kavakiotis et al., 2017). For machine learning method, how to select the valid
features and the correct classifier are the most important problems.
Recently, numerous algorithms are used to predict diabetes, including the traditional machine
learning method (Kavakiotis et al., 2017), such as support vector machine (SVM), decision tree (DT),
logistic regression and so on. Polat and Günes (2007) distinguished diabetes from normal people by
using principal component analysis (PCA) and neuro fuzzy inference. Yue et al. (2008) used
quantum particle swarm optimization (QPSO) algorithm and weighted least squares support vector
machine (WLS-SVM) to predict type 2 diabetes Duygu and Esin (2011) proposed a system to predict
diabetes, called LDA-MWSVM. In this system, the authors used Linear Discriminant Analysis
(LDA) to reduce the dimensions and extract the features. In order to deal with the high dimensional
datasets, Razavian et al. (2015) built prediction models based on logistic regression for different
onsets of type 2 diabetes prediction. Georga et al. (2013) focused on the glucose, and used support
vector regression (SVR) to predict diabetes, which is as a multivariate regression problem. Moreover,
more and more studies used ensemble methods to improve the accuracy (Kavakiotis et al., 2017).
Ozcift and Gulten (2011) proposed a newly ensemble approach, namely rotation forest, which
combines 30 machine learning methods. Han et al. (2015) proposed a machine learning method,
which changed the SVM prediction rules.
Project Goal
The goal is to predict whether a person has diabetes or not. The goal is achieved by using logistic
regression in machine learning. With early detection of diabetes, controlling blood sugar through
diet, oral medication or insulin as the main treatment can treat it. Regular screening for complications
is also required.
Methodology
• Data Selection: Data is the foundation for any machine learning project. The job is to find
ways and sources of collecting relevant and comprehensive data, interpreting it, and analysing results
with the help of statistical techniques.
• Data Cleaning: This set of procedures allows for removing noise and fixing inconsistencies in
data. A data scientist can fill in missing data using imputation techniques. A specialist also detects
outliers — observations that deviate significantly from the rest of distribution.
• Data Splitting: A dataset used for machine learning should be partitioned into three subsets —
training, test, and validation sets.
• Model Selection: After a data scientist has pre-processed the collected data and split it into
three subsets, he or she can proceed with a model training. This process entails “feeding” the
11
algorithm with training data. An algorithm will process data and output a model that is able to find a
target value in new data. The purpose of model training is to develop a model.
• Model Evaluation: The goal of this step is to develop the simplest model that is able to
formulate a target value fast and well enough and check the accuracy.
Project Objective
The main objective of this project is Diabetes Detection using Machine Learning.
Project Workflow
The work architecture showing the process of Diabetic Detection using Machine Learning:
Data
Data Selection Data Cleaning
Visualization
Model
Model Testing Data Splitting
Evaluation
Project Implementation
• Data Selection: The process of selecting data depends on the type of project that is desired to
be created. The data set can be collected from various sources such as a file, database, sensor and
many other such sources.
• Data Visualization: Data visualization is the graphical representation of information and data.
By using visual elements like charts, graphs, and maps, data visualization tools provide an accessible
way to see and understand trends, outliers, and patterns.
• Data Pre-Processing: As it is known that data pre-processing is a process of cleaning the raw
data into clean data, so that it can be used to train the model. So, data preprocessing is definitely
required to achieve good results from the applied model in machine learning and deep learning
projects.
• Selection of Dependent and Independent Data: The dependent and independent data is
selected and stored in y and x.
12
• Splitting of the Data: The classifier is trained using ‘training data set’, then test the
performance of your classifier on unseen ‘test data set’. The data is split for training and testing by
using the ‘train_test_split’.
• Fitting into the Model: In a data set, a training set is implemented to build up a model. Once
the model is trained, the same can be used to train the model to predict using the testing data i.e., the
unseen data. Once this is done, a confusion matrix can be developed, which tells how well the model
is trained.
• Model Evaluation: It 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.
Tools Used
Support Vector Machine: A support vector machine (SVM) is a machine learning algorithm that uses
supervised learning models to solve complex classification, regression, and outlier detection problems by
performing optimal data transformations that determine boundaries between data points based on predefined
classes, labels, or outputs. SVMs are widely adopted across disciplines such as healthcare, natural language
processing, signal processing applications, and speech & image recognition fields.
Technically, the primary objective of the SVM algorithm is to identify a hyperplane that
distinguishably segregates the data points of different classes. The hyperplane is localized in such a
manner that the largest margin separates the classes under consideration.
The support vector representation is shown in the figure below:
13
SVMs Optimize Margin Between Support
Vectors or Classes
As seen in the above figure, the margin refers to
the maximum width of the slice that runs
parallel to the hyperplane without any internal
support vectors. Such hyperplanes are easier to
define for linearly separable problems; however,
for real-life problems or scenarios, the SVM
algorithm tries to maximize the margin between
the support vectors, thereby giving rise to
incorrect classifications for smaller sections of
data points.
SVMs are potentially designed for binary
classification problems. However, with the rise
in computationally intensive multiclass
problems, several binary classifiers are
constructed and combined to formulate SVMs
that can implement such multiclass
classifications through binary means.
In the mathematical context, an SVM refers to a set of ML Algorithms that use kernel methods to
transform data features by employing kernel functions. Kernel functions rely on the process of
mapping complex datasets to higher dimensions in a manner that makes data point separation easier.
The function simplifies the data boundaries for non-linear problems by adding higher dimensions to
map complex data points.
While introducing additional dimensions, the data is not entirely transformed as it can act as a
computationally taxing process. This technique is usually referred to as the kernel trick, wherein data
transformation into higher dimensions is achieved efficiently and inexpensively.
The idea behind the SVM algorithm was first captured in 1963 by Vladimir N. Vapnik and Alexey
Ya. Chervonenkis. Since then, SVMs have gained enough popularity as they have continued to have
wide-scale implications across several areas, including the protein sorting process, text categorization,
facial recognition, autonomous cars, robotic systems, and so on.
Decision Tree Classifier: A decision tree is a non-parametric supervised learning algorithm for
classification and regression tasks. It has a hierarchical tree structure consisting of a root node, branches,
internal nodes, and leaf nodes. Decision trees are used for classification and regression tasks, providing easy-
to-understand models.
A decision tree is a hierarchical model used in decision support that depicts decisions and their
potential outcomes, incorporating chance events, resource expenses, and utility. This algorithmic
model utilizes conditional control statements and is non-parametric, supervised learning, useful for
both classification and regression tasks. The tree structure is comprised of a root node, branches,
internal nodes, and leaf nodes, forming a
hierarchical, tree-like structure.
It is a tool that has applications spanning several
different areas. Decision trees can be used for
classification as well as regression problems. The
name itself suggests that it uses a flowchart like a
tree structure to show the predictions that result
from a series of feature-based splits. It starts with a
root node and ends with a decision made by leaves.
14
• Root Node: The initial node at the beginning of a decision tree, where the entire population or
dataset starts dividing based on various features or conditions.
• Decision Nodes: Nodes resulting from the splitting of root nodes are known as decision
nodes. These nodes represent intermediate decisions or conditions within the tree.
• Leaf Nodes: Nodes where further splitting is not possible, often indicating the final
classification or outcome. Leaf nodes are also referred to as terminal nodes.
• Sub-Tree: Similar to a subsection of a graph being called a sub-graph, a sub-section of a
decision tree is referred to as a sub-tree. It represents a specific portion of the decision tree.
• Pruning: The process of
removing or cutting down specific nodes
in a decision tree to prevent overfitting
and simplify the model.
• Branch / Sub-Tree: A subsection
of the entire decision tree is referred to as
a branch or sub-tree. It represents a
specific path of decisions and outcomes
within the tree.
• Parent and Child Node: In a
decision tree, a node that is divided into
sub-nodes is known as a parent node, and the sub-nodes emerging from it are referred to as
child nodes. The parent node represents a decision or condition, while the child nodes
represent the potential outcomes or further decisions based on that condition.
Gaussian NB: Naive Bayes is a basic but effective probabilistic classification model in machine learning that
draws influence from Bayes Theorem.
Bayes theorem is a formula that offers a conditional probability of an event A taking happening given
another event B has previously happened. Its mathematical formula is as follows: –
Where
• A and B are two events
• P(A|B) is the probability of event A provided event B has already happened.
• P(B|A) is the probability of event B provided event A has already happened.
• P(A) is the independent probability of A
• P(B) is the independent probability of B
Now, this Bayes theorem can be used to
generate the following classification
model –
Where
• X = x1,x2,x3,.. xN аre list of independent predictors
• y is the class label
• P(y|X) is the probability of label y given the predictors X
The above equation may be extended as follows:
15
• Naive Bayes is often used for text categorization since the dimensionality of the data is
frequently rather large.
Types of Naive Bayes Classifiers
Naive Bayes Classifiers are classified into three categories —
i) Gaussian Naive Bayes
This classifier is employed when the predictor values are continuous and are expected to follow a
Gaussian distribution.
ii) Bernoulli Naive Bayes
When the predictors are Boolean in nature and are supposed to follow the Bernoulli distribution, this
classifier is utilized.
iii) Multinomial Naive Bayes
This classifier makes use of a multinomial distribution and is often used to solve issues involving
document or text classification.
Codes
Data Collection
diabetes_dataset = pd.read_csv("[Link]")
df = pd.read_csv("[Link]")
Data Exploration
[Link]()
diabetes_dataset.head()
diabetes_dataset.shape
diabetes_dataset.describe()
diabetes_dataset["Outcome"].value_counts()
diabetes_dataset.groupby("Outcome").mean()
[Link]().sum()
Data Analysis
[Link](figsize=(8,7))
[Link]('Age', fontsize=10)
[Link]('Count', fontsize=10)
diabetes_dataset['Age'].hist(edgecolor="black")
fig,ax = [Link](4,2, figsize=(20,20))
[Link](diabetes_dataset.Pregnancies, bins=20, ax=ax[0,0], color="red")
[Link](diabetes_dataset.Glucose, bins=20, ax=ax[0,1], color="red")
[Link](diabetes_dataset.BloodPressure, bins=20, ax=ax[1,0], color="red")
[Link](diabetes_dataset.SkinThickness, bins=20, ax=ax[1,1], color="red")
[Link](diabetes_dataset.Insulin, bins=20, ax=ax[2,0], color="red")
[Link](diabetes_dataset.BMI, bins=20, ax=ax[2,1], color="red")
[Link](diabetes_dataset.DiabetesPedigreeFunction, bins=20, ax=ax[3,0], color="red")
[Link](diabetes_dataset.Age, bins=20, ax=ax[3,1], color="red")
[Link]()
[Link]([Link](), annot = True, fmt = ' 0.1f' , linewidth = .5)
[Link](x = 'Outcome', data = df , palette = ['g','r'])
[Link]([Link])
Data Standardization
x = diabetes_dataset.drop(columns = "Outcome", axis=1)
df1 = diabetes_dataset.drop(columns = "Outcome", axis=1)
y = diabetes_dataset["Outcome"]
print(x)
17
print(y)
scaler = StandardScaler()
[Link](x)
standardized_data = [Link](x)
x = standardized_data
Train-Test Split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, stratify=y,
random_state=2)
Model Training
model_1 = LogisticRegression()
model_2 = SVC()
model_3 = DecisionTreeClassifier()
model_4 = GaussianNB()
model_5 = KNeighborsClassifier()
coloumns = ['LogisticRegression', 'SVC', 'DecisionTreeClassifier', 'GaussianNB',
'KNeighboursClassifier']
resultOfAccu = []
resultOfRecall = []
resultOfF1 = []
def call(model):
[Link](x_train, y_train)
pre = [Link](x_test)
accuracy = accuracy_score(pre , y_test)
recall = recall_score(pre , y_test)
f1 = f1_score(pre,y_test)
[Link](accuracy)
[Link](recall)
[Link](f1)
[Link](confusion_matrix(pre,y_test),annot = True)
print(model)
print('accuracy is:', accuracy, 'recall is:', recall , 'f1 is:', f1)
call(model_1)
call(model_2)
call(model_3)
call(model_4)
call(model_5)
18
comp = [Link]({'Algorithms':coloumns,'Accuracies':
resultOfAccu,'Recall':resultOfRecall,'F1':resultOfF1})
comp
fig,ax = [Link](figsize=(20,5))
[Link]([Link],resultOfAccu, label = 'Accuracy')
[Link]([Link],resultOfRecall, label = 'Recall')
[Link]([Link],resultOfF1,label = 'F1Score')
[Link]()
[Link]()
19
Step-by-step Working
20
21
22
23
24
25
26
27
Project Limitations
• This project mainly focuses on the algorithmic aspect of the system and thus the parts of the
program that govern the visualization of the data can be improved further which could result in more
realistic look and focus on user experience. And the dataset we use that only contains the female
records there is no male data.
Future Scope
In this python project, a diabetic predictor was built on the Kaggle dataset. It has been observed that a
good dataset provides better accuracy. Selection of appropriate algorithms with good home dataset
will lead to the development of prediction systems. These systems can assist in proper treatment
methods for a patient diagnosed with diabetes. There are many treatments for a patient based on
diabetes; data mining and machine learning can be a very good help in deciding the line of treatment
to be followed by extracting knowledge from such suitable databases.
Summary
Logistic Regression is a powerful Machine Learning tool, and can be used for successfully for
predicting categorical outputs of biomedical data. Data wrangling and data mining can benefit from
excellent performances offered by Python and its libraries so well supported by the community.
Linear Algebra programming has intrinsic advantages in avoiding, where possible, ‘while’ and ‘for’
loops. It is implementable by NumPy, a package that vectorizes the matrixes. NumPy makes working
on them more comfortable, and guarantees better control over the operations, especially for large
arrays.
Moreover, the Machine Learning scenario with Python is enriched by the presence of many powerful
packages (i.e., Scikit-learn,) which provide excellently optimized classifications and predictions on
data.
The Diagnostic Data Set, with its 768 patients and 9 features, offers an exhaustive assortment of
parameters for classification and for this reason represents a perfect example for Machine Learning
applications. Anyway, many of these features seem to be redundant, and a definite impact on
classification and prediction by some of them remains still unknown.
Bibliography
• [Link]
• [Link]
• [Link]
arning%20methods%20are%20widely,forest%20generates%20many%20decision %20trees
28
29