So now let's go ahead and probably implement this regression problem statement.
So what I will do I will go ahead and write over here.
And inside this folder only I will be creating this because we are going to follow the same steps.
So I'll go ahead and write I regression regression ipynb I'll say churn regression or I'll say salary
regression.
Okay.
So this will be my file.
I will go ahead and quickly select my kernel okay.
Now first of all, what are the steps that we will do.
So initially we will go ahead and import all the necessary modules that is required like train test,
split, standard scaler, label encoder one hot encoder and pickle.
Okay.
So we'll go with the first step over here.
Then uh, after this we will be reading our CSV file.
So I'll go ahead and write read underscore CSV.
And let me just go ahead and write this as churn modeling.
Churn.
Churn underscore modeling dot csv file okay.
Now if I go ahead and write data dot head I will be able to get my out input over here.
And this is my entire data set right.
As I said estimated salary will be my output feature and remaining all will be my independent feature.
Okay.
And that is the reason the output feature is a continuous value over here.
So we are considering this as a regression problem statement.
Now after this the next state that what I will do is that I will delete this row number, customer ID
and surname.
Whatever things we did in the classification problem almost the same way, we have to probably do this.
Okay, so I have executed this.
Then we will go ahead and convert this, uh, gender from female male to zeros and ones.
Because this here I'm going to use my label encoder.
So for that I will go ahead and create my variable label encoder gender.
I will initialize to label encoder.
And then I will go ahead and write.
Data of gender is equal to label encoder dot fit underscore transform okay.
Similarly I will go ahead and do it for the geographic column.
Also where I'm going to use one hot encoder Again I'll use fit underscore transform.
I'll convert this into arrays.
I will get all my values, then convert this into my data frame along with my features.
Okay, so if you go ahead and see your geo underscore encoded underscore DF, you'll be able to see
hey I'm getting this three features.
Okay.
Then we are going to combine this one hot encoded columns with my original data.
Step by step we are going to do this.
So finally you'll be able to see data dot head.
And we also dropping the geographic column.
Now I don't have my geographic column.
And if you probably go ahead and see my this three columns have got added.
Perfect.
Now we are going to split our data into features uh into our independent features and target feature.
Right dependent feature also.
So I'm dropping estimated salary from this.
And all the remaining features will go in x axis and my estimated salary will go in the y axis.
Okay.
So this is done a bit of processing.
Now I will save all the pickle file specifically for transformation like label encoder dot pickle.
Then you have one hot encoder dot pickle.
Then one more pickle that we are going to probably create a scalar pickle.
But uh, the scalar dot pickle we will specifically use uh only when we try to, uh, you know, do
the train test split.
So let's go ahead and do the train test split also.
So here I will take this code quickly.
I will just copy and paste it so that I don't have to waste more time on this writing the entire thing,
because we are doing the same thing.
If I was doing something else then I could had probably done that.
Okay, so here I'll go back to my salary regression quickly.
I will go ahead and write code now.
Quickly I'll paste it over here.
Okay.
Train test split.
And now I will go ahead and apply my standard scaler scale this feature quickly.
Salary okay.
And here I will go ahead and do this.
And this is done right.
Okay I'm getting an error.
Could not convert string to me.
Why.
Because let's see my x x x value.
Okay.
Gender is still male and female.
Why why why why.
Because data of gender it is.
Okay, fine.
Data of gender.
So if I replace this what is my data over here.
If you see the data or let me execute this once again, I think I have missed something so I'll quickly
execute this.
I'll remove this tree.
I'll see this.
Now let's go ahead and check my data.
So my data is basically having zeros and ones.
I think I missed one line of code in executing it.
Then I'll do the concatenation.
And finally I will do the concatenation.
This has got executed I have gender zero and 1X1Y.
I get it and I'm doing the train test split and now it should do the scaling.
Okay, so I missed one code.
So that is the reason that was the issue.
Now we'll go ahead and save all the pickle files.
Scaler dot pickle file one hot encoder G0 and label encoder gender pickle file.
Okay, now it's the time to train our Ann with regression problem statement.
Right.
How to train our Ann over here.
That is what we are basically going to discuss.
And this is the important thing.
Remaining thing are almost same.
We do not have to make any changes okay.
Now let's go ahead and quickly do this.
So first of all again I will import TensorFlow as Keras okay.
And then we are importing sequential and dense.
So first of all we'll go ahead and import this.
So once this is imported we are good to go and work with sequential and dense.
And we are good to work with our entire neural network okay.
So here we are executing this.
Then quickly let us go ahead and do this okay.
So we will go ahead and build our model again.
To build our model we will use the same technique.
Like first of all I will say hey go ahead and take this extreme dot shape of one as input shape.
This will be my input layer.
Then we are going to apply an activation function.
The first hidden layer will have 64 neurons.
The second hidden layer will be having 32 neurons.
And the activation function will be ReLU.
And the dense will basically have one.
Okay.
Now when we are using one here, we are not applying any activation function.
When we don't apply any activation function, the default activation function that will be applied is
called as linear activation function.
Now linear activation function is specifically for regression.
That basically means I'm not doing anything.
Instead I'm getting whatever output I'm getting.
I'm using that okay.
Now after this we will go ahead and compile this model okay.
So we will go ahead and write compile the model.
Now for compiling the model I will go ahead and write compile model dot compile.
And here uh first of all I'll go ahead and write my optimizer.
Let's say the optimizer is Adam.
For this case you can also go ahead and initialize it from Tensorflow.keras.
But I'm directly writing the keyword.
Then my loss.
Now this is what is the main thing that actually changes in regression.
In loss we will basically use mean absolute error okay.
You can also use mean squared error.
But here we are just going to use mean absolute error.
It is up to you.
And here the metrics that we are specifically going to use is nothing.
But it will be me.
Okay.
Again, you can see the Keras documentation what all loss will specifically be happening over here.
So if I go ahead and open Google right.
And if I just search for Keras loss functions okay.
So if I just go ahead and click on this.
So it shows so many different for, for uh, you know uh classification problems you have binary cross
entropy, binary focus, cross entropy, categorical cross entropy.
Um, then you have binary cross entropy functions, all these things.
Right?
Uh, for regression you have mean squared class.
See this?
This is what I've used.
Right?
Means absolute error.
Right function.
Uh, you can also use mean squared error.
Right.
And already I've spoken about this, uh, even in machine learning, if, you know, machine learning,
I think there all these things will be probably covered up, you know?
So you need to have that basic knowledge with respect to all using this kind of loss function.
Okay.
So now what I will do I will use a matrix as me.
And then finally let's go ahead and display the summary.
So I'll just go ahead and write model dot summary.
And when I display the model summary then here you can see this is my input.
Uh this is my input.
So the total number of weights and bias are nothing but two nine, four, five.
These are the trainable parameters that we have right?
Then I have dense one dense two.
And finally my output is over here okay.
Now let's go ahead and train this particular model again for training this model I will go ahead and
set my logs okay.
So here are some of the logs I'll say this is my regression logs I'm using early stopping and TensorBoard
as you all know.
So we are going to use both of them so that this folder will get uh, it will just try to whatever logs
is basically coming during the training.
It will be coming over here.
Then we are going to set it to early stopping also.
Now in the case of early stopping I will be using validation loss.
Like uh, whenever we are training our neural network you will be getting, um, training loss, uh
sorry training accuracy.
You'll having training loss.
Then you'll also have, uh, validation accuracy and validation loss.
But I really need to monitor the validation loss.
And I'll keep the patient's value to ten.
That basically means I'm going to see it for ten epochs okay.
And finally we will go ahead and train the model.
Now in order to train the model I need to use this callbacks so early stopping and TensorBoard callback
I'm giving my Xtrain Ytrain y epochs will be 100 and all.
Everything is there, right?
So let's go ahead and train this, uh, and let's see how much will be the answer that I will be getting.
So epoch one here, you can see me.
Uh, uh, these all values are there validation loss here also values are coming.
Right.
And these are keep on decreasing unless and until keeps on decreasing.
This epochs will be going on right.
So the validation loss also you'll be able to see that it is decreasing okay.
See validation m e validation loss unless and until is not decreasing.
Right.
If it if it is maintained uh, in a similar manner.
Hardly.
There is not much difference.
Then it will stop.
So here you can see in epoch 47 it has stopped because till here the loss was going down.
But after this it was almost similar.
Right.
So uh, now my model is almost trained.
Uh, again, this is, uh, I'm actually doing it in my local machine.
Again, I have a powerful CPU, so I'll be able to do it very much fast.
Now let's go ahead and evaluate the model.
Okay.
Now, before that, uh, let me just go ahead and, uh, load my, uh, load extension TensorBoard.
Okay.
So I will just go ahead and execute it.
Then I will say, hey, go ahead and take this TensorBoard and my directory will be regression logs.
Slash.
Let's see this particular folder.
So here you have regression logs slash fit okay.
So slash fit will be my.
So once I execute it here will be able to see that we'll be able to load the TensorBoard.
Now in this TensorBoard all the information regarding how the training has happened and all.
So I'm getting some error.
So this should be directory I guess.
Let's see over here okay.
Again you do not need to buy at it.
You can see the documentation and you should be able to see this okay.
You may be thinking Chris you don't know all these things.
Uh I'm not a I will definitely not buy at it.
So you should also not do that okay.
So this should be the log directory.
And here should be my folder okay.
Now I think it should get executed.
Now we are launching the TensorBoard.
Now this is good enough.
It will take all the events from inside this and it will display it to you okay.
And let's see it is taking some time to load it quickly.
Let it get loaded.
I think for the first time I have to execute two times, but it does not happen in Google Colab.
In Google Colab, in the first instance, only you will be able to run it.
Okay, so this is really, really good.
So here I have got my entire information which is amazing in the TensorBoard.
Now see see see this entire things right now if I go ahead and see with respect to regression epoch
versus loss.
Wow this graph looks amazing.
See it is going down.
Down completely.
Right?
So loss is coming down.
If you see mean absolute error this is also coming down.
I think this is an amazing graph.
And this looks good.
That basically means your epoch is going down that the model is really performing well.
Then here you have evaluation loss versus iteration.
No iteration by iteration.
How much it is basically getting reduced.
Then you also have me versus iteration.
This also you can actually check it out, right?
Um, if you just just want to see the validation.
Validation has this epic loss and all.
So sorry epoch loss and all.
So here you have your Ma.
You can probably zoom in and zoom out.
And based on this you can check you can also do something like this and check it out.
Right.
So all this information from the logs you are able to see it okay.
Now let's do one thing.
So if you see me and all, we will also try to evaluate this model for our test data.
So let's go ahead and evaluate model on the test data.
We're just going to check the accuracy how good it is.
So I will just go ahead and compute my test underscore loss and test underscore m.
Uh sorry me mean absolute error I'm just going to write model dot evaluate x underscore test y underscore
test perfect.
So now I'll just go ahead and print.
And I'll use a f string.
And I'll say hey this is my test.
M a e which is nothing but it will be test underscore ma.
So here you can see five zero, one six and more near to zero.
If it is going on, we can probably say that our model is pretty trained in an amazing way.
Okay.
And this is done.
And what you can also do is that, uh, um, along with this, I can also go ahead and save my regression.py
file.
So I will say, hey, go and save this as my regression model.
Regression model dot h5 file.
Okay, so once I do this, this entire model will get saved.
And I have my regression model dot h5 file.
Right.
So almost we have done each and everything right.
Now if you want to deploy this you have all the H5 files.
You have all the training that is been done.
Now you can also go ahead and deploy it.
So for deployment purpose, what I am doing is that I will give you the entire code again with the help
of Streamlit, you can deploy it.
Let me just go ahead and create a file over here.
Let me say streamlit_regression.py okay.
So I'll give you the code over here so that you can go ahead and do the deployment.
And it's very simple.
It's almost like this only.
So here you can see all the things and estimated salary.
You can basically go ahead and compute it okay.
See we are loading it.
We are creating some user input box.
We are converting this into a data frame.
We are doing the encoding.
Then we are predicting the salary okay.
You can do this entire thing okay.
Oh let me see one more model huh.
Regression underscore model dot h5 file okay.
Perfect.
So I hope you understood this particular video.
Again.
It's more about how much you practice and how easy, uh, it was probably to train the entire.
And that is artificial neural network, uh, for classification, for regression.
Now, one of the most common question that has been asked is how do you decide that?
How many hidden layers are neural, uh, hidden neurons you need to take?
We need to discuss about that also, because that is something very important.
So in this case, it is very, very much difficult to just say that, hey, you probably need to just
use this many number of hidden layers or this many number of hidden neurons.
That is not possible.
So what we do is that we basically apply a lot of hyperparameter tuning, like with respect to the number
of hidden layers and uh, with respect to number of hidden neurons.
And that is what I will be showing you in the next video, like how you can also come to a conclusion
and I'll show you one type of hyperparameter tuning how, uh, we can specifically do.
So yes, uh, this was it for my side.
I hope you like this particular video.
I'll see you all in the next video.
Thank you.