Honours* in Data Science #Fourth year of Engineering (Semester VII) #410502:
Machine Learning and Data Science Laboratory
Dr. Girija Gireesh Chiddarwar
Assignment No 1 - Creating & Visualizing Neural Network for the given data. (Use
python)
Note: download dataset using Kaggal. Keras, ANN visualizer, graphviz libraries are
equired.
Problem Statement in detail-
Creating & Visualizing Neural Network
Neural networks, also known as artificial neural networks (ANNs) or simulated
neural networks (SNNs), are a subset of machine learning and are at the heart
of deep learning algorithms.
Their name and structure are inspired by the human brain, mimicking the way that
biological neurons signal to one another.
Artificial neural networks (ANNs) are comprised of a node layers, containing an
input layer, one or more hidden layers, and an output layer.
Each node, or artificial neuron, connects to another and has an associated weight
and threshold.
If the output of any individual node is above the specified threshold value, that
node is activated, sending data to the next layer of the network.
Otherwise, no data is passed along to the next layer of the network.
Neural networks are used for solving many business problems such as sales
forecasting, customer research, data validation, and risk management.
Download dataset using Kaggal -
For ANN creation and visualization we are using pima-indians-diabetes.cvs dataset
This dataset describes the medical records for Pima Indians and whether or not each
patient will have an onset of diabetes within coming years.
Fields description follow:
preg = Number of times pregnant
plas = Plasma glucose concentration a 2 hours in an oral glucose tolerance test
pres = Diastolic blood pressure (mm Hg)
skin = Triceps skin fold thickness (mm)
test = 2-Hour serum insulin (mu U/ml)
mass = Body mass index (weight in kg/(height in m)^2)
pedi = Diabetes pedigree function
age = Age (years)
class = Class variable (1:tested positive for diabetes, 0: tested negative for
diabetes)
Libraries
Keras -It is an open-source software library that provides a Python interface for
artificial neural networks.
ANN Visualizer- It is a python library that enables us to visualize an Artificial
Neural Network using just a single line of code.
Graphviz -To create a neat and presentable graph of the neural network.
Problem Statement in detail-
Algorithm -
Install required library – keras, ann_visualizer, graphviz
Load pima indians dataset.
Fix random seed for reproducibility.
Split into input (X) and output (Y) variables.
Create Model
Compile Model
Fit the Model
Evaluate the Model
Visualize the model
Program
# Use Google Colab
#install ann visualizer#!pip3 install ann_visualizer##Load dataset in sample_data
folder , path is /content/sample_data/pima-indians-diabetes.csv## Create your first
MLP in Keras
from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
numpy.random.seed(7)
# load pima indians dataset
dataset = numpy.loadtxt("/content/sample_data/pima-indians-diabetes.csv",
delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
#
Program
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10)
# evaluate the model
scores = model.evaluate(X, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))##Visualize the
model
#Results are stored in /content/sample_data folder with name network.gv.pdf#from
ann_visualizer.visualize import ann_viz;
ann_viz(model, title="My first neural network")
#
Output
## Use Google Colab
#install ann visualizer
#ip3 install ann_visualizer!p
##Load dataset in sample_data folder , path is /content/sample_data/pima-indians-
diabetes.csv
## Create your first MLP in Keras
from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
numpy.random.seed(7)
# load pima indians dataset
dataset = numpy.loadtxt("/content/sample_data/pima-indians-diabetes.csv",
delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10)
# evaluate the model
scores = model.evaluate(X, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
##Visualize the model
#Results are stored in /content/sample_data folder with name network.gv.pdf
#from ann_visualizer.visualize import ann_viz;
ann_viz(model, title="My first neural network")
#