K-Nearest Neighbors(Machine Learning)

Vatsal Sharma
2 min readJun 23, 2021

--

(IMG Credits-imfunny.net)

K-Nearest Neighbors is one of the simplest Supervised Machine Learning algorithm mostly used for Classification. In simpler words, it says, If you belong to your neighbors then you’re one of them.

What is it?

Mainly used for Classification, KNN is a lazy-learner algorithm in Machine Learning. Here, K refers to the number of neighbors (we can decide that).

Based on the previous datapoints, it predicts the category of the new Datapoint provided. It locates the new datapoint in the space(let us say graph), then the Euclidean Distance is calculated from each datapoint. Now, all the Euclidean Distance are compared & ‘K’ is taken into the account, the most number of points (K) near to the provided datapoint decide it’s category.

(IMG Credits- Google Images)

Why is it called “Lazy Learner?”

(IMG Credits- Anubhav Singh Bassi)
  • It does no training at all when you supply the training data.
  • At training time, all it does is, it stores the complete data set but it does not do any calculations at this point.
  • It doesn’t learn a discriminative function from the training data but “memorizes” the training dataset.

Applications

  • Recommendation Systems.
  • Pattern Recognition.
  • Gene Expression.
  • Measure document similarity.

and many more…

Using KNN

Way 1- Directly importing from scikit learn library in Python.

Input- from sklearn.neighbors import KNeighborsClassifier

This imports the KNN model.

Way 2- Implementing KNN Model from Scratch.

Happy Learning Folks!

You can also visit my previous blogs by clicking on their name below-

Follow me on LinkedIn- https://www.linkedin.com/in/imvat18/

--

--

Vatsal Sharma

Building Yarnit 🚀 | SDE-I @Yarnit | Ex- Data Science Intern @Aiotize | JIIT'22 | Aficionado 🏏 https://www.linkedin.com/in/imvat18/