“Show me who your friends are and I’ll tell you who you are?” . This is the assumption K-nn uses in order to identify class label of a point.

In Machine Learning K-nn comes under non-parametric techniques used for both classification and regression . The idea is simple just look for k Nearest Neighbors of a point to choose its class label.The Goal of this article is cover mathematical aspects of k-nn .

K-nn for classification

The idea behind classification is simple , just take majority vote of class labels among k-Nearest Neighbors .The class label with highest votes is assigned as class label of the point. This makes implementing knn extremely simple (for illustration see fig below) .

**Knn Alogrithm for classification:**

- Choose k .
- Find distance of every point to every point .
- Find the k Nearest Neighbor of the query point.
- Take majority vote .
- assign class label with highest vote.

K-nn for regression

The idea behind regression is simple ,find k-Nearest Neighbors and take the mean or median(preferred) of the k Nearest Neighbors and assign as that as the value of query point. There is no such figure present which can graphically describe the knn for regression.

**Knn algorithm for regression:**

- Choose k .
- Find distance of every point to every point .
- Find the k Nearest Neighbor of the query point.
- Take mean or median of k-Neighbors .
- assign it as the value .

There are two factors which directly affects the accuracy and performance of Knn , the choice of distance metric and value of k.Let’s discuss first what distance metric to use.

There are many distance metric available but we will focus on two most popular techniques:

- Euclidean distance
- Manhattan distance

**Euclidean distance** :This can be interpreted as direct distance from one point to another or in simple words pythagoras theorem. The euclidean distance from a point to another can easily be calculated as follows.

Let and , so Euclidean distance will be ,

**Manhattan Distance : **This can be interpreted as path followed distance from one point to another or in simple words distance between two points which passes through points in between. The manhatton distance from a point to another can easily be calculated as follows.

Let and , so Manhattan distance will be ,

Now to choose the best value of k there are options available ,but this must be noted that these methods are not general to all.

- Choose K on the basis of intuition .(Not recommended)
- Choose K on the basis of data (very problem specific).
- Use cross validation data to choose best k (Recommended approach).

There are also some modified versions of Knn:

- Weighted knn
- Using Minkowski distance metric.

Knn is one of the most simplest approaches in whole machine learning ,.Even with this simplicity knn can give very good results .Hope you enjoyed reading it.

“

HAPPY MACHINE LEARNING”