Hello everyone, I am (name) from LearnVern. (7 seconds gap, music).
Welcome to Machine learning course and this tutorial is the continuation of the previous session.
So, let’s begin.
Now, we are discussing the KNN algorithm- K nearest neighbour.
As we discussed this algorithm, KNN. which means it sees its nearest neighbour.
Nearest neighbour (5 seconds, typing) ok?
So, now we will see what package we have for this purpose.
We move on to this particular page, and as you can see what package we have here is “sklearn dot neighbours dot KNeighboursClassifier package”.
Here, we can see that sklearn dot neighbours means it comes under neighbours and it is K neighbours classifier which means how many neighbours are to be considered. Here, consideration is regarding distance which means how many neighbours have less distance.
So, let’s see it. Here are these parameters. Like, n numbers, how many neighbours are to be considered which is by default 5. We can also add weights which are uniform by default. Here, we can provide distance. We can generate some other functions also.
Algorithm is ‘auto’ by default, otherwise we can also use the ‘ball_tree’ or ‘kd_tree’ algorithm.
After that, there are other parameters, which we will keep default for now. We will not change them. And there are also attributes.
1:39
After that, as we can see here, there are other classifiers also, RadiusNeighbours, KNeighbourRegressor. There are also regressors, nearest neighbours, and unsupervised learners etc. ok? So, there are other variants of this algorithm which come under regression which we will discuss later. And, these algorithms are for unsupervised learning also. Like, we have a distance-based matrix here, so suppose we don’t have labels at distance levels, even then we can do grouping in unsupervised learning. We will see this while studying unsupervised learning.
Now, see, here we can take a simple example. First, we will see this and after that we will work on the iris dataset.
So, here we have taken two datasets x and y. I will explain to you how these are two datasets.
2:27
As you can see x contains a list which has 1,2,3, 4 - four lists. Similarly, we create a dataset X, generally it is depicted in capital letters. So I am also keeping it capital.
So, I created a list, I put a value of 1 in it. After it I added another value which is 2 by adding a comma. Likewise, I added elements 3 and 4.
In this we got these many values in X.
For Y, as you can see there are four values in a single list. So, we added 0 comma 0 comma 1 comma 1.
This one is input and the other one is output. Now, in the input section, I give bigger values for 3rd and 4th elements as 8 and 9. What will happen with this? Because it will ease out prediction for our algorithm, like the smaller input values have 0 as output for example 1 and 2 have 0 output. And, bigger values like 8 and 9 have 1 as output. So, I took these two datasets X and Y.
3:32
Now sk learn dot neighbours, so this is step one we are moving in step by step and this is step 1 - loading the data.
In step 1, before loading data there can be one step of importing a library or handling a dataset. So, we imported the library from sklearn.neighbours import KNeighboursClassifier. After that we created an object KNeighbourClassifier and mentioned there that only 3 neighbours are to be considered.
After that we did training using the fit method and then we gave predictions. At the time of prediction, we gave 1 value input and got 1 as output.
So, let’s implement this model in this way and after that we will do the next programming with the iris dataset.
4:15
So, here we have
Form sklearn.neighbours
So, what will we import from sklearn.neighbours?
Import KNeighboursClassifiers. We imported this.
I hope you are able to understand me.
After importing, we need to create an object, So, I am creating an object with the name KNN.
So, KNN is equal to KNeighboursClassifier and in this I have to mention how many neighbours there are. So, I can mention n neighbours, I am mentioning 3 neighbours here.
So, I created an object which is this model. We can train it now.
So, KNN dot fit, we will give it training using ‘fit’. Since it is supervised, we will pass both X and Y.
Now this model is trained or it has completed learning. After this, we will use KNN.predict for prediction. So, KNN dot predict, we need to provide one input here; I am giving value 3.
I executed it. As you can see, since 3 is nearer to 1 and 2 so it gave output as 0. Ok.
Now, 5 is nearer to 2 and to 8 as well. Let’s see what output it gives for input 5.
It gave 0 output. If I make it 5.1, it gives output 1.
As you can see, 5 is equidistant from 2 and 8, it is considered 5 nearer to 2 and 5.1 nearer to 8.
So, this is how the KNN- k nearest neighbour classifier works.
Now we move forward.
From sklearn dot datasets import load_iris.
Clear, so far?
Here, our x comma y is equal to load underscore iris, in brackets again we will use here return x y is equal to True, T will be capital here.
So, we have x and y now.
We have already created a model, so I will create another model here named IKNN.
So, IKNN which means iris status claim, i will make one more model and logistics regression, here we are not using logistics, we are using KNeighbours so KNeighborsClassifier.
Here we will change the number of neighbours, I kept it 5 which is by default.
So I kept the number of neighbours as five.
Now, we need to train this model iknn. So, we will use the dot fit method.
So, what will we add in this?
we will put x and y in it. So we added the X and Y.
Now, our model is trained. Now after training we have to make predictions from it.
So, iKNN dot PREDICT, predict.
So we have to pass here with the predict function.
X colon two.
I extract the first two records and all columns from x here in this way.
I passed it here and let’s see what prediction it gives.
It has given 0, 0 predictions.
As we did earlier, I passed 130 and 132 values here. The prediction is 2 as expected.
And it is giving 2 and 2.
So, in this way you can apply KNN algorithm for any dataset.
Try it yourself and if you have any confusion, If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
We will see the further parts in the next session.
So, keep learning, and remain motivated.
Thank you.
Share a personalized message with your friends.