Hello and welcome to this art tutorial.
I'm super excited to be in the deep learning part.
This is one of the most fascinating and exciting branch of machine learning,
and besides, it's one of the most powerful.
In the following tutorials, we're going to solve the business problem
described by Kirill at the beginning of this section,
and you will see that we are going to get strong results.
Thanks to this artificial neural network that we are about to build with are.
So as usual,
we are going to make this artificial neural network model very efficiently.
And we're going to use the best package
for that, which I will let you find out about in the next tutorials.
So let's start.
And the first step of our journey is the boring step data preprocessing.
But we will do it very efficiently because we have our template
or classification template that I've prepared here.
And why can we use this classification template?
Well, it's by nature of the business problem you saw in the business
problem description that you have some independent variables.
And with these independent variables, you have to predict a dependent variable
that has a binary outcome.
And since the outcome of the dependent variable is binary,
that means it's a categorical variable.
That means we have to predict classes zero and one,
and therefore that makes our problem a classification problem.
And so okay we're going to build a deep learning model.
But this deep learning
model is going to be nothing else than a classification model.
And that's why we are going to use our classification template that we have here.
And that will save us a lot of time to build our artificial neural network.
And besides, we want to focus on the deep learning model itself.
So we will get there very quickly thanks to this template.
All right.
So let's take everything from this template from the top down to here.
Because we cannot use this section here because, you know this is the section
to visualize the training set results.
And the test set results as well.
But only when we have two independent variables,
because one independent variable corresponds to one dimension.
And since now in the data
set of the business problem, we have, I think 10 or 11 independent variables.
Well, then it's a little bit hard to represent something in 11 dimensions.
So we won't take this, but we will definitely take everything that's above.
So I'm going to copy that and let's go back to our A and and model
and paste this classification template right here.
All right.
And now in this template we're going to change a very few things.
And of course we are going to build our artificial neural network
right here in this section create your classifier here
we can already replace classifier here by and to then build the model.
All right.
But of course
we need to make sure everything's okay in all the data pre-processing step.
And that's what we're going to do right now.
All right. So let's start.
Let's start with the basic step setting the right folder has a working directory.
So right now I'm on my desktop I'm going to my machine Learning 80 folder.
Then we are in part eight Deep learning.
And now section 40 Artificial Neural Networks.
Here we go.
Make sure that you have the churn modeling dot CSV file.
And if that's the case you can click on this more button here.
And then set as working directory. Great.
And now let's change a few things.
So first of all let's start with this section importing the data set.
Well the name of the data set is not social network ads.
It's now for churn modeling.
All right.
We are now ready to import the data set.
So let's do it right now I'm going to select this line.
And execute.
All right.
Data sets will import it.
Well we have actually 14 variables.
But let's see if we include all these variables in the real data set.
You know the one on which we want to build our deep learning model.
So let's see I'm going to click on this data set here
to see which independent variables we include in the model.
All right so just a quick reminder.
This data set contains 10,000 observations containing
some informations of customers in a bank like the surname
the credit score geography gender, age and all the other informations here.
And during six month the bank looked for each customer.
If the customer stayed or left the bank within the six month period
and this result,
whether the customer stayed or left is given in this last column here exited.
So one means that the customer left the bank during the six months, and zero
means that the customer stayed in the bank during the six months.
So what's important to understand now is that all these variables here,
from row number to estimated salary, are the independent variables.
And the last column here exited is the dependent variable.
So right now our goal is to make a model where we can predict this result.
Exited here whether the customer left or stayed in the bank
from the information contained in all these independent variables here.
But the thing is that in these independent variables,
some definitely don't have an impact on this dependent variable.
Exited.
And so now what we have to do is only take the independent variables
that could have an impact and correlations
with the decision of the customer to leave or stayed in the bank.
And so that's what we're going to do right now.
So let's look at each of these independent variables one by one.
And let's see which one we keep in our model.
All right so let's start with the first one row number.
Well row number has definitely no impact on the dependent variable exited.
So of course we will not include it then customer ID.
Well customer ID that's the same.
That's just an identification number.
This definitely doesn't have any impact on the decision of the customer
to stay or leave in the bank.
So we will not include that either.
Then the surname.
Well that's the same.
It's not because your name is Andrews that you have more chance
to leave the bank than if your name is Romeo.
All right, so we don't include surname either,
but then we have credit score, and credit score might have an impact
on the decision of the customer to stay or leave in the bank.
Indeed, we can assume that customers with a low credit score
are more likely to leave the bank than customers with a high credit score,
so definitely we will include credit score in our model.
All right then we have geography.
Well,
maybe some customers are more likely to leave the bank in one specific country.
And that can be due to external factors like the economy of the country
or any other factors.
But yes, definitely,
there might be some correlations between the countries
and the decision to stay or leave the bank.
So we willing to do that as well then?
Gender. Well, that's the same.
Maybe men or women are more likely to stay in the bank than the other.
So we need to check it out then. Age.
Well that's the same.
And that's even quite intuitive.
We might expect that younger people, or more likely to leave the bank
than older people,
because all the people have more balance and have more stability.
So we include age as well then tenure.
So tenure is for how long the customer has been in the bank.
And so that's the same.
We might expect that customers that have been in the bank for a long time
are more likely to stay in the bank than recent customers.
So yes, we'll take it then. Balance.
Well, balance, of course, we might expect that this customer with this
high balance has a lot more chance to stay in the bank
than this customer with the zero balance all right, than the number of products.
So that's the number of banking products the customers have in the bank.
And so of course, maybe that the customers with many products in the bank
are more likely to stay than customers with, for example,
one product in the bank. So we'll need to check it out.
That's just assumptions.
That's the model that we'll find out about these correlations more thoroughly.
But you know definitely from our intuition we need to include
number of products as well.
Then has great card.
Well that's a little bit of the same as this variable.
Customers that have a credit card might be more likely
to stay in the bank than customers that don't have a credit card.
So yes, is active member. That's the same.
If a customer is active,
then this customer is more likely to stay in the bank than a customer
that is not active.
So yes, it might be a significant independent variable.
Then estimated salary.
Well, that's the salary of the customer estimated by the bank.
And it would make sense that customers with a high estimated salary
have more chance to leave the bank than customers with a low estimated salary.
All right.
So that was the last independent variable of this data set.
So now we know which independent variables we include in our data set.
And that's what we're going to specify right now by updating our data set
taking only the indexes of the independent variables
we want to include in the model.
So let's see what these indexes are okay.
So indexes in R start at one.
And so basically we taking all the independent variables
from credit score up to estimated salary.
So let's see index one index suit index three index four.
So we are taking the indexes 456789
ten 1112 and 13.
All right.
So we are taking the indexes from 4 to 14.
Because you know in R it's not like in Python when we separate
a matrix of features and the dependent variable vector
we include all the variables in one data frame.
And so we include the dependent variable grade.
So let's input these indexes.
So we just said that we want to take the indexes from four.
So that's the index of the first independent variable
up to the index 14 which is the index of the dependent variable.
And that's great.
Now we can update our data set by selecting this line and execute.
Great.
And now as you can see if I will go back to the data set here
we have all are potentially statistically significant independent variables
that might have an impact on the dependent variable exited.
And so now the first step of data pre-processing is completed.
We import correctly
the data set by choosing all the relevant independent variables.
Okay. Now let's move on to the second step.
The second step is encoding the target feature as vector.
Well we don't really need to do that because the dependent variable of our data
set is a categorical variable with a binary outcome 1 or 0.
And the thing to understand is that the package we're going to use
is going to recognize it as a categorical variable with a binary outcome.
So we actually don't need to encode this target feature exited as a vector.
So I'm going to remove this line. We don't need it.
However we do need to do something regarding some categorical variables.
Of course I'm talking about
the two categorical independent variables we have in our data set.
And these two variables are of course geography and gender.
So we have two problems here.
So we need to do two things here for these variables.
The first thing we need to do is to convert them as vectors.
And then we will need to do something
more than we used to do when encoding our categorical variables.
It's to set them as numeric.
And to do this we'll use the as numeric function.
And why do we need to do this especially here.
Well that is
just because the deep learning package that we're going to use is requiring it.
And that's the only reason it expects vectors but set as numeric
numeric vectors.
So let's do this I'm going back to my A and model.
And so first we're going to change this to say
that we're encoding the categorical
variables as factors.
All right.
And now we're going to take this
categorical data file that we made in part one data preprocessing.
Because you know there is the code ready to encode any categorical data.
So I'm going to select all of this and paste it here in this second
step of data preprocessing to encode the categorical variables as vectors.
All right. So let's do this.
We just need to replace the names of the variables and then add
this as dot numeric function to set the factors as numeric.
So let's start by replacing all the names here.
Well the first categorical variable gives the countries
but it is not called country.
It is called geography.
So we will replace here country by geography.
Same here.
And the good
news is that now we don't need to change the names of the categories here
France, Spain and Germany because that's the same names.
So that's great.
And we will keep the labels 123.
All right that's good.
And now we add this as dot numeric function
to set the factors as numeric.
So I'm putting all these factor function
here inside the parentheses of the as numeric function.
And now I just need to align everything.
Well here we go.
And same for here.
All right. Great.
And now let's do the same for the second categorical variable.
So we need to replace purchase here by gender.
So let's do it.
Purchased replaced by gender.
All right.
Same here gender.
And now we replace the two categories no and yes by
female and male.
And here we can give the labels we want.
So let's for example take labels one for female and two for male.
All right.
And let's not forget to add the US
dot numeric function which I remind we just
do for the future deep learning package that we're going to use.
So parentheses here parentheses here.
And now let's align everything.
Here we go. All right. Great.
So now everything is ready.
This section is ready that encodes as required
by the deep learning package our categorical independent variables.
All right.
So I'm going to select all this section here.
And let's execute.
All right. Executed properly.
Now let's have a look at the data set to see what the variables became.
Perfect.
Geography was encoded
into one, two and three categories that are numeric categories.
And the gender one for female and two for male.
Great and again as numeric vectors.
Perfect.
So this section is now completed.
And let's move on to the next one.
We can see how we're getting very efficient at this.
The next one is about splitting the data sets into the training set
and the test set.
We need to do that because we will train our artificial neural network
on the training set, and we will test its performance on the test set.
So we'll do that.
But let's not execute too fast.
We need to replace purchase here
by the name of the dependent variable, which is exited.
And maybe we can change the split ratio as well.
You know, put 80% for the training set so that we have 8000 observations to train
our artificial neural network and 2000 observations to test
its performance on new observations.
That is, the new observations of the test set.
So now that's ready.
We don't have to do anything more here.
The most important thing is not to forget to replace purchased by exited.
And so now I'm going to select all these section and execute perfect.
Now we have our training set and our test set.
Great.
So that is the whole data set.
That is our training set with 8000 observations.
And that is our test set with 2000 observations.
Perfect.
Now let's go back to our and
and we are finally getting to the last step of data preprocessing.
And that is feature scaling.
So now the question is
do we need to apply feature scaling to train an artificial neural network.
And the answer is yes.
Absolutely.
That's 100% compulsory.
And that is because training
an artificial neural network is highly compute intensive.
So there is going to be a lot of computations.
And besides parallel computations.
So definitely we need to apply feature scaling.
And besides it is required by the package.
So we will execute this.
But before let's not forget to change the indexes.
These index is three here where the index
of the dependent variable and part one data preprocessing.
So right now we just need to replace this index three.
Here by our new index of the dependent variable.
And so what is this index.
That is the index of the exited column.
Well we can see that directly here.
This data set has 11 variables.
So that means that the exited column here has index 11.
So let's replace three here by 11 then here as well
1111 and 11.
Great.
And now the feature scaling section is ready.
So let's select the whole section.
And execute. Great.
And now if we have a look at our training set
well well yes definitely everything is scaled.
And our test set same.
Everything is definitely scaled.
We are happy.
We are ready to build our artificial neural network.
And that's what we're going to do in the next tutorial.
So I'm super excited to start.
I look forward to seeing you there.
And until then, enjoy machine learning.