A Detailed Introduction to K-Nearest Neighbor (KNN) Algorithm

K Nearest Neighbor (KNN from now on) is one of those algorithms that are very simple to understand but works incredibly well in practice. Also it is surprisingly versatile and its applications range from vision to proteins to computational geometry to graphs and so on . Most people learn the algorithm and do not use it much which is a pity as a clever use of KNN can make things very simple. It also might surprise many to know that KNN is one of the top 10 data mining algorithms. Lets see why this is the case !

In this post, I will talk about KNN and how to apply it in various scenarios. I will focus primarily on classification even though it can also be used in regression). I also will not discuss much about Voronoi diagram or tessellation.

KNN Introduction

KNN is an non parametric lazy learning algorithm. That is a pretty concise statement. When you say a technique is non parametric , it means that it does not make any assumptions on the underlying data distribution. This is pretty useful , as in the real world , most of the practical data does not obey the typical theoretical assumptions made (eg gaussian mixtures, linearly separable etc) . Non parametric algorithms like KNN come to the rescue here.

It is also a lazy algorithm. What this means is that it does not use the training data points to do any generalization. In other words, there is no explicit training phase or it is very minimal. This means the training phase is pretty fast . Lack of generalization means that KNN keeps all the training data. More exactly, all the training data is needed during the testing phase. (Well this is an exaggeration, but not far from truth). This is in contrast to other techniques like SVM where you can discard all non support vectors without any problem. Most of the lazy algorithms – especially KNN – makes decision based on the entire training data set (in the best case a subset of them).

The dichotomy is pretty obvious here – There is a non existent or minimal training phase but a costly testing phase. The cost is in terms of both time and memory. More time might be needed as in the worst case, all data points might take point in decision. More memory is needed as we need to store all training data.

Assumptions in KNN

Before using KNN, let us revisit some of the assumptions in KNN.

KNN assumes that the data is in a feature space. More exactly, the data points are in a metric space. The data can be scalars or possibly even multidimensional vectors. Since the points are in feature space, they have a notion of distance – This need not necessarily be Euclidean distance although it is the one commonly used.

Each of the training data consists of a set of vectors and class label associated with each vector. In the simplest case , it will be either + or – (for positive or negative classes). But KNN , can work equally well with arbitrary number of classes.

We are also given a single number "k" . This number decides how many neighbors (where neighbors is defined based on the distance metric) influence the classification. This is usually a odd number if the number of classes is 2. If k=1 , then the algorithm is simply called the nearest neighbor algorithm.

KNN for Density Estimation

Although classification remains the primary application of KNN, we can use it to do density estimation also. Since KNN is non parametric, it can do estimation for arbitrary distributions. The idea is very similar to use of Parzen window . Instead of using hypercube and kernel functions, here we do the estimation as follows – For estimating the density at a point x, place a hypercube centered at x and keep increasing its size till k neighbors are captured. Now estimate the density using the formula,

Where n is the total number of V is the volume of the hypercube. Notice that the numerator is essentially a constant and the density is influenced by the volume. The intuition is this : Lets say density at x is very high. Now, we can find k points near x very quickly . These points are also very close to x (by definition of high density). This means the volume of hypercube is small and the resultant density is high. Lets say the density around x is very low. Then the volume of the hypercube needed to encompass k nearest neighbors is large and consequently, the ratio is low.

The volume performs a job similar to the bandwidth parameter in kernel density estimation. In fact , KNN is one of common methods to estimate the bandwidth (eg adaptive mean shift) .

KNN for Classification

Lets see how to use KNN for classification. In this case, we are given some data points for training and also a new unlabelled data for testing. Our aim is to find the class label for the new point. The algorithm has different behavior based on k.

Case 1 : k = 1 or Nearest Neighbor Rule

This is the simplest scenario. Let x be the point to be labeled . Find the point closest to x . Let it be y. Now nearest neighbor rule asks to assign the label of y to x. This seems too simplistic and some times even counter intuitive. If you feel that this procedure will result a huge error , you are right – but there is a catch. This reasoning holds only when the number of data points is not very large.

If the number of data points is very large, then there is a very high chance that label of x and y are same. An example might help – Lets say you have a (potentially) biased coin. You toss it for 1 million time and you have got head 900,000 times. Then most likely your next call will be head. We can use a similar argument here.

Let me try an informal argument here - Assume all points are in a D dimensional plane . The number of points is reasonably large. This means that the density of the plane at any point is fairly high. In other words , within any subspace there is adequate number of points. Consider a point x in the subspace which also has a lot of neighbors. Now let y be the nearest neighbor. If x and y are sufficiently close, then we can assume that probability that x and y belong to same class is fairly same – Then by decision theory, x and y have the same class.

