Churn Modelling — Artificial Neural Network (ANN)

as in the previous article, I have given you an introduction to Artificial Neural Network (ANN) now I will tell you how to make a basic ANN model in this article with some lines of codes.

In this article, imagine we are working for a bank and we have a task to predict whether a person will close his/her account with the bank or not. This allows us to know who will leave so we can pay more attention to them and devise better schemes to ensure they do not close their account with the bank.

If you want to learn more about Artificial Neural Network (ANN), follow this link -

So let’s get started. The first step is to get the dataset and then apply it to the model. You can download or copy data from the URLhttps://raw.githubusercontent.com/aviralb13/codes/main/datas/Churn_Modelling.csv

Importing the libraries

Now we will import pandas, NumPy, and Tensorflow as shown below. If your system does not have these libraries installed, you may get them using the pip command.

Data preparation

Now we’ll read the data using Pandas and save it in a variable called data so we don’t have to call it again and again. Using the head command, we can view the first 5 components of the data; if you want to see more, enter the number inside the bracket.

Defining X and Y

We will now define two variables, x and y. We will store the individual’s credit score, geography, gender, age tenure, balance, number of items, has credit card, is active member, and estimated salary in the x column, and whether or not the person has exited in the y column. Using iloc (here in iloc we have done simple slicing ie we do in lists and strings).

I believe that these x parameters are more appropriate, and if you want to modify the parameters because you believe they are relevant, you can do so.

Encoding

Because certain data columns are strings (words), we must first transform them to integers using encoding methods.

As a result, we must encode two columns (Geography and Gender)

First, we’ll encode gender using label encoding, which will assign female values to 0 and male values to 1.

and second we will encode Geography by one hot encoding For example, in one heated encoding, the country France Germany and Spanish will have three separate columns, and if the nation is France, the France column will be assigned a value of 1 and the rest of the columns will be assigned a value of 0.

Making the model

Splitting the Dataset

We must first import the test train split from sklearn model selection before splitting our model dataset into a train and test dataset.

Feature scaling

Here is another term I’d like to introduce to you: feature scaling.

Why do we do feature scaling? When a particular value in a dataset is very high, it starts dominating the model, reducing model accuracy. To overcome this problem, there is a method called feature scaling in which we scale down the high values to normalise the values. Here i am using Standard Scaler

Model

Making an ANN model is far more difficult than making machine learning models.

First, I’m starting a sequential model with tensorflow keras. The second step is to add the hidden layers to the model. The layers are added like this: here in this model, there are three hideen layers and one layer to compile the above layers.

  • In the first hidden layer, I’m using units of 6 and an activation function relu. I’ll explain these concepts in a later article, so just remember them for now.
  • The second hidden layer is similar to the previous one, and I’m using units of 6 and the activation function relu.
  • In the final layer, I set the units to 1 and the activation function to sigmoid.

Here comes the compiler; now we must compile all three layers into the model. I am using the optimizer function as Adam, loss as binary cross entropy, and metrics as accuracy.

Now we must use the fit command to fit the entire dataset into the model.

batch size means number of examples in one iteration.

epochs means the number of passes of the entire training dataset the machine learning algorithm has completed.

Predicting

Single value

To predict a single value, we need to enter all of the values in a list, and remember that we trained the model using a dataset of features, so we need to scale the given value, which is why there is sc.transform, and the threshold is 0.5, which means that if the value is greater than 0.5, it will return true, otherwise it will return false.

in the given example the values are

country is France

credit score is 600

gender is male

age is 42

tenure is 3

balance is 60000

number of products is 2

has credit card is 1

active member is 1

estimated salary is 50000

Test values

nowhere I created a variable called prediction that will predict the values of our test data and concatenate the predicted and actual values.

Accuracy

Now we’ll look at our model’s accuracy. Our model is 86 percent accurate, which means it correctly predicted 86 values out of a possible 100.

source code

you can go check the link for full code

Conclusion

in the article I have given you information and codes on how to make a Artificial Neural Network (ANN) with source code I would be making more exciting models for you so stay connected.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store