Skip to Main Content
Back to website
Read previous article: Decision Trees Read next article: Naive Bayes
13 mins read

K-Nearest Neighbors (KNN)

K-nearest-neighbor (kNN) classification is one of the most basic and straightforward methods for classification. It is often the go-to choice for classification studies when there is minimal or no prior knowledge about the data’s distribution (Peterson, 2009). This method retains the entire training set during the learning phase and assigns each query to a class based on the majority label of its k-nearest neighbors within the training set.

The simplest form of KNN is the Nearest Neighbor rule (NN), where K = 1 (Imandoust, 2013). In this case, each sample is classified similarly to its closest surrounding samples. If the classification of a sample is unknown, it can be predicted by examining the classification of its nearest neighbor samples. For an unknown sample, the distances between this sample and all the samples in the training set are calculated. The sample with the smallest distance is the closest to the unknown sample, and thus, the unknown sample is classified based on the classification of this nearest neighbor.

Let’s understand the K-nearest-neighbor (kNN) classification with the help of an example. Suppose there are two categories, Category A and Category B, and we have a new data point x1. We need to determine which category this data point belongs to. To solve this problem, we can use the K-NN (K-Nearest Neighbors) algorithm. K-NN helps us identify the category or class of a specific data point by considering its closest neighbors. Refer to the diagram below for a visual representation.

Read previous article: Decision Trees Read next article: Naive Bayes