K-Nearest Neighbors (KNN)
A method that finds the observations closest to a new data point and classifies it based on its neighbors' species.
How can you tell the species of a wildflower you've never seen before? If its petal length and width are similar to flowers you've already observed, is it likely to be the same species? And if different species are mixed together nearby, what should you base your judgment on?
K-nearest neighbors (KNN) is a method that finds the data points closest to a new observation and predicts its species from the information of those neighbors. Rather than working out a complicated classification formula in advance, it calculates distances to the stored training data.
Discriminant analysis assumes a distribution for each group and derives a decision boundary, and SVM optimizes the margin that separates the groups. KNN, by contrast, classifies by looking directly at which species are gathered around the new flower and how many of each. The results can change with the number of neighbors K and how distance is defined.
KNN can be used for biological classification, grouping similar customers, pattern recognition, recommendations, simple baseline prediction models and more. In this article we'll use wildflower measurements to look at how KNN works, how to set K, distance and standardization, how to read classification results, how it differs from other models and what to watch out for in the analysis.
What is KNN?
A supervised learning method that classifies a new observation using information from nearby observations.
The training data record the petal length and width and sepal length and width of flowers whose species is already known. When a new flower comes in, KNN looks for training examples with similar values on these four measurements. In classification, the basic approach is to compare the species of the K closest flowers and predict the species that appears most often.
KNN doesn't necessarily require a linear boundary or an assumption of normality. But the distance measure that decides what counts as close, and the units of the variables, directly affect the results.
Why use KNN?
It's useful when you want to classify by the similarity of observations without specifying a complicated function in advance.
Even if flower species can't be cleanly separated by petal length alone, looking at several measurements together can bring flowers of the same species close to each other. KNN uses this kind of local structure for classification.
It is easy to understand as a first comparison model and can classify several species at once, but when the training data are very large, searching for neighbors for every new flower can get computationally expensive. There's also the problem that distance becomes less meaningful as the number of dimensions grows.
How does KNN work?
It predicts in this order: calculate distances → pick the K closest → take a majority vote.
① Represent the flower measurements as coordinates
The lengths and widths of the petals and sepals are the input variables. To make it easier to follow, the figure shows only the two petal variables; an actual classification can use all four variables together.
② Calculate the distance between the new flower and the training data
With Euclidean distance, you square the differences on each variable, add them up and take the square root. You can use other distance measures as well, and the input variables should be on similar scales.
③ Pick the K closest flowers
With K=5, you find five neighbors and check each neighbor's species. If you set up distance-based weights, closer neighbors can also get more votes.
④ Predict the species with the most votes
For example, if the five flowers are 1 of species A, 3 of B and 1 of C, the new flower is classified as B. You also need to set a rule for handling ties.
Worked example: classifying wildflower species
Three species are distinguished using four measurements of each flower.
Suppose a researcher collected 180 hypothetical wildflowers. There are 60 flowers of each of the three species A, B and C, and the petal length and width and sepal length and width of each flower were recorded.
| Variable | Example value | Role in analysis |
|---|---|---|
| Petal length | 2.9 cm | Predictor |
| Petal width | 1.0 cm | Predictor |
| Sepal length | 5.6 cm | Predictor |
| Sepal width | 2.8 cm | Predictor |
| Species | A / B / C | Target to classify |
Of the 180 flowers, 135 are used as training data and 45 as the final test data. Assume a stratified split so that each species contributes 15 flowers to the test data. The classification results below are hypothetical figures constructed to explain the concepts.
What should you check before the analysis?
First look at units and measurement errors, which change what distance means.
| Check | What to examine | Why |
|---|---|---|
| Data units | Confirm all four measurements are in cm | Consistent distance calculation |
| Variable distributions | Check ranges and outliers | Keep one variable from dominating distance |
| Standardization | Compute the transformation from the training data | Adjust for differences in scale between variables |
| Duplicate specimens | Check whether the same flower was measured repeatedly | Prevent leakage between training and test |
| Class proportions | Check the sample size for each species | Reliability of evaluation for minority species |
| New conditions | Whether samples from other seasons or regions are included | Confirm the scope of application |
In what order do you run a KNN analysis?
Proceed in this order: split into training and test, define distance, choose K, evaluate at the end.
① Define the target and the measurement variables
Set species as the correct label and the four petal and sepal measurements as input variables.
② Split into training and test data
Set aside the 45 test flowers for the final check, and choose settings by cross-validation within the 135 training flowers.
③ Decide on standardization, distance and weighting
You can compare standard Euclidean distance, Manhattan distance, equal weights and distance weights.
④ Choose K by cross-validation
Evaluate several values of K with the same cross-validation procedure, and look at both validation performance and stability.
⑤ Check performance on the final test data
After K is chosen, use the held-out test data once to calculate the confusion matrix and per-species precision and recall.
How do you choose K?
A small K is sensitive to small local changes, while a large K looks at a wider area.
| K | Characteristics | Caution |
|---|---|---|
| K=1 | Decides by the species of the single closest flower | Sensitive to noise and outliers |
| K=3 | Majority vote of the three closest flowers | Reflects local patterns |
| K=5 | Combines the species of five flowers | Boundary may be relatively stable |
| K=15 | Reflects the distribution over a wider neighborhood | Minority patterns may be drowned out |
A small K isn't always more accurate, and an odd K isn't always right. With three species, ties can occur even with an odd K. In this example, assume K=5 was chosen by cross-validation within the training data.
How do you interpret the classification results?
Check not only overall accuracy but also which species were confused with each other.
These are the results of classifying the 45 hypothetical test flowers with K=5. Rows are actual species and columns are predicted species.
| Actual \ Predicted | A | B | C | Total |
|---|---|---|---|---|
| A | 14 | 1 | 0 | 15 |
| B | 1 | 12 | 2 | 15 |
| C | 0 | 2 | 13 | 15 |
| Total | 15 | 15 | 15 | 45 |
Overall accuracy is 86.7% (39/45). Recall is 93.3% (14/15) for species A, 80.0% (12/15) for B and 86.7% (13/15) for C. Some B and C flowers are confused with each other, a difference that accuracy alone can't reveal.
How do distance and variable standardization affect the results?
Even with the same K, the neighbors change depending on how distance is calculated.
For example, if petal length is a few cm and some additional sensor value is measured in the hundreds, the sensor value can largely dictate similarity under plain Euclidean distance. Standardization puts the variables on the same scale, but it can also give meaningless variables the same weight, so variable selection matters too.
| Option | Meaning | Things to consider |
|---|---|---|
| Euclidean distance | Based on the sum of squared differences across variables | Sensitive to large gaps |
| Manhattan distance | Sum of absolute differences across variables | Adds up the differences on each axis |
| Uniform weights | Gives each of the K neighbors one equal vote | Distant neighbors have the same influence |
| Distance weights | Closer neighbors have more influence | Can be sensitive to noise very close by |
What matters for the classification is not the absolute size of the distances themselves but which observations are chosen as nearest neighbors. If you change the variables or the distance method, choose K again by cross-validation.
What to watch out for in KNN
Check whether closeness really means similarity.
Did you standardize using the test data too?
If you standardize with statistics from the full data and then split, information from the test data leaks into the training process.
Are there too many dimensions?
As the number of variables grows, the distances between most observations can become similar. Consider selecting meaningful variables or reducing dimensions.
Did you try only one K?
K=1 or K=5 isn't always appropriate. Compare a range of candidates in cross-validation.
Are the same specimens in both training and test data?
If repeated measurements of the same flower appear on both sides, classification performance can be inflated. Consider splitting by specimen or by collection site.
Will it work for flowers from other regions?
Flower size can vary with the time and place of collection. And even for a new species outside the range of the training data, KNN will still pick one of the existing species.
How is it different from other methods?
They are all used for classification, but they learn different criteria.
| Method | Core approach | Difference from KNN |
|---|---|---|
| KNN | Predicts from the K nearest neighbors | Compares distances directly |
| Discriminant analysis (LDA/QDA) | Uses each group's distribution and covariance | Based on distributional assumptions |
| SVM | Learns a decision boundary that accounts for the margin | Focuses on the boundary rather than neighbors |
| Logistic regression | Models group probabilities | Coefficients are easy to interpret |
| Decision tree | Splits the data by conditions on variables | Rule structure is relatively clear |
Besides classification, there is also KNN regression, which averages the outcomes of nearby observations. What we cover here, though, is KNN classification, which chooses a species.
How do you report KNN results?
Report the data split, how distance and K were chosen, and the classification errors.
Actual results should include the sample size, the number in each species, the measurement variables used, the standardization method, the distance measure, the number of neighbors and weighting, the cross-validation scheme and the per-species errors on the independent test data. Where possible, also present the uncertainty in performance.
To classify wildflower species, K-nearest neighbors (KNN) classification was applied with the lengths and widths of the petals and sepals as input variables. Of the 180 flowers, 135 were used as training data and 45 as final test data, and K=5 was chosen by cross-validation within the training data. On the hypothetical final test data, accuracy was 86.7% (39/45), and recall for species A, B and C was 93.3%, 80.0% and 86.7%, respectively. These figures are hypothetical examples for explaining the method.
Stating that the transformation and the choice of K were done within cross-validation, and that the final test data were not reused for model selection, makes the results easier to reproduce and evaluate.
Where can KNN be used?
It applies to many classification and prediction problems where the similarity of observations can be put to use.
| Field | What is predicted | Example inputs |
|---|---|---|
| Biological classification | Flower and plant species | Petal and sepal measurements |
| Customer analytics | Customer type | Purchase frequency, spending |
| Medical research | Patient group | Measured clinical characteristics |
| Product quality control | Pass / defect | Sensor readings |
| Document classification | Topic category | Text feature vectors |
When choosing KNN, it's good to check not only whether it can classify, but also whether the data are representative enough, whether distances to new observations are meaningful for the real problem and whether prediction is fast enough.
KNN classifies a new observation by looking at what data surround it.
The criterion for finding nearest neighbors and the value of K determine the classification.
When classifying wildflower species, KNN finds flowers with similar petal and sepal measurements and judges the new flower's species from them. You don't need to assume a complicated formula first, but which variables you use and how you calculate distance change who the nearest neighbors are. If K is too small, the result becomes sensitive to noise in individual observations; if it is too large, small differences between species can be missed.
So when using KNN, compute preprocessing parameters from the training data only, choose K and the distance method by cross-validation, and then check classification errors on separate test data. Looking at per-species recall and the confusion matrix in addition to overall accuracy also shows which flowers are hard to tell apart. Checking whether similarity in the data really reflects similarity between species is also an essential part of interpreting the results.
KNN classifies the species of new data from the K closest observations. The distance measure, standardization and K directly affect the classification, so they should be chosen within the training data. You need to look at the per-species confusion matrix and recall along with accuracy to understand actual classification performance.