Hello my friends.
Welcome to this new practical activity.
On this time, the Naive Bayes classification model,
we already implemented logistic regression k nearest neighbors, SVM and kernel SVM.
And so far the best accuracy we've got with these four models
is oh point 93, which we got with both
K-nearest neighbors and kernel SVM.
And now we're about to implement a new classification model,
the Naive Bayes one, a very well known and widely used.
So you definitely want to have it in a toolkit.
And so the big question is will we beat
that best accuracy of 0.03.
And now let's do this.
Let's start the implementation of Naive Bayes.
So we're going to go into this folder
and we're going to start with Python as usual.
And inside this folder you will find two files
the Naive Bayes implementation in the Ipynb format
and the same social network as data set, which I will quickly re-explain.
So this is a dataset containing 400 customers.
Each row here represents the customer, and for each customer
we get the two features age and estimated salary.
With which we are going to predict the dependent variable purchased,
which tells if yes or no, the customer but the SUV.
So zero means that indeed the customer didn't buy the SUV,
and one means that the customer but the SUV.
All right, so we're going to train a new classification model to understand
the correlations
between these two features age and salary, and that dependent variable purchased
in order to predict the customers that will buy the SUV.
And once we get these predictions, we will target these customers
on social networks with some beautiful ad of this beautiful car.
All right, so that's the same story.
And now we're going to start our implementation.
And I can't wait to see if we're going to beat the accuracy.
And I can also to show you the visualization results at the end.
All right.
So right now it is loading and laying out the notebook.
And here it is.
Here is a Naive Bayes implementation
still resulting from the same classification template
which we made in the first section of this part three.
When implementing logistic regression.
So basically all the cells here are the same
as in this logistic regression implementation or classification template.
The only cell that will change will be, you know, the one where we build
and train the classification model, meaning this one.
That's the only cell that we will
re-implement ourselves because all the rest is the same.
But this notebook is in read only mode.
And therefore, in order to re-implement that cell, we need to create
a copy of this notebook by clicking save a Copy in Drive.
And as you can see, this will create a copy
in which we will be able to re-implement that cell.
All right.
So once again laying out the notebook and there we go.
Here is our copy on which we are authorized to modify anything.
And especially that cell we want to re implement.
So I'm scrolling down here to find it.
Here it is training the naive base model on the training set.
So let's immediately
remove that cell because I want you to re-implement it from scratch.
As if we had no idea on how to implement this.
All right, let's create a new code cell.
And now your turn.
I want you to train your machine learning independence by figuring out by yourself
how to indeed build and train that naive base model on the training set.
So of course, to do this, you have several options.
You can directly type in the search bar of Google or Bing.
Well, Naive Bayes scikit learn class okay.
Or you can navigate the cycling API which is right here.
In order to find that class which we need to build our Naive Bayes model.
And my personal recommendation is to try it with the second option,
because indeed, this will get you familiar with this API.
And the more you get familiar with it, the better.
Okay, so please press pause.
Now try to find this class
and then try to build this naive base model in training on the training set.
And now in two seconds we will implement the solution together.
All right let's do this.
Let's build this naive base model with scikit learn.
So the API is huge.
And this is organized in alphabetical order.
And so the first thing we'll try is to find, you know, a module called
maybe Naive Bayes. Right.
Just the name of the model.
So we will scroll down down to N, you know the letter N
and we'll see if we get Naive Bayes somewhere or something close to it.
Let's see.
Linear model meaningful learning metrics we're getting close.
And Gaussian mixture models model selection multiclass.
Well there is a lot of n okay perfect naive Bayes.
So let's click this Naive Bayes.
All right.
And so the model indeed is naive underscore Bayes.
And among these models.
Well according to which one are we going to take here.
That's a good question actually.
Well in fact the classic one and the one that you learned in the intuition
lecture is of course, this one, the Gaussian, naive Bayes, Gaussian and B.
And that's exactly what we'll use to implement our Naive Bayes model.
All right.
So the name of the class is Gaussian B.
And this time
the good news is that we won't have to worry
too much about the parameters because there are actually only two parameters.
And so here very simply
we will just call this class without inputting any parameters.
So that's super easy.
Let's copy this and let's go back to our implementation to copy one.
Let's paste that here.
Let's remove this little thing here.
And now you know how to adapt this.
We need to start with from,
you know, the Naive Bayes module of the scikit learn library.
And then here you add import.
There we go that Gaussian and B class.