How to Build Your First Neural Network using KERAS

In this tutorial, we will be learning to build a neural network using Keras library. Keras is a simple tool used to construct neural networks.

There will be the following sections :

  1.  Importing Libraries
  2.  Importing Dataset
  3. Data Preprocessing
  4.  Building a 2-layered model
  5.  Training the Model on the dataset
  6.  Predicting the test results
  7.  Confusion matrix and Performance of the model

 

WHAT IS A NEURAL NETWORK?

A system of neurons connected to each other is a neural network. It’s a set or series of algorithms that recognize the relationships in a set of data.
Feedforward neural network, recurrent neural network, convolutional neural networks are some types of neural networks.

Building A Neural Network using KERAS

We will build a 2-layered neural network which is the simplest one and great to be your first neural network. It means an input layer, one hidden layer, and an output layer will be there. The index always starts from the first hidden layer.

Note: You need to ensure that the libraries are installed on your computer. Or, you can also work on google colab.

1. IMPORTING LIBRARIES

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

Pandas: A python package which is a fast, powerful, and open-source data manipulation tool.
Numpy: A python package used for scientific computing.
Matplotlib: A library used to create visualizations in python.
LabelEncoder: LabelEncoder is a class that helps in encoding the labeled data.
ColumnTransformer: A class that helps us to select the columns undergoing transformations.
OneHotEncoder: A class that helps in encoding more than two labeled data.
train_test_split: This splits our dataset into training and testing datasets.
StandardScaler: Used for feature scaling.

So, these are the initial libraries you need to have. You can find documentation of pandas, numpy, matplotlib, and sklearn libraries at the end of this tutorial.

 

2. IMPORTING DATASET

dataset = pd.read_csv('your_dataset')
X = dataset.iloc[:, :].values
y = dataset.iloc[:, ].values

The above is the template to import a dataset and distribute it into X and y values. X is containing all the features which we tend to use for training(for ease it means the factors which lead to prediction). And y will contain the predictive value.

I will be using a sample bank dataset.

So, my code will be:

dataset = pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, -1].values

To see your dataset:

 

dataset.head()

 

3. DATA PREPROCESSING

Encoding Categorical Data: In my dataset, ‘gender’ and ‘geography’ need to be encoded.

For encoding gender category which has male and female as two labels, we use LabelEncoder.

label_encoding = LabelEncoder()
X[:, 2] = label_encoding.fit_transform(X[:, 2])
print(X)

OUTPUT:

[[619 'France' 0 ... 1 1 101348.88]
 [608 'Spain' 0 ... 0 1 112542.58]
 [502 'France' 0 ... 1 0 113931.57]
 ...
 [709 'France' 0 ... 0 1 42085.58]
 [772 'Germany' 1 ... 1 0 92888.52]
 [792 'France' 0 ... 1 0 38190.78]]

Now, to encode geography category which has France, Spain, and Germany as the labels, we use OneHotEncoder.

one_hot_encoding = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [1])], remainder='passthrough')
X = np.array(one_hot_encoding.fit_transform(X))
print(X)

OUTPUT:

[[1.0 0.0 0.0 ... 1 1 101348.88]
 [0.0 0.0 1.0 ... 0 1 112542.58]
 [1.0 0.0 0.0 ... 1 0 113931.57]
 ...
 [1.0 0.0 0.0 ... 0 1 42085.58]
 [0.0 1.0 0.0 ... 1 0 92888.52]
 [1.0 0.0 0.0 ... 1 0 38190.78]]

Splitting the dataset into train and test datasets:

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

Thus, the dataset will be split as an 80% training dataset and a 20% test dataset.

Feature Scaling:

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train)

OUTPUT:

[[-1.01460667 -0.5698444   1.74309049 ...  0.64259497 -1.03227043
   1.10643166]
 [-1.01460667  1.75486502 -0.57369368 ...  0.64259497  0.9687384
  -0.74866447]
 [ 0.98560362 -0.5698444  -0.57369368 ...  0.64259497 -1.03227043
   1.48533467]
 ...
 [ 0.98560362 -0.5698444  -0.57369368 ...  0.64259497 -1.03227043
   1.41231994]
 [-1.01460667 -0.5698444   1.74309049 ...  0.64259497  0.9687384
   0.84432121]
 [-1.01460667  1.75486502 -0.57369368 ...  0.64259497 -1.03227043
   0.32472465]]

