Example of Neural Network in Python With Keras (N.1)

Tiba Razmi
3 min readMar 3, 2019
Photo by Russ Sanderlin

The Keras library in Python makes building and testing neural networks a snap. It provides a simpler, quicker alternative to Theano or TensorFlow–without worrying about floating point operations, GPU programming, linear algebra, etc. The Keras API should seem familiar for anyone who’s worked with the well-known and well-loved scikit-learn API. To start our program we are going to follow these steps:

0. Installing Keras with TensorFlow backend:

  1. Loading Data:

In this case, data is from Pima Indians onset of diabetes dataset. First, we need to study our dataset to understand the characterization of data.

A. Number of times pregnant

B. Plasma glucose concentration a 2 hours in an oral glucose tolerance test

C. Diastolic blood pressure (mm Hg)

D. Triceps skin fold thickness (mm)

E. 2-Hour serum insulin (mu U/ml)

F. Body mass index (weight in kg/(height in m)²)

G. Diabetes pedigree function

H. Age (years)

1. Class variable (0 or 1)

For this problem we want to see whether the patient had an onset of diabetes or not (1 or 0) which is binary classification.

#load dataset
dataset = np.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (I) and output (O) variables
I = dataset[:,0:8]
O = dataset[:,8]

2. Defining Model:

model = Sequential()

Model in Keras is Sequential model which is a linear stack of layers.

input_dim=8

The first thing we need to get right is to ensure that the input layer has the right number of inputs. Here we can specify that in the first layer by input_dim argument and set it to 8 (number of input variable).

model.add(Dense())

Fully connected layers are defined using the Dense class.

model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))

3. Compile Model.

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

4. Fit Model.

model.fit(X, Y, validation_split=0.33, nb_epoch=150, batch_size=10)

5. Evaluate Model.

Evaluating the performance of the model on the dataset using evaluate function.

scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

Final code all together:

# Create your first MLP in Kerasfrom keras.models import Sequentialfrom keras.layers import Denseimport numpy# fix random seed for reproducibilityseed = 7numpy.random.seed(seed)
# load pima indians datasetdataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")# split into input (X) and output (Y) variablesX = dataset[:,0:8]Y = dataset[:,8]# create modelmodel = Sequential()model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))model.add(Dense(8, init='uniform', activation='relu'))model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile modelmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])# Fit the modelmodel.fit(X, Y, validation_split=0.33, nb_epoch=150, batch_size=10)# evaluate the modelscores = model.evaluate(X, Y)print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))# for me I got acc: 76.69%

References:

  1. Deep learning with Python ebook by Jason Brownlee.

--

--