# K-Nearest Neighbours

K-Nearest Neighbors is one of the most basic yet essential classification algorithms in Machine Learning. It belongs to the supervised learning domain and finds intense application in pattern recognition, data mining and intrusion detection.

It is widely disposable in real-life scenarios since it is non-parametric, meaning, it does not make any underlying assumptions about the distribution of data (as opposed to other algorithms such as GMM, which assume a Gaussian distribution of the given data).

We are given some prior data (also called training data), which classifies coordinates into groups identified by an attribute.

As an example, consider the following table of data points containing two features: Now, given another set of data points (also called testing data), allocate these points a group by analyzing the training set. Note that the unclassified points are marked as ‘White’. Intuition
If we plot these points on a graph, we may be able to locate some clusters or groups. Now, given an unclassified point, we can assign it to a group by observing what group its nearest neighbors belong to. This means a point close to a cluster of points classified as ‘Red’ has a higher probability of getting classified as ‘Red’.

Intuitively, we can see that the first point (2.5, 7) should be classified as ‘Green’ and the second point (5.5, 4.5) should be classified as ‘Red’.

Algorithm
Let m be the number of training data samples. Let p be an unknown point.

1. Store the training samples in an array of data points arr[]. This means each element of this array represents a tuple (x, y).
2. ```for i=0 to m:
Calculate Euclidean distance d(arr[i], p).```
3. Make set S of K smallest distances obtained. Each of these distances corresponds to an already classified data point.
4. Return the majority label among S.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

K can be kept as an odd number so that we can calculate a clear majority in the case where only two groups are possible (e.g. Red/Blue). With increasing K, we get smoother, more defined boundaries across different classifications. Also, the accuracy of the above classifier increases as we increase the number of data points in the training set.

Example Program
Assume 0 and 1 as the two classifiers (groups).

## C/C++

 `// C++ program to find groups of unknown ` `// Points using K nearest neighbour algorithm. ` `#include ` `using` `namespace` `std; ` ` `  `struct` `Point ` `{ ` `    ``int` `val;     ``// Group of point ` `    ``double` `x, y;     ``// Co-ordinate of point ` `    ``double` `distance; ``// Distance from test point ` `}; ` ` `  `// Used to sort an array of points by increasing ` `// order of distance ` `bool` `comparison(Point a, Point b) ` `{ ` `    ``return` `(a.distance < b.distance); ` `} ` ` `  `// This function finds classification of point p using ` `// k nearest neighbour algorithm. It assumes only two ` `// groups and returns 0 if p belongs to group 0, else ` `// 1 (belongs to group 1). ` `int` `classifyAPoint(Point arr[], ``int` `n, ``int` `k, Point p) ` `{ ` `    ``// Fill distances of all points from p ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``arr[i].distance = ` `            ``sqrt``((arr[i].x - p.x) * (arr[i].x - p.x) + ` `                 ``(arr[i].y - p.y) * (arr[i].y - p.y)); ` ` `  `    ``// Sort the Points by distance from p ` `    ``sort(arr, arr+n, comparison); ` ` `  `    ``// Now consider the first k elements and only ` `    ``// two groups ` `    ``int` `freq1 = 0;     ``// Frequency of group 0 ` `    ``int` `freq2 = 0;     ``// Frequency of group 1 ` `    ``for` `(``int` `i = 0; i < k; i++) ` `    ``{ ` `        ``if` `(arr[i].val == 0) ` `            ``freq1++; ` `        ``else` `if` `(arr[i].val == 1) ` `            ``freq2++; ` `    ``} ` ` `  `    ``return` `(freq1 > freq2 ? 0 : 1); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 17; ``// Number of data points ` `    ``Point arr[n]; ` ` `  `    ``arr.x = 1; ` `    ``arr.y = 12; ` `    ``arr.val = 0; ` ` `  `    ``arr.x = 2; ` `    ``arr.y = 5; ` `    ``arr.val = 0; ` ` `  `    ``arr.x = 5; ` `    ``arr.y = 3; ` `    ``arr.val = 1; ` ` `  `    ``arr.x = 3; ` `    ``arr.y = 2; ` `    ``arr.val = 1; ` ` `  `    ``arr.x = 3; ` `    ``arr.y = 6; ` `    ``arr.val = 0; ` ` `  `    ``arr.x = 1.5; ` `    ``arr.y = 9; ` `    ``arr.val = 1; ` ` `  `    ``arr.x = 7; ` `    ``arr.y = 2; ` `    ``arr.val = 1; ` ` `  `    ``arr.x = 6; ` `    ``arr.y = 1; ` `    ``arr.val = 1; ` ` `  `    ``arr.x = 3.8; ` `    ``arr.y = 3; ` `    ``arr.val = 1; ` ` `  `    ``arr.x = 3; ` `    ``arr.y = 10; ` `    ``arr.val = 0; ` ` `  `    ``arr.x = 5.6; ` `    ``arr.y = 4; ` `    ``arr.val = 1; ` ` `  `    ``arr.x = 4; ` `    ``arr.y = 2; ` `    ``arr.val = 1; ` ` `  `    ``arr.x = 3.5; ` `    ``arr.y = 8; ` `    ``arr.val = 0; ` ` `  `    ``arr.x = 2; ` `    ``arr.y = 11; ` `    ``arr.val = 0; ` ` `  `    ``arr.x = 2; ` `    ``arr.y = 5; ` `    ``arr.val = 1; ` ` `  `    ``arr.x = 2; ` `    ``arr.y = 9; ` `    ``arr.val = 0; ` ` `  `    ``arr.x = 1; ` `    ``arr.y = 7; ` `    ``arr.val = 0; ` ` `  `    ``/*Testing Point*/` `    ``Point p; ` `    ``p.x = 2.5; ` `    ``p.y = 7; ` ` `  `    ``// Parameter to decide group of the testing point ` `    ``int` `k = 3; ` `    ``printf` `(``"The value classified to unknown point"` `            ``" is %d.\n"``, classifyAPoint(arr, n, k, p)); ` `    ``return` `0; ` `} `

