Hello my friends.
Welcome to this new practical activity on our second classification model,
which is of course the k nearest neighbors and now we're going to go into point
three classification and then k nearest neighbors k and n.
All right.
And as usual we're going to start with Python.
And inside this folder you will indeed find two files.
Of course the k nearest neighbors implementation in ipynb format
which you can open with either Jupyter Notebook or Google Colab.
And the same data set social network ads
containing 400 observations, where each observations
you know, each row corresponds to customers.
And for each of these customers, we get the age.
That's the first feature, the estimated salary.
That's the second feature.
And their purchase decision, whether or not they bought yes or no, the SUV.
And that's of course the dependent variable.
And our exercise here is to learn some correlations between these features.
The age and estimated salary to predict this dependent variable purchased.
And the goal of all this is to of course, predict which customers
are going to buy the new SUV so that we can optimize our targeting.
All right.
So exactly the same.
And now we're going to open this implementation k nearest neighbor
with either Google Collaboratory or Jupyter Notebook.
Choose your favorite.
And now it is opening the notebook laying it out in a second.
So learning it laying it out there we go.
And that's the notebook.
And as you noticed, as you probably noticed, I actually kept
the logistic regression implementation to show you, indeed, that
all the code cells inside these two implementations are the same.
You know, all the code cells are the same, except
the one where we indeed build and train the model on the training set.
Right?
So up to here, you know, all this is the same, all this is the same.
And once we reach that step where we train the classification
model on the train set, well that's where we have this single change.
And then all the rest is the same.
Then we predict a new result, you know, with the same name
for the classifier which we called classifier.
Then we predict test result here once again we have nothing to change.
It's all the same right? All the same.
So if by any chance you are starting with K
and then you know before logistic regression,
I really encourage you to do logistic regression first
because all these cells are explained in detail.
And for this in an implementation we will just re-implement,
you know, that cell to train the K and and model on the training set.
All right. So see.
So that's why it is a classification template
which you can use to build any other classification models.
And together we will of course use this classification template
to build all our other classification models, including K and N support
Vector Machine, kernel, SVM, Naive Bayes, Decision Tree Classification
and Random Forest Classification and we will all build them in maximum efficiency.
All right. So let's start with K.
And as you understood the only cell that we have to change here is this one.
So actually since we cannot do it here you know re-implement it here
because this is in read only mode.
Well we'll have to create a new file, you know, a copy of this file
by clicking here.
This button, save a copy and drive.
This will create a copy.
And inside which we will be able to re-implement
that cell to build and train the K, and then model.
All right.
So let's do this.
We just need to scroll down here.
All the rest is the same. And there we go.
So what we're going to do is just remove that cell and there we go.
Now we can re-implement that cell.
So let's create a new code cell.
And now I would like you to please
press pause on this video to indeed try to do it yourself first.
Because once again,
I not only want to train you on machine learning, but also I want to train you
on how to be independent and create things on your own.
You know, create your machine learning models on your own.
I already guided you on how to navigate the scikit
learn API to find some information in some tools.
And now I would like you to do this same exercise again.
Please press pause on the video and navigate the scikit learn API
to find the class that allows indeed to build the k and end model,
and then you know how to train it on the training set.
All right, so in two seconds now we're going to start the solution together.
Here we go.
All right.
So let's suppose you know I'm just like you I'm not an expert in machine learning.
And I would like to build and train a K in and model on the training set.
So since I actually have no idea of what is the name of the class
that does it, well, I'm going to go of course, to search it online and inside.
Well, we will do it this way scikit learn.
Then we're going to go to the first link.
Right.
We will end up in the scikit learn welcome page.
Then we're going to go remember to API which contains all the
modules and functions and classes of scikit learn.
And now there we go.
We are looking for the class that allows us to build the k and model.
So let's scroll down a bit.
And if we have, you know, too much difficulty to find it,
well we will do Ctrl or command F to find it.
You know, there are many tricks actually.
You can also directly type in your search bar K nearest neighbor class
scikit learn or scikit learn k nearest neighbor class, something like that.
But I like to navigate the secondary API because it is really well made.
And so here by scrolling down, do we find it?
Yes. Here it is. Nearest neighbor.
So it was actually hard to miss.
Indeed we had to scroll down a bit, but that's okay
because it's really good to get familiar with the Scikit-Learn library.