Hello and welcome to this art tutorial.
Today we're going to implement the k nearest neighbors classifier on R.
So let's start right now.
The first thing we're going to do is to set the working directory.
So right now I'm on my desktop.
We're going to go to Machine Learning A-Z folder part three classification.
And then section K Nearest neighbors.
All right. That's the folder we want to set. It's working directory.
Make sure that you have the social network add CSV file.
And if that's the case you're ready to click on this more button
here to set the folder as working directory.
Here we go. That's done.
And now we're going to use our classification template to be efficient
and generate all the results in a flashlight.
So I'm going to select everything from here to the top.
There you go. Copy and
paste it here.
And now we need to change a very few things.
First we need to create the classifier here.
And we just need to change the titles
of our plots in the training set results and the test results.
So let's do this right now so that we don't forget
I'm going to replace classifier by.
So it's k and ends.
We're just doing this to specify the algorithm that we're plotting.
And here as well classifier is replaced by k.
And then right.
And now what we only need to do is to create our classifier.
But first let's select and execute the pre-processing steps.
So I just need to select everything from here
and then press Command Control plus enter to execute.
That's done. All right.
So we have our data sets. We can have a look.
That's our data set our training set and our test set
which are well scaled okay.
So as a reminder the data set contains information
about users in a social network.
The social network has a business client, which is a car company
that puts ads on the social network.
And the social network gathered not only the information
like the age and the estimated salary of those users,
but it also took the response to these users to the ad.
That is zero.
If the user didn't buy
the product, the car, and one if the user bought the product.
So, you know, it's a very cool luxury
SUV launched at a ridiculously low price.
So a lot of people, when they saw the ad said, let's do this, let's get the car.
And we can see there are a lot of buyers.
So yeah, as you can see, it must be a very cool car and cheap.
So now let's go back to and and create our classifier.
So the classifier is a k.
And then classifier.
And to create this we're going to
first import the right library.
We need a library for this which is by the way called class.
So let's do this.
So if by any chance the class
library is not in your packages list, you need to install it using this command.
Install the packages and in parentheses and in quotes the name of the class
which is class, but I think you might have it by default, so.
But if that's not the case, please write this command with class inside.
Then execute and it will install it.
But so here we're just going to write library
and in parentheses class that's selected automatically.
The class you see that right now the class package is not selected.
And once this is executed it will select it automatically.
All right.
And now now we're going to do something a little different
because usually you know we create a class.
If I go here and then we create this vector of prediction y using this predict
function that we apply on our classifier and on new observations in the test set.
But here it's like we're going to do this two steps all in once because
we're actually going to remove this line.
And we are actually going to replace this command
fitting K and end to the training set
and predicting
the test set results.
Because all in once we're going to fit K and into our training set and predict
if the users of our test set by yes or no, the SUV.
So you're going to understand
why we're going to directly create a vector of prediction y pred here.
And then, you know, before we had classifier equals
then the SVM or logistic regression
here, it's going to be directly white bread equals k and n
because actually this k and then function here will return
the predictions of the test set observations okay.
So now let's have a look at this k and in function.
And you'll get it.
So press F1 okay.
So k nearest neighbor classification.
Let's first input the arguments.
The first argument is train.
So you can guess that by train they mean the training set.
So here we need to specify what the training set is.
And actually since we call it training set it's training set.
So train set here.
All right training set.
However the training set as you can see contains
the independent variables and the dependent variable.
And we only need to take the independent variables.
We want to include the dependent variable in the k in in function here.
So here we need to take only the first two columns.
So we're going to add a bracket here.
And then comma because on the left of this comma
all the lines that I'm taking all the observations and that the right of this
comma is the columns I want to include in the training set.
So it's all the columns except for the last one.
So I'm going to put here minus three
which means I'm removing the last column of my training set.