## Python

 `# Python3 program to find groups of unknown ` `# Points using K nearest neighbour algorithm. ` ` `  `import` `math ` ` `  `def` `classifyAPoint(points,p,k``=``3``): ` `    ``''' ` `     ``This function finds the classification of p using ` `     ``k nearest neighbor algorithm. It assumes only two ` `     ``groups and returns 0 if p belongs to group 0, else ` `      ``1 (belongs to group 1). ` ` `  `      ``Parameters -  ` `          ``points: Dictionary of training points having two keys - 0 and 1 ` `                   ``Each key have a list of training data points belong to that  ` ` `  `          ``p : A tuple, test data point of the form (x,y) ` ` `  `          ``k : number of nearest neighbour to consider, default is 3  ` `    ``'''` ` `  `    ``distance``=``[] ` `    ``for` `group ``in` `points: ` `        ``for` `feature ``in` `points[group]: ` ` `  `            ``#calculate the euclidean distance of p from training points  ` `            ``euclidean_distance ``=` `math.sqrt((feature[``0``]``-``p[``0``])``*``*``2` `+``(feature[``1``]``-``p[``1``])``*``*``2``) ` ` `  `            ``# Add a tuple of form (distance,group) in the distance list ` `            ``distance.append((euclidean_distance,group)) ` ` `  `    ``# sort the distance list in ascending order ` `    ``# and select first k distances ` `    ``distance ``=` `sorted``(distance)[:k] ` ` `  `    ``freq1 ``=` `0` `#frequency of group 0 ` `    ``freq2 ``=` `0` `#frequency og group 1 ` ` `  `    ``for` `d ``in` `distance: ` `        ``if` `d[``1``] ``=``=` `0``: ` `            ``freq1 ``+``=` `1` `        ``elif` `d[``1``] ``=``=` `1``: ` `            ``freq2 ``+``=` `1` ` `  `    ``return` `0` `if` `freq1>freq2 ``else` `1` ` `  `# driver function ` `def` `main(): ` ` `  `    ``# Dictionary of training points having two keys - 0 and 1 ` `    ``# key 0 have points belong to class 0 ` `    ``# key 1 have points belong to class 1 ` ` `  `    ``points ``=` `{``0``:[(``1``,``12``),(``2``,``5``),(``3``,``6``),(``3``,``10``),(``3.5``,``8``),(``2``,``11``),(``2``,``9``),(``1``,``7``)], ` `              ``1``:[(``5``,``3``),(``3``,``2``),(``1.5``,``9``),(``7``,``2``),(``6``,``1``),(``3.8``,``1``),(``5.6``,``4``),(``4``,``2``),(``2``,``5``)]} ` ` `  `    ``# testing point p(x,y) ` `    ``p ``=` `(``2.5``,``7``) ` ` `  `    ``# Number of neighbours  ` `    ``k ``=` `3` ` `  `    ``print``(``"The value classified to unknown point is: {}"``.\ ` `          ``format``(classifyAPoint(points,p,k))) ` ` `  `if` `__name__ ``=``=` `'__main__'``: ` `    ``main() ` `     `  `# This code is contributed by Atul Kumar (www.fb.com/atul.kr.007) `

Output:

```The value classified to unknown point is 0.
```

This article is contributed by Anannya Uberoi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.