#### Latest posts by Prasad Kharkar (see all)

- Linear Discriminant Analysis using Python - April 30, 2018
- Principal Component Analysis using Python - April 30, 2018
- artificial neural network using keras - April 22, 2018

We have been learning about neural networks for a last few articles and now we have come to final step. We will create a simple artificial neural network using keras.

# Artificial Neural Network using Keras:

We need theano, tensorflow and keras libraries to create artifical neural networks.

### Installing Theano:

Theano is capable of evaluating complicated mathematical expressions and running on graphical processing units. Install theano by running below command on terminal.

pip install –upgrade –no-deps git+git://github.com/Theano/Theano.git

It will download theano from git and install it.

### Installing TensorFlow:

Tensorflow facilitates machine learning and deep learning with its extensive support for numerical computations. Installation instructions are given on tensorflow official page. Please install it in a way most suitable for you.

### Installing Keras:

Keras implements deep learning concepts using python and uses theano and tensorflow under the hood. We can directly create artificial neural networks using keras because it wraps all the complicated things. Run below command on below terminal to install.

pip install –upgrade keras

## The Problem Statement:

We have a dataset of customers of a bank. It consists of customerId, surname, creditScore, geography, gender,age, tenure, balance, numberOfProducts, hasCreditCard, isActiveMember, estimatedSalary and whether is exited the bank. Based on the data we want to predict whether a customer is going to leave the bank or not. This is a classification problem.

Please download dataset for artificial neural network using keras from https://superdatascience.com/machine-learning/.

### Read dataset:

We are simply going to read the downloaded dataset **churn_modelling.csv.**

1 2 3 4 |
import pandas as pd dataset = pd.read_csv('Churn_Modelling.csv') X = dataset.iloc[:, 3:13].values y = dataset.iloc[:, 13].values |

Note that we are picking only relevant independent variables from dataset and storing in **X** matrix. We omitted columns 0,1 and 2 from dataset as rowNumber, customerId and surname are not really important for deciding whether the customer will leave the bank or not.

**y** matrix stores whether customer left the bank or not.

## Data Preprocessing:

Note that customer information contains categorical variables **gender** and **geography. **We need to encode them and convert into numbers as discussed in data preprocessing article .

1 2 3 4 5 6 7 8 |
from sklearn.preprocessing import LabelEncoder, OneHotEncoder labelencoder_X_1 = LabelEncoder() X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1]) labelencoder_X_2 = LabelEncoder() X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2]) onehotencoder = OneHotEncoder(categorical_features = [1]) X = onehotencoder.fit_transform(X).toarray() X = X[:, 1:] |

### Create Training and Test Set:

We have information of 10000 customers. Let us use 90% data as training set and 10% data as test set.

1 2 |
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 0) |

### Perform Feature Scaling:

1 2 3 4 |
from sklearn.preprocessing import StandardScaler sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) |

## Artificial Neural Network Using Keras:

### Import Keras Libraries:

1 2 3 |
import keras from keras.models import Sequential from keras.layers import Dense |

- Sequential model is a liner structure of layers of artificial neural network.
- Dense is an actual neural network layer

### Create Neural Network:

1 2 3 4 5 |
classifier = Sequential() classifier.add(Dense(output_dim = 12, activation = 'relu', input_dim = 11)) classifier.add(Dense(output_dim = 12, activation = 'relu')) classifier.add(Dense(output_dim = 1, activation = 'sigmoid')) classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) |

- We initialized an artificial neural network using keras Sequential class and stored in
**classifier**. - Then created a
**Dense**input layer with- output_dim as 12 ( The number 12 came from simply adding 11 input variables and 1 output variable. There is not strict rule for output_dim parameter ),
- activation function as
**‘relu’**(It is actually a rectifier function and generally applied to input and hidden network layers ) and - input_dim = 11 because we have 11 independent input variables in X matrix.
- added this layer to neural network using
**classifier.add()**

- Then created another Dense hidden layer with similar parameters without
**input_dim**(because input_dim parameter is provided to only input layer) and added this hidden layer using**classifier.add()** - Then created an output layer with
**output_dim**as 1 and sigmoid activation function (because generally it is applied for output layers) and added it to network by**classifier.add().** - Then compiled this neural network using ‘adam’ optimizer, ‘binary_crossentropy’ loss function for
**accuracy**metrics

That’s it. We have created a neural network, added an input layer, a hidden layer and an output layer to it and compiled it. Now its time for training and making predictions.

## Train ANN model:

1 2 3 |
classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100) y_pred = classifier.predict(X_test) y_pred = (y_pred > 0.5) |

**classifier.fit() **method trains the model with 9000 rows in batch size of 10 rows each. Once the model is trained with all 9000 rows, one epoch is completed. As we have provided **nb_epoch = 100** . Our model will be trained 100 times in batches of 10 rows on 9000 rows. As explained in previous articles, different weights will be assigned to neural networks using backpropagation so that optimal cost function value is found.

** **Note that our model calculates probability of a customer leaving bank. We are deciding a customer will leave bank if probability is more than 0.5 or he stays with bank if probability is less than or equal to 0.5.

### Execute the Program:

Run program and you should see that 100 iterations are being performed as below.

Epoch 97/100

9000/9000 [==============================] – 2s 202us/step – loss: 0.3173 – acc: 0.8676

Epoch 98/100

9000/9000 [==============================] – 2s 216us/step – loss: 0.3171 – acc: 0.8692

Epoch 99/100

9000/9000 [==============================] – 2s 210us/step – loss: 0.3176 – acc: 0.8684

Epoch 100/100

9000/9000 [==============================] – 2s 205us/step – loss: 0.3172 – acc: 0.8694

Note that accuracy is changing for each epoch.

### Confusion Matrix:

1 2 |
from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, y_pred) |

Use confusion matrix to compare test set against predictions.

We ran predictions on 1000 rows and out of them (765 + 101) = 866 results are correct. So our accuracy for artificial neural network using keras is 86.6 %.

## 1 comment for “artificial neural network using keras”