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 :
- Importing Libraries
- Importing Dataset
- Data Preprocessing
- Building a 2-layered model
- Training the Model on the dataset
- Predicting the test results
- 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:
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