Hello, my friends.
All right. Are you ready to start this
big implementation of your very first artificial brain?
Well, I'm definitely ready.
So let's do this. Let's smash this together.
All right, so we're going to start with the data preprocessing phase,
which we will tackle in this same tutorial because we quickly
want to get to the interesting stuff.
So let's do this efficiently thanks to our data
preprocessing template but also our data preprocessing toolkit.
And therefore the first thing we're going to do here is to import the libraries.
So we're going to create a new code cell here.
We're going to go into our data preprocessing template to steal the cell
we want meaning this one and get it back into our implementation.
The first cell.
All right.
So that's the first thing.
However I just want to show you something extra.
It is about the beauty of Google Colab.
I want to show you that indeed
TensorFlow 2.0 is already pre-installed in Google Colab.
You know, in any Google Colab notebook you will ever open.
So the way for me to show you this is to first import TensorFlow,
because okay, it is already pre-installed as a library
inside a notebook, but we still need to import it.
And in fact here, since we will actually won't use matplotlib,
we're just going to delete this import of this library
and then just add as a third library here.
Well TensorFlow.
And the way to import TensorFlow we start with indeed import
then the name of the library which is TensorFlow of course.
And then we add a shortcut simple one
like the classic one okay.
And now I'm going to create a new code cell.
And inside I'm going to enter the following TF dot
double underscore version double underscore again.
And this will simply print the version of TensorFlow we're using
which I just want to show you
is indeed TensorFlow to write the brand new TensorFlow.
So let's do this.
First we need to execute this cell and this one.
But remember if we execute this cell now this will take some time
because actually the notebook is not running yet.
And the way to run this is just to click this folder here.
Right.
And it is now that it will connect to a runtime to enable file browsing,
but mostly to, you know, start running the notebook.
Okay.
And at the same time let's take that opportunity to upload the data set.
So please go to your machine learning is that Codes and Datasets folder
which had to download
either in the previous tutorial or at the beginning of each practical activity.
So there we go inside the import a deep learning almost close to the end.
By the way, you must be very excited almost seeing the tip of the tunnel.
Then let's go to this section on Artificial Neural network.
Let's go to Python and let's select this dataset
churn modeling dot CSV open okay.
And there we go.
Now we have everything. We have the data set.
And besides our notebook is already running.
As you can notice it takes a little bit more time than usual
because you know the data set is this time more real world and mostly bigger.
Okay. All right.
So let's do this.
Let's run this first cell here to import the libraries.
This time numpy pandas and TensorFlow.
And now let's play this cell to indeed reassure ourselves
that the TensorFlow version we're going to be working with is 2.2.0.
Basically TensorFlow two, which is so much better than TensorFlow one.
I'm so happy about their new version.
Okay great.
So it's good to have the confirmation.
And now now let's tackle this part one data preprocessing.
We're going to do this
efficiently thanks to our data preprocessing template in toolkit.
So let's create first
a new code cell to import the data set which we already have in a notebook.
Perfect.
So let's go to our data preprocessing template.
And let's now steal this second cell to import the data set.
All right.
Let's go back here. And let's face that inside.
And now of course the question is what do we need to replace.
Well the obvious first change we need to do is the name of the data set,
which this time is not data dot CSV but churn
underscore modeling, dot CSV create.
Then let's look at the rows one by one.
This one is okay.
Now what about this one?
This line of code creates the matrix features x and the way it does
this is it takes all the columns except the last one.
But let's actually have a look again at our data set.
Right.
We noticed when I described to you the data set that the first columns
are actually irrelevant in the sense that they will not help
to predict the outcome of the dependent variable.
And these columns are, you know, the non helpful columns or obviously this one.
This just gives the row number of this data set.
So we clearly don't want to include it then customer ID as well.
Right.
The customer ID is just a key identifier of each customer.
Because you know each row corresponds to a different customer.
So of course the customer
ID has absolutely no impact on the dependent variable exited.
So we will also exclude that column.
We don't have.
So you know the neural network will just figure it out.
But let's just ease the learning process of our future neural network.
Right.
We're all on the same boat here.
Okay. Then what about the surname?
Does the surname have an impact on
whether the customer will stay in or leave the bank?
Well absolutely not.
Right.
Surname of course, has no impact on the decision of a customer
to stay in or leave the bank.
So we will also exclude this column
and then all the rest, you know, all the other features here look fine.
They might have an impact
on the dependent variable, meaning they might help to predict
if each customer will stay in the bank or leave the bank.
Okay, so we will definitely keep all the other ones, meaning all the features
starting from this one.
Do credit score and so here in our implementation,
instead of taking all the columns except the last one, well we will take
all the columns starting from this one except the last one,
meaning all the columns from credit score up to estimated salary.
And the way to do this is still to keep that upper bound of the range.
You know, finishing at the one before last column.
Right? You know, that's exactly the upper bound.
That's the range.
But at the left of this range we won't specify
nothing, which means the first column, the first index.
But instead we will specify the index of the column
we want to start with which is the credit score.
Right. We know we want to start from here.
And therefore now the question is what is the index of that column.
Well let's see indexes in Python start from zero.
So this has index zero.
Then this has index one.
This has index two and this has the next three.
And therefore here instead of specifying nothing here as a lower
bound of the range, well we will specify the index three
so that we can take all the columns starting from the column of index three
up to the one before last, and taking all the rows, all the values of the data set.
And this will create a relevant matrix of features.
Perfect. So this line of code is done.
Now what about the next one.
Well obviously the next one is fine.
It will just take the last column of this data set, which is exactly
what we want for dependent variable exited.
So all good here. Nothing to change.
We can just play the cell and we will have our data set,
our matrix of features and our dependent variable vector.
Let's check it out.
Actually let's create two new code cells right.
One where we will print the matrix of features x,
and one where we will print the dependent variable vector y.
Perfect. All right.
So let's do this now
actually let's play first this cell to print the matrix of features x.
And there we go.
We have indeed all the features starting from the credit score.
This is a credit score.
Then you know the country of residence and then the gender and all the other ones
you know has credit card. Yes or no is active.
And the last one is the estimated salary.
Okay. So we have all these features. Perfect.
And of course we don't have
the dependent variable values because they are right here in Y.
And there we go.
These are all the decisions that the customers to state or leave in the bank.
So of course this one here corresponds to this customer here
which obviously has decided to leave the bank.
Right.
This is actually this same one here exited one.
And then well, this second customer
has decided to stay in the bank and corresponds to this one.
Right.
Which is exactly this one as well. Okay.
This customer.
All right.
So all good so far.
First step of the data preprocessing phase done successfully.
And now let's move on to the more advanced steps of our data preprocessing phase
which is about encoding the categorical data.
Right.
Of course we noticed that there are two categorical variables.
This first one giving the country of residence of the customers,
and the second one giving the gender of the customers.
So we'll have to do some encoding work here to encode these categorical data.
And either simple labels, you know, zero and one for the gender
or some one hot encoding for this categorical variables
in which indeed there is no relationship order between these values, you know,
between these categories France, Spain and Germany.
Okay. So let's do this.
Let's start first with the label encoding of the gender column.
So let's create a new code cell.
And now of course to do it efficiently we're going to go into our data
preprocessing toolkit.
We're going to scroll down to find.
By the way there is no missing data in the data set.
I checked them and in reality you would also have to check them.
But all good.
We don't have to take care of any
missing data so we can directly move to encoding categorical data.
And now since we're taking care of label encoding the gender column,
well we're going to take this.
That's exactly the tool we need to perform label encoding.
So I'm stealing this code cell.
Now I'm adding it inside our notebook here.
Our implementation.
But remember that in our data preprocessing toolkit we did this
on the dependent variable vector.
But now we want to do it on this specific column of the matrix of features x.
And therefore what we only need to replace here is this y by that specific
column of the matrix of features x to which we want to apply label encoding.
And so.
Well now the question is how can we get this column.
Well we just need to get the index and then call x with that index.
And so well there we go.
That's the first column of x. It has index zero.
That's the second column of x. It has index one.
And that's the third column of x which has index two.
And therefore here we simply need to replace y
by our matrix of features x of which we're going to take all the rows.
And I'm taking them with this column, you know which means arrange in Python.
And then to take the column we want meaning the gender column which has
index two.
Well I just need to add here after the comma the index two
so that it will take all the rows but only the column of index two.
And now of course we need to take this.
And paste that inside the fit transform method called from our object,
which is an instance of the label encoder class.
And done.
We just performed successfully label encoding to the gender column
of our matrix of features x.
Let's make sure it's the case by creating a new code cell here.
And do, new print of the matrix of features X.
Let's run the cell you know, first.
All right. Good.
And now let's print X.
And let's just make sure that we no longer see
female, female, female, female, male, female.
But whatever encoding there was, which probably will be one
for female or zero for female and zero for male or one female.
Let's see what they did.
All right. And there we go. Right.
That's the new column after the label encoding and so female
was encoded into zero and male was encoded into one.
That's of course a random decision of the machine to choose this.
Integers associated.
And so all good.
Now this column is well label encoded.
And now we're going to proceed to the one hot encoding of the geography column.
And this time we have indeed to perform one hot encoding
because there is no other relationship between France, Spain and Germany.
So we couldn't, you know, encode France into zero.
Then Spain into one and German into three.
We have to perform one hot encoding instead.
And so let's do this.
Let's go back to our data preprocessing toolkit.
Let's take that cell this time, which is executive
cell that perform one hot encoding.
And let's paste it inside a new code cell
to one hot encode the geography column.
All right.
Now the question is of course what do we have to replace or change
in that cell to indeed perform one hot encoding on the geography column?
Well, remember, the only thing that you have to
change inside this code is that index of the column
you want to apply one hot encoding on, right?
And remember that in our data CSV file of our part one data preprocessing.
While the categorical variable
with the three different states was in the first column.
That's why we had index zero here.
But this time this column is actually the second column.
Therefore it has index one.
And therefore very simply we just need to replace zero here by one okay.
And that's it.
All the rest will be done automatically.
Let me show you this. Let's play that cell.
And now let's create a new code cell to print again X.
All right. Good.
And now let's play that cell and see what x has become.
And indeed well remember when we perform one hot encoding.
Well the dummy variables
are actually moved to the first columns of the matrix of features.
We have them exactly here you know in the three first columns.
So let's see.
Let's see how the one hot encoding was done.
This is the first combination
of dummy variables which corresponds to friends.
You know these are the same rows here.
And therefore friends was encoded into 100.
Now Spain was encoding into 001.
And finally Germany was encoded into
well this 1010 okay.
So that's all one hot encoding.
Then we no longer see the gender column.
But no worries it is still here.
And so perfect.
One hot encoding was not only done successfully, but also Western efficiently
thanks to our data preprocessing toolkit and template.
Good.
Now let's move on to the next step, which is to split
the data set into the training set and the test set.
And once again we're going to do that so efficiently
thanks to this time our data preprocessing template.
Indeed we have to steal now this cell that splits the data
set into the training set and the test set.
So let's step back into our implementation
in a new code cell right here.
And now we can just just this 100%.
We will just play that cell.
And we don't have to do a print of these four entities.
We perfectly understand how they work, but feel free to do it if you want.
You're free to do any modification in this copy, of course, of the notebook.
And finally, we have a final step
of our data preprocessing phase, which is feature scaling.
And now I want to say something very, very important.
Feature scaling is absolutely compulsory for deep learning.
Whenever you build an artificial neural network you have to apply feature scaling.
That's absolutely fundamental.
And it is so fundamental that we will actually apply feature
scaling to all our features, you know, regardless of whether they already
have some values of zero and one.
You know, like the dummy variables. And same for these ones.
We will just scale everything because it is so important to do it
for deep learning.
So the feature scaling step here will be very simple.
We will just take our data preprocessing toolkit.
We will go right at the end because I think this is our last tool.
Yes there we go.
We will take that full cell and we will paste it right back
in a new code cell just below feature scaling will paste it here.
And now instead of selecting
some specific indexes here, we'll just take everything.
So I'm just removing all our index selections here right.
So that we can just scale everything.
And that's the way it should be for neural network
you know for building and training a neural network.
All right. So perfect.
This will just apply feature scaling to all the features
of both the training set and the test set.
But of course our scaler object is only fitted to the training set.
Right.
Remember it's to avoid information leakage that doesn't change.
But there you go.
Now we have the code to perform features counting already.
So let's do this.
Let's run this final cell and then the data preprocessing phase will be over.
So congratulations I hope we did it efficiently enough for you.
That's the way it should be.
I'd like to remind, by the way, that, you know, the data preprocessing
phase counts for 70% of the work of a data scientist.
So that's why it was really important for me to give you some very efficient
data preprocessing template and toolkit so that, as you can see, we can do it
efficiently in less than 20 minutes, you know, in less than 20 minutes.
With my explanation, but without the explanation,
even in less than ten minutes.
So I hope you understand and appreciate the importance.
And now, my friends,
it is time for the exciting step, the exciting part of this implementation.
I'm talking of course, about part to building the CNN.
So there we go.
Recharge yourself with good energy, and as soon as you're ready,
let's tackle together part two, where we're going to build
for the first time an artificial brain leveraging TensorFlow 2.0.
I can't wait to see you in the next tutorial.
And until then, enjoy machine learning.