With this data preprocessing phase is also completed. Importing dataset is also a part of data preprocessing.

 

4. BUILDING A 2-LAYERED MODEL

First, we need to import keras and other modules.

import keras 
from keras.models import Sequential
from keras.layers import Dense

Now, we will construct a model with one input layer, one hidden layer, and an output layer.

model = Sequential()
model.add(Dense(units = 5, activation = 'relu'))
model.add(Dense(units = 5, activation = 'relu'))
model.add(Dense(units = 1, activation = 'sigmoid'))

Next, we will compile the model.

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

There are other optimizers present. We can select as per our need but, ‘adam’ is considered the most efficient.

Thus, the model has been constructed.

 

5. TRAINING THE MODEL ON THE DATASET

To train our model with the dataset, we will pass X_train and y_train into the fit() function. And I am giving number of epochs as 50.

model.fit(X_train, y_train, batch_size = 10, epochs = 50)

OUTPUT:

Epoch 1/50
8000/8000 [==============================] - 1s 145us/step - loss: 0.5153 - accuracy: 0.7846
Epoch 2/50
8000/8000 [==============================] - 1s 102us/step - loss: 0.4486 - accuracy: 0.8070
Epoch 3/50
8000/8000 [==============================] - 1s 102us/step - loss: 0.4302 - accuracy: 0.8174
Epoch 4/50
8000/8000 [==============================] - 1s 102us/step - loss: 0.4226 - accuracy: 0.8196
Epoch 5/50
8000/8000 [==============================] - 1s 104us/step - loss: 0.4153 - accuracy: 0.8259
Epoch 6/50
8000/8000 [==============================] - 1s 104us/step - loss: 0.4022 - accuracy: 0.8334
Epoch 7/50
8000/8000 [==============================] - 1s 109us/step - loss: 0.3865 - accuracy: 0.8435
Epoch 8/50
8000/8000 [==============================] - 1s 125us/step - loss: 0.3746 - accuracy: 0.8481
Epoch 9/50
8000/8000 [==============================] - 1s 109us/step - loss: 0.3658 - accuracy: 0.8512
Epoch 10/50
8000/8000 [==============================] - 1s 111us/step - loss: 0.3598 - accuracy: 0.8518
Epoch 11/50
8000/8000 [==============================] - 1s 111us/step - loss: 0.3556 - accuracy: 0.8524
Epoch 12/50
8000/8000 [==============================] - 1s 112us/step - loss: 0.3521 - accuracy: 0.8535
Epoch 13/50
8000/8000 [==============================] - 1s 112us/step - loss: 0.3492 - accuracy: 0.8550
Epoch 14/50
8000/8000 [==============================] - 1s 112us/step - loss: 0.3481 - accuracy: 0.8577
Epoch 15/50
8000/8000 [==============================] - 1s 111us/step - loss: 0.3464 - accuracy: 0.8580
Epoch 16/50
8000/8000 [==============================] - 1s 113us/step - loss: 0.3454 - accuracy: 0.8585
Epoch 17/50
8000/8000 [==============================] - 1s 111us/step - loss: 0.3440 - accuracy: 0.8609
Epoch 18/50
8000/8000 [==============================] - 1s 111us/step - loss: 0.3433 - accuracy: 0.8596
Epoch 19/50
8000/8000 [==============================] - 1s 104us/step - loss: 0.3431 - accuracy: 0.8600
Epoch 20/50
8000/8000 [==============================] - 1s 106us/step - loss: 0.3427 - accuracy: 0.8594
Epoch 21/50
8000/8000 [==============================] - 1s 104us/step - loss: 0.3417 - accuracy: 0.8602
Epoch 22/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3418 - accuracy: 0.8593
Epoch 23/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3417 - accuracy: 0.8599
Epoch 24/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3409 - accuracy: 0.8608
Epoch 25/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3410 - accuracy: 0.8604
Epoch 26/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3407 - accuracy: 0.8610
Epoch 27/50
8000/8000 [==============================] - 1s 110us/step - loss: 0.3403 - accuracy: 0.8601
Epoch 28/50
8000/8000 [==============================] - 1s 118us/step - loss: 0.3401 - accuracy: 0.8629
Epoch 29/50
8000/8000 [==============================] - 1s 120us/step - loss: 0.3403 - accuracy: 0.8599
Epoch 30/50
8000/8000 [==============================] - 1s 118us/step - loss: 0.3398 - accuracy: 0.8597
Epoch 31/50
8000/8000 [==============================] - 1s 117us/step - loss: 0.3393 - accuracy: 0.8612
Epoch 32/50
8000/8000 [==============================] - 1s 119us/step - loss: 0.3391 - accuracy: 0.8608
Epoch 33/50
8000/8000 [==============================] - 1s 114us/step - loss: 0.3393 - accuracy: 0.8591
Epoch 34/50
8000/8000 [==============================] - 1s 104us/step - loss: 0.3391 - accuracy: 0.8596
Epoch 35/50
8000/8000 [==============================] - 1s 105us/step - loss: 0.3386 - accuracy: 0.8593
Epoch 36/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3386 - accuracy: 0.8618
Epoch 37/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3383 - accuracy: 0.8614
Epoch 38/50
8000/8000 [==============================] - 1s 101us/step - loss: 0.3383 - accuracy: 0.8619
Epoch 39/50
8000/8000 [==============================] - 1s 105us/step - loss: 0.3378 - accuracy: 0.8597
Epoch 40/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3380 - accuracy: 0.8621
Epoch 41/50
8000/8000 [==============================] - 1s 102us/step - loss: 0.3378 - accuracy: 0.8625
Epoch 42/50
8000/8000 [==============================] - 1s 104us/step - loss: 0.3370 - accuracy: 0.8625
Epoch 43/50
8000/8000 [==============================] - 1s 102us/step - loss: 0.3374 - accuracy: 0.8620
Epoch 44/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3376 - accuracy: 0.8618
Epoch 45/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3372 - accuracy: 0.8618
Epoch 46/50
8000/8000 [==============================] - 1s 105us/step - loss: 0.3371 - accuracy: 0.8619
Epoch 47/50
8000/8000 [==============================] - 1s 104us/step - loss: 0.3371 - accuracy: 0.8622
Epoch 48/50
8000/8000 [==============================] - 1s 103us/step - loss: 0.3372 - accuracy: 0.8620
Epoch 49/50
8000/8000 [==============================] - 1s 105us/step - loss: 0.3369 - accuracy: 0.8620
Epoch 50/50
8000/8000 [==============================] - 1s 107us/step - loss: 0.3366 - accuracy: 0.8624
<keras.callbacks.callbacks.History at 0x7f86f2078be0>

