
Nearest neighbor: pattern recognition by looking at the neighbours
Nearest neighbor estimates the properties of an unknown element by looking at the closest element whose properties are known. The technique combines clustering with a distance measure and is particularly strong in pattern and image recognition, provided both settings are right.
Key takeaways
- Nearest neighbor assigns an unknown element to the group of the closest known element.
- The technique is a combination of clustering and a distance calculation as used in linear regression.
- Distance is not by definition geometric distance: you can include other factors, shapes or patterns, which makes the method suitable for image recognition.
- The method depends strongly on prior knowledge: too few known points or a poorly chosen distance definition gives poor results.
- Unlike a neural network, nearest neighbor is not a black box: a wrong choice by the user has clear, traceable consequences.
What is nearest neighbor?
Nearest neighbor is a technique that estimates the properties of an unknown element by using a distance function to find the closest element with known properties and adopting its classification.
Our neighbours are very often our kind of people. They have a similar house and therefore often a similar income and a similar car, and the similarities can go further: the children’s sports clubs, schools and hobbies. Yet there is no uniformity: the neighbours of my neighbours probably differ more from me, and the residents at the end of the street even more. As long as the houses are comparable in size, the similarities decrease as distance increases.
Conversely, the properties of the residents of any house can be estimated by first determining in which part of the neighbourhood it lies and then equating them with those of the nearest house whose properties are known: the nearest neighbor. The technique is one of the methods of predictive analytics and is mainly applied in pattern and image recognition. It consists in fact of a combination of clustering with elements of linear regression.
The problem of the nearest neighbour
Common sense says that determining all properties accurately is impossible, but that it must be possible to divide residents into groups. That in fact means clustering the residents on a number of determining properties, using the data of a limited number of residents, sometimes even from other neighbourhoods. Next, the nearest neighbour of each house has to be determined: which already classified house is as close as possible? How do you determine that distance, and which factors play a part? Is only the geometric distance important, or does another street count too? The concept of distance is not by definition identical to geometric distance. Where necessary you adjust the calculated distance for additional factors.
A schematic example
Assume the properties of five points are known. On that basis each falls into a group: points 1 and 2 are grey, point 3 is white, points 4 and 5 are black. Three new points A, B and C must be assigned to one of the three groups. Measured with the geometric distance, point A lies closest to the white point and point B to a grey point. Point C is a problem, because its distance to the known points 1, 3 and 5 is equal. Two roads are open for point C:
- The status remains undetermined.
- The status is determined again, with the distances to the newly classified points A and B counting as well.
Which road you take depends strongly on the application and the data. Solving the problem comes down to two questions: on the basis of which properties do you form the groups, and how do you determine to which group a new element belongs? The answer depends strongly on the data used and the desired properties, and the technique requires a priori information: some knowledge about the form of the final solution.
One of the great qualities of the method lies in the flexibility of the distance determination. It is possible to include a shape or pattern in it. This is particularly useful in processing image information, such as face recognition in surveillance images and locating organs and bones in medical images. Nearest neighbor is also easy to translate to multiple dimensions.
The limitations: a car dealer’s campaign
The technique is particularly powerful in pattern recognition, provided you choose the settings correctly, and because it combines two techniques, the settings for the clustering and for the distance determination must be right simultaneously. The method therefore depends strongly on the quality of the prior knowledge of the situation.
Suppose a company wants to sell more cars from the lower mid-range segment. To track down potential customers in a certain suburb, the sales department uses nearest neighbor. The starting point is its own customer file in that suburb and the assumption that neighbours often have the same type of car. The settings: cluster from its own customers with a lower mid-range car, and take the houses that are at most two houses away from a known house. The analysis yields a series of addresses that receive an invitation to the showroom with a good offer. To the surprise of the sales department, the response is minimal. Three mistakes were made:
- The reason for the campaign is to expand the number of customers in the neighbourhood, which shows that coverage is insufficient: there are too few known points.
- Adjacent houses do not have to have the same properties (think of the neighbours at the back).
- The chosen distance determination, in combination with the limited coverage, is not sufficient to reach all possible customers.
After two adjustments, increasing the number of known points so that the clustering improves and using a distance measure in which ‘nearest neighbour’ is limited to the people living next door, the campaign suddenly does succeed. Further improvement is possible by including more information in the clustering, such as brand loyalty.
Nearest neighbor versus neural network
Nearest neighbor versus neural network
Both are used for pattern recognition, but they are totally different techniques.
Nearest-neighbor method
Neural-network model
Principle
Estimate via a distance function
Learns a relationship from data
Prior knowledge
For the clustering
For the learning phase
Transparency
Choices are traceable
Black box
Influence of user
Large: settings decide
Small
Main risk
Wrong distance or too few points
Sees patterns that are not there
Both methods use a priori knowledge: the neural network to learn, nearest neighbor for the clustering. The important difference is that a neural network behaves like a black box on which the user has little or no influence, whereas with nearest neighbor a wrong choice in one of the two sub-methods has clear consequences. Which method is most applicable depends on the situation.
How Diamond Decisions applies nearest neighbor
Diamond Decisions uses nearest neighbor for questions of the type ‘which known customers, products or cases does this one resemble?’. After inspiration, business modeling defines what ‘resemble’ means for the decision at hand, data analytics tests whether there are enough known points and which distance measure works, and the decision makers see which neighbours led to each recommendation.
Talk to the author
Herman van DellenDiamond Decisions
Discuss your question with Herman
Tell us which decision needs to improve. In a no-obligation conversation we show you where your data delivers the most.
Schedule an introduction →Step 3 of our approach
Diamond Data Analytics
A data warehouse that starts with your most important decisions and is ready for conversational AI: a working prototype within 24 hours.
Explore Data Analytics →Frequently asked questions
01
What is nearest neighbor?
A technique that classifies an unknown element by looking for the closest element with known properties and adopting its group.
02
What is nearest neighbor used for?
Mainly for pattern and image recognition, such as face recognition and locating organs in medical images, and for recommendations based on similar customers or products.
03
What is the difference between nearest neighbor and a neural network?
Nearest neighbor estimates via a distance function and its choices are traceable. A neural network learns a relationship from data and works as a black box on which the user has little influence.
04
What determines the quality of nearest neighbor?
Enough known points, a good clustering of those points and a distance definition that fits the question. If one of them is wrong, the result is poor.
05
Is nearest neighbor useful for SMEs?
Yes, for example to find prospects who resemble your best customers or to suggest products that similar customers bought. It only works when your own customer file covers the target group sufficiently.
Further reading
About the author
About Diamond Decisions
Diamond Decisions helps organizations become better by enabling better decisions. We don’t start with data, dashboards or AI, but with the decision that needs to improve, and work back from there to KPIs, data and technology. We do that in four steps: inspiration and alignment, business modeling, analytics and data platform and data-driven decision makers. That way BI and AI don’t remain an IT project, but deliver value every day.

