Sitemap
Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Machine Learning: k-NN Algorithm

--

Press enter or click to view image in full size
k-Nearest Neighbors. Sources: Google Images.

The k-Nearest Neighbors(k-NN) algorithm is simple and easy to implement. It is Supervised Learning technique which can solve both Classification and Regression problems. So, the first question which arise is-

What is Supervised Learning?

Supervised Learning says — “Teach Me”. Which means the learning goes through labelled dataset.

Imagine the model as a child in the family. The child is taught about the difference between cat, dog, etc. after which a child is able to distinguish between the animals which he/she sees. Similarly, the model is taught with the labelled data which helps to learn the difference between the features of the categories of data available and predict the outcome of new observations.

k-Nearest Neighbors (k-NN)

k-Nearest Neighbors Algorithm (k-NN) a non-parametric method used for Classification and Regression. In both cases, the input consists of k closest training data in feature space. The output depends on whether k-NN is used for classification or regression:

Classification:In k-NN classification, the output is a class membership. An object is classified by a plurality vote of its neighbors, with the object being assigned to the class most common among its k nearest neighbors (k is a positive integer),If k= 1, then the object is simply assigned to the class of that single nearest neighbor.

Regression: In k-NN regression, the output is the value for the object. This value is the average of the values of k nearest neighbors.

We all must have heard the word “CART” throughout our learning about Machine Learning techniques.The CART (Classification and Regression Trees) decision tree algorithm can be used to build both classification trees (to classify categorical response variables) and regression trees (to forecast continuous response variables). k-Nearest Neighbor method can create both classification and regression models as well.

k-Nearest Neighbor(k-NN) for Classification:

In pattern recognition, the k-NN algorithm is a method for classifying objects based on closest training examples in the feature space. k-NN is a type of instance-based learning, or lazy learning. The k-NN is the fundamental and simplest classification technique when there is little or no prior knowledge about the distribution of the data. This rule simply retains the entire training set during learning and assigns to each query a class represented by the majority label of its k-nearest neighbors in the training set. In this method each sample should be classified similarly to its surrounding samples. Therefore, if the classification of a sample is unknown, then it could be predicted by considering the classification of its nearest neighbor samples.

The 1-NN decision rule: the point ? is assigned to the class on the left.

Example:-

“ It is k-NN decision rule for K=1 for a set of samples divided into 2 classes. The Nearest Neighbor rule (NN) is the simplest form of k-NN when K= 1. ”- An unknown sample is classified by using only one known sample. Which is clearly visible in the figure.

The k-NN rule decision rule, with K= 4: the point ? is assigned to the class on the left as well

Example:-

Get Gaurav Parihar’s stories in your inbox

Join Medium for free to get updates from this writer.

“It is k-NN decision rule for K=4 for a set of samples divided into 2 classes.”- An unknown sample is classified by using more than one known sample in space. K is set to 4, so that the closest four samples are considered for classifying the unknown one. Three of them belong to the same class, whereas only one belongs to the other class. Which is clearly visible in the figure.

The performance of a k-NN classifier is primarily determined by the choice of K as well as the distance metric applied. The estimate is affected by the sensitivity of the selection of the neighborhood size K, because the radius of the local region is determined by the distance of the Kth nearest neighbor to the query and different K yields different conditional class probabilities. If K is very small, the local estimate tends to be very poor owing to the data sparseness and the noisy, ambiguous or mislabeled points. In order to further smooth the estimate, we can increase K and take into account a large region around the query. Unfortunately, a large value of K easily makes the estimate over smoothing and the classification performance degrades with the introduction of the outliers from other classes.

k-Nearest Neighbor(k-NN) for Regresssion:

The above method can be used for regression, by simply assigning the value for the object to be the average of the values of its K nearest neighbors. It can be useful to weight the contributions of the neighbors, so that the nearer neighbors contribute more to the average than the more distant ones. Regression problems are concerned with predicting the outcome of a dependent variable given a set of independent variables.

Press enter or click to view image in full size
The k-NN decision rule for regression. Source: Google Images.

Example-

Let’s consider the above figure and start with 1-Nearest Neighbor. In this case we search the example set (green squares) and locate the one closest to the new observation X. For this particular case, this happens to be x4. The outcome of x4 (i.e., y4) is thus then taken to be the answer for the outcome of X (i.e., Y). Thus for 1-nearest neighbor we can write: Y = y4.

Now consider, the 2-Nearest Neighbor method. In this case, we locate the first two closest points to X, which happen to be y3 and y4. Taking the average of their outcome, the solution for Y is then given by:

Similarly, if the value of K increases the predicted value is the average of all the nearest neighbor.

Conclusion: After learning about the k-NN algorithm let’s discuss about it’s advantages and disadvantages.

Advantages: k-NN has several main advantages: simplicity, effectiveness, intuitiveness and competitive classification performance in many domains. It is Robust to noisy training data and is effective if the training data is large.

Disadvantages: Despite the advantages given above, k-N has a few limitations. KNN can have poor run-time performance when the training set is large. It is very sensitive to irrelevant or redundant features because all features contribute to the similarity and thus to the classification. By careful feature selection or feature weighting, this can be avoided. Two other disadvantages of the method are:

1. Distance based learning is not clear which type of distance to use and which attribute to use to produce the best results.

2. Computation cost is quite high because we need to compute distance of each query instance to all training samples.

“Thank You for giving your valuable time to read this blog on a Machine Learning Algorithm.”

--

--

Gaurav Parihar
Gaurav Parihar

Written by Gaurav Parihar

Machine Learning Enthusiast, Student of Computer Science, Jaypee Institute of Information Technology.

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com