The book "Pattern Classification" by Duda and Hart has an excellent discussion about this Nearest Neighbor rule. One of their striking results is to obtain a fairly tight error bound to the Nearest Neighbor rule. The bound is

Where is the Bayes error rate, c is the number of classes and P is the error rate of Nearest Neighbor. The result is indeed very striking (atleast to me) because it says that if the number of points is fairly large then the error rate of Nearest Neighbor is less that twice the Bayes error rate. Pretty cool for a simple algorithm like KNN. Do read the book for all the juicy details.

Case 2 : k = K or k-Nearest Neighbor Rule

This is a straightforward extension of 1NN. Basically what we do is that we try to find the k nearest neighbor and do a majority voting. Typically k is odd when the number of classes is 2. Lets say k = 5 and there are 3 instances of C1 and 2 instances of C2. In this case , KNN says that new point has to labeled as C1 as it forms the majority. We follow a similar argument when there are multiple classes.

One of the straight forward extension is not to give 1 vote to all the neighbors. A very common thing to do is weighted kNN where each point has a weight which is typically calculated using its distance. For eg under inverse distance weighting, each point has a weight equal to the inverse of its distance to the point to be classified. This means that neighboring points have a higher vote than the farther points.

It is quite obvious that the accuracy *might* increase when you increase k but the computation cost also increases.

Some Basic Observations

1. If we assume that the points are d-dimensional, then the straight forward implementation of finding k Nearest Neighbor takes O(dn) time. 2. We can think of KNN in two ways – One way is that KNN tries to estimate the posterior probability of the point to be labeled (and apply bayesian decision theory based on the posterior probability). An alternate way is that KNN calculates the decision surface (either implicitly or explicitly) and then uses it to decide on the class of the new points. 3. There are many possible ways to apply weights for KNN – One popular example is the Shephard’s method. 4. Even though the naive method takes O(dn) time, it is very hard to do better unless we make some other assumptions. There are some efficient data structures like KD-Tree which can reduce the time complexity but they do it at the cost of increased training time and complexity. 5. In KNN, k is usually chosen as an odd number if the number of classes is 2. 6. Choice of k is very critical – A small value of k means that noise will have a higher influence on the result. A large value make it computationally expensive and kinda defeats the basic philosophy behind KNN (that points that are near might have similar densities or classes ) .A simple approach to select k is set 7. There are some interesting data structures and algorithms when you apply KNN on graphs – See Euclidean minimum spanning tree and Nearest neighbor graph .

8. There are also some nice techniques like condensing, search tree and partial distance that try to reduce the time taken to find the k nearest neighbor. Duda et al has a discussion of all these techniques.

Applications of KNN

KNN is a versatile algorithm and is used in a huge number of fields. Let us take a look at few uncommon and non trivial applications.

1. Nearest Neighbor based Content Retrieval This is one the fascinating applications of KNN – Basically we can use it in Computer Vision for many cases – You can consider handwriting detection as a rudimentary nearest neighbor problem. The problem becomes more fascinating if the content is a video – given a video find the video closest to the query from the database – Although this looks abstract, it has lot of practical applications – Eg : Consider ASL (American Sign Language) . Here the communication is done using hand gestures.

So lets say if we want to prepare a dictionary for ASL so that user can query it doing a gesture. Now the problem reduces to find the (possibly k) closest gesture(s) stored in the database and show to user. In its heart it is nothing but a KNN problem. One of the professors from my dept , Vassilis Athitsos , does research in this interesting topic – See Nearest Neighbor Retrieval and Classification for more details.

2. Gene Expression This is another cool area where many a time, KNN performs better than other state of the art techniques . In fact a combination of KNN-SVM is one of the most popular techniques there. This is a huge topic on its own and hence I will refrain from talking much more about it.

3. Protein-Protein interaction and 3D structure predictionGraph based KNN is used in protein interaction prediction. Similarly KNN is used in structure prediction.

References

There are lot of excellent references for KNN. Probably, the finest is the book "Pattern Classification" by Duda and Hart. Most of the other references are application specific. Computational Geometry has some elegant algorithms for KNN range searching. Bioinformatics and Proteomics also has lot of topical references.

Like this:

Related

86 Responses

The Bayes error rate metric is interesting! Does a neighbor’s error rate influence the classification of a point and hence its error rate?

