Day 20 of 100DaysofML

Charan Soneji
100DaysofMLcode
Published in
2 min readJul 6, 2020

KNN or K-Nearest Neighbours. This is a ML based supervised algorithm which students are mostly asked about in interviews and a lot of their exams as well. It's a very interesting algorithm and lets get right into it. So according to wiki, the definition is given as:

In pattern recognition, the k-nearest neighbors algorithm (k-NN) is a non-parametric method proposed by Thomas Cover used for classification and regression. In both cases, the input consists of the k closest training examples in the feature space.

Now, let’s try to make this simple. Let us consider a set of points plotted on a graph and we have categorized them into 3 different clusters. Now, when we get a new point, we need to categorize that point into one of these 3 clusters, so what do we do. Yes, we make use of KNN. Have a look at the diagram given below:

Classification of a random point

So as we may see in the above diagram, the point X is to be classified into one of the 3 classes and to do this, we need to calculate the distance of the point X from each of these class centers. I’ll take another example to explain the concept of distance and centers.

Here, in the diagram, we have 3 main classes- A,B and C and each of them have a center, say A0, B0 and C0 as we may see in the graph given. Now, for a given point X, we need to calculate the distance of the point from each of the given clusters and based on the lowest distance, the point gets classified or put into one of these clusters.

I watched the below given video for further understanding.

Couldn’t cover much today but did how much I could. Keep Learning.

Cheers.

--

--