How to build your first Neural Network in Python

Pranay
May 22, 2020  ยท  4794 views

A beginner guide to learn how to build your first Artificial Neural Networks with Python, Keras, Tensorflow without any prior knowledge of building deep learning models.

Prerequisite: Basic knowledge of any programming language to understand the Python code. You need not to be aware of what machine learning and neural networks are about.

What are Artificial Neural Networks

Machine Learning:

As per Wikipedia, Machine learning (ML) is the study of computer algorithms that improve automatically through experience. It is seen as a subset of artificial intelligence. Machine learning algorithms build a mathematical model based on sample data, known as "training data", in order to make predictions or decisions without being explicitly programmed to do so.

In quest of such algorithms and inspired by the biological brain there are some computational systems developed which simulate how a biological brain works. These are build based on the concept of how Neurons are build and function with the brain. These systems are called as Artificial Neural Networks.

Neural Network:

As per Wikipedia, Artificial neural networks (ANN) or connectionist systems are computing systems vaguely inspired by the biological neural networks that constitute animal brains. Such systems "learn" to perform tasks by considering examples, generally without being programmed with task-specific rules.

Setup environment for Artificial Neural Networks

In building our first Artificial Neural Network, we are using the following tools and libraries. Here is the complete tutorial on how to setup

  1. Python with Anaconda distribution (with Spyder IDE)
  2. Tensorflow
  3. Keras
  4. Python libraries like Scikit-learn, Pandas, numPy

Artificial neural network python code

Dataset & problem

We are going to use the Bank Turnover Dataset and predict if a customer would leave the bank in the next 6 months. This is a classification problem as we need to predict a boolean value i.e., Yes/No if a customer leaves the bank.

Steps to create Artificial Neural Network

Before going through these steps, first setup your deep learning environment using the steps here - How to setup deep learning environment

These are the most common steps in building any neural network using Python, Tensorflow and Keras. Following these we shall build the model in Python

  1. Data pre-processing
  • Import libraries
  • Import dataset
  • Encoding the categorical data
  • Splitting the date set to test and train data
  • Feature scaling
  1. Building Artificial Neural Network (ANN)
  • Importing libraries/packages
  • Initializing ANN
  • Adding first hidden layer and the input layer
  • Adding more hidden layers
  • Adding output layers
  • Build and run the ANN
  1. Evaluate the model
  • Predict test results
  • Building confusion Matrix

1. Data pre-processing steps:

Importing libraries

This is a simple step to include all libraries that you want to import to your model/program. This is link using/importing packages to your program in any other language.

So here, we are importing some basic Python libraries. It is not all required to import all your libraries initially. We can also import libraries somewhere middle in the code.

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

Import dataset###

Dataset is the data using which our model is going to learn the patterns in the data. Based on the learning our model would be able to predict the outcome of new data. The Dataset would always have the outcome mentioned for each record and this outcome is used for building the model.

If we see the Dataset in our example, its just a csv file with some rows and columns. The last or one of the column would be outcome and each row in the file is one instance/occurrence. By looking at each row in the sheet we can understand that there was so and so outcome for so and so inputs.

In the code below we have had the inputs in X and the outcomes in Y.

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

Encoding the categorical data

# Encoding categorical data
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(n_features = [1]) #To avoid dummy variable trap
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]

Splitting the date set to test and train data

To check the performance of our model, we should have some data to check the accuracy of the predictions. For this purpose, the entire Dataset is split into Training set and Validation sets. Mostly with 80% of the data considered for training data and 20% for Validating the data.

# Splitting the dataset into the Training and Test dataset
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

Feature scaling

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

2. Building Artificial Neural Network (ANN)

Importing libraries/packages###

# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

Initializing ANN

# Initialising the ANN
classifier = Sequential()

Adding first hidden layer and the input layer

# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 11))

Adding more hidden layers

# Adding the second hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))

###Adding output layers###

#Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))

Build and run the ANN

# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)

##3. Evaluate the model ##

###Predict test results ###

# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)

Building confusion Matrix

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

Note: If you have made this far then probably you are pretty serious in learning and building Deep Learning models. For a more detailed tutorial I strongly suggest to go through this course on Deep Learning - Complete Deep Learning course for beginners from Udemy

Complete Python code to build the model

# Part 1 - Data Preprocessing

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

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

# Encoding categorical data
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(n_features = [1]) #To avoid dummy variable trap
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Part 2 - Now let's make the ANN!

# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 11))

# Adding the second hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))

# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))

# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)

# Part 3 - Making predictions and evaluating the model

# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
AUTHOR

Pranay

A Software Engineer by profession, a part time blogger and an enthusiast programmer. You can find more about me here.


Post a comment




Thank you! You are now subscribed.

Sign up for our newsletter

Subscribe to receive updates on our latest posts.

Thank you! You are now subscribed.