Since the set is a feature set, noisy irrelevant features could degrade the kNN’s performance. And I read that you could smooth the features using evolutionary algorithms.
Whereas, I have worked on multi-objective EA (NSGA2, SPEA) and kNN is a favorite choice to cluster fit alleles and promote them to a next generation. Something like mutually-beneficial!

Could u suggest me , how would be the combination of Naive Bayes (at first stage) and KNN (at second stage). This is in the case of application of Hydrid Machine Learning tech.to network traffic to classify trusted and untrusted packets.

What is ur suggestion. KNN should be used in first or second stage.Pls suggest.

thanks for the information. Actually, i have a question. What will you do if you have 3 classes. It’s highly possible you will run into the situation that there does not exist such a dominating type right?

That is correct . It is possible that you will run into cases where the Pr(X|class1) is approximately same as the probability of P(X|class2), P(X|class3) etc. There are a few solutions. One of them is to assign different weights to each distance instead of giving them uniform weights. So a point that is closer to test point will get more weight that the point that is farther away. It is still possible to have a degenerate case where the weighted distances are approximately equal in which case we can decide to randomly allocate it to one of the classes.

The last method by using randomness is exactly what I did. And I think it’s also reasonable to use weight, but I am not so comfortable about choosing it wisely. I was thinking about reduce the number of neighbors, and compare it again, which kind of mimic the idea of weighting.

You can use the inverse of the distance as a weight. So this means that the closer points will have higher weight and farther points have lesser weight. This is slightly more principled approach that giving arbitrary weights.

Selecting k is a critical and I think is still a research area. There are some basic tips though :

1.If #classes = 2, then select k as odd to avoid ties.
2. Use some technique like cross validation. If you increase the number of folds and compare it with the results, that might give some hint on it.
3. A similar idea is to use elbow method that is used for k-means clustering.

I am have not read much about choice of k so your best bet might be to ask in some forums like meta optimize.

Hi,
I was trying to do a matlab implementation of K-NN density estimation for 1D data. Since I have a 1-D hypercube, my V is just the distance of the kth point.
I can see that for those points whose kth neighbor is very close the estimated densities exceed 1. What should I be doing?

I am not sure if I understood your question correctly – From my understanding, you have a set of points and given a point , you want to estimate the density at that point.

I am not sure how you will get a value > 1 for this case. Lets say training set is -3, -2, 1, 2 and 3 . Lets also assume you want to find density at point 0.

1) Let k =1. The nearest point here is 1 at a distance of 1. But since this is a half-line centered at 0 and extends to 1 on one side, it becomes a line segment from -1 to 1 with length 2. So the density is (1/5) / 2 which is 0.1 . ie you keep checking for new points between , say, -0.1 to 0.1, -0.2 to 0.2 and so on. At (-1,1), you got a point. But now the length is 2.
2) Let k =2. The nearest point is at 1. There are equally nearby points at -2 and 2. So the density is (3/5) / 4 or 3/20. Or if you just use the formula (k/n) / V, it is (2/5) / 4 which is 0.1 .

I “think”, not considering the “entire” length could be your problem.

Again, it is well known that the output of knn for small k and n is not really a valid density as it typically diverges or plain discontinuous. If you need any other info, let me know.

Hi this was a great explanation of KNN. However, I got a little lost after the beginning. I was wondering if you could recommend some books to understand the fundamentals with regards to intelligent algorithms.

To be more specific, when you talk about metric space, scalars and vectors, I get a little lost. I do have a mathematical understanding of these scalars/vectors matrices etc… But I fail at understanding how they apply to intelligent algorithms.

So if you do know of a good book or two that teaches the fundamentals of these concepts, it would be really helpful, as I’m very keen on the subject of text classifying.

There are lot of excellent books for understanding these data mining algorithms – my favorite is the book by Duda and Hart (Pattern Recognition) and by Christopher Bishop (Pattern Recognition and Machine Learning). Both of them (Especially PRML) have initial chapters that discuss the math preliminaries.

Good day. Thanks for the great work you did. It’s been very helpful. Pls, I am thinking of doing a comparative work using k-means model and k-NN model in recognizing and classifying stock patterns. How do you see it?

You will need the past time series data of the stock. Then you can use the current stock price to find the k nearest values in the time series. But instead of directly using them you will use their successors in the time series as the estimate. Of course you can use weighted data to smooth the variations because voting directly does not work (unless you just want to know if it goes up or down).

Sounds like an assignment problem to me. Here is one possible way : Given any point , k can vary between 1 to 4. If you take one out, it varies between 1 and 3. So , remove first point. For the remaining 4 points, vary K between 1 and 3 and check which gives the best result in terms of RMSE or any other error metric. Now remove second point and repeat same experiment for points 1,3,4,5 . Rinse and repeat !