Thus, we have trained our model with our dataset.

 

6. PREDICTING THE TEST RESULT

y_predicted = model.predict(X_test)
y_predicted = (y_predicted > 0.5)
print(y_predicted)

Thus, we have used predict function to predict the test data results.

OUTPUT:

[[False]
 [False]
 [False]
 ...
 [False]
 [False]
 [False]]

7. CONFUSION MATRIX AND PERFORMANCE OF THE MODEL

A confusion matrix is a table that helps us to understand the performance of our model.

from sklearn.metrics import confusion_matrix
find_cm = confusion_matrix(y_test, y_predicted)
print(find_cm)

OUTPUT:

[[1513   82]
 [ 191  214]]

ACCURACY: (find_cm[0][0] + find_cm[1][1]) / (find_cm[0][0] + find_cm[0][0] + find_cm[0][0] + find_cm[0][0])

accuracy = (1727 / 2000)
print(accuracy)

OUTPUT:

0.8635

Accuracy is the classification rate.  (True positives + False negatives) / Sum of all elements

RECALL: (find_cm[0][0]) / (find_cm[0][0] + find_cm[0][1])

recall = 1513 / 1595
print(recall)

OUTPUT:

0.94858934169279

High recall gives the intuition that class has been correctly recognized. (True positives) / (True positives + True negatives)

PRECISION: (find_cm[0][0]) / (find_cm[0][0] + find_cm[1][0])

precision = 1513 / 1704
print(precision)

OUTPUT:

0.8879107981220657

High Precision indicates an example classified as positive is indeed positive. (True positives) / (True positives + False positives)

Thus, we have built our neural network.

DOCUMENTATIONS:

Also, you may visit:

Thank you for reaching till here.

 

 

Leave a Reply

Your email address will not be published.