i am a student and i would like to use kNN for my thesis. i have some problems in visualizing on what happens during the process. can you provide me some graphs, figures, or any picture about what would happen during the process?.. thanks..

Can you please finish this sentence “Where n is the total number of…”? I am not clear whether you are referring to the number of points or the dimensions of the feature set. I’ve been experimenting with your approximation of sqrt(n) for k, assuming n is the number of data points, but I get very poor results. I think this is because my classes are not very evenly distributed, and I have many more sample points with one particular (background) classification. Looks like I may need to look into some more complex methods for determining k.

Hello can you help me…. i don’t no how to code the k-nn algorithm to robot and the other codes. the concept of my proposal are identified the colors of banana and count in different and save and them send the report through SMS through bluetooth”,

There are lot of codes available in the internet to do KNN based on different media type (vectors,images,video etc). I would suggest googling it . I personally have no idea about vision or image processing. 🙂

Excellent post. Just one comment on the idea (not yours maybe 🙂 to use cross-validation to figure out the optimal k: Computing the performance of knn on a fixed data set with N points is already (N-fold) cross validated. So there’s no more information to get from repeating this in separate cross validation…

Hi Saravanan,
I am trying to read your blob to see if it is applicable to myuse cass.

I have thousands to ten-thousands of data points (x,y)coming from 5 to 6 different source. I need to uniquely group them based on certain distance criteria in such a way that the formed group should exactly contain only one input from each source and each of them in the group should be within certain distance d. The groups formed should be the best possible match.
1.Is this a combination of clustering and nearest neighbor?
2.What are the recommendation for the algorithms?
3.Are there any open source available for it?

I see many references saying KD tree implementation and k-clustering etc. I am not sure how can I tailor to this specific need.

I posted teh above question in another website and I got reply saying mine is not a clustering since clustering always is a random association of points. My requirement might be a 5 fold Nearest Neighbor Join. I want to understand what it is. Please send any inputs that might have.

That is very goos blog, I have one question. Can you please see if you can answer it. I have thousands to ten-thousands of data points (x,y)coming from 5 to 6 different source. I need to uniquely group them based on certain distance criteria in such a way that the formed group should exactly contain only one input from each source and each of them in the group should be within certain distance d. The groups formed should be the best possible match.
1.Is this a combination of clustering and nearest neighbor?
2.What are the recommendation for the algorithms?
3.Are there any open source available for it?

I see many references saying KD tree implementation and k-clustering etc. I am not sure how can I tailor to this specific need. I got some responses saying this is not really a clustering case since clustering clusters a group of objects, need not be based on the source/count and recommendation was to use NN join. What is meant by Nearest Neighbor join?

Hi,
This was a very neat explanation of K-nearest algorithm. Thank you. I am currently planning to use this algorithm in extracting temporal relation between events within a given textual annotated data but I am not very clear on how to go about it using this algorithm. I am aware of the SVM machine learning algorithm used in this area but using KNN is more simpler and easier and so, I want to give it a shot. Do you know of any source that would provide the details of its application in temporal relation analysis or text mining concepts?
Thank you.

Respected Sir,
The post explains very clearly about K-NN classifier. I got the answers for many of the questions which i had for a very long days. Kindly post some article regarding the support vector machines (SVM )Classifiers too…..

Hi,
This is a great post for understanding basics of K-NN. Kudos to Saravanan Thirumuruganathan.

My question is about the number of classes. Say we have data from only one class for a two-class problem (normal-class and abnormal-class). Given a test-point and normal-class data points, is it possible to use 1-NN to say whether the test-point is a member of normal-class? I think this might be called an outlier detection problem as well.

hi
I’ll appreciate if you can point me to code implementation of KNN algorithm in R. I am pursuing my masters in economics and i don’t want to use in-built class in R. I’ll greatly understand it if i can take a look at the implementation code.
Thanks

i am priya. doing my M.E. i want the code of knn algorithm in java for my project. i searched in google. bt i’m nt getting the exact one. sir pls do post me a link or reply the link to priyasaishu@gmail.com

Concrete question: let’s say that I have N, n-dimensional data points. I want to find an estimate for my (N+1)st data points having attributes (a1, a2, …, an). Now, I identify my k-nearest neighbors to my (N+1)st and produce an estimate for this data point. How do I produce a confidence interval for this estimate? Is it as simple as computing the sample standard deviation of the k points used and building a confidence interval about the estimate obtained for the (N+1)st point? Or is there an additional penalty that’s involved?