K-Nearest Neighbor (KNN) Algorithm: Last Updated: 14 May, 2025
K-Nearest Neighbor (KNN) Algorithm: Last Updated: 14 May, 2025
1/3
For example, consider the following table of data points containing two
features: ▲
Open In App
KNN Algorithm working visualization
The red diamonds represent Category 1 and the blue squares represent
Category 2.
The new data point checks its closest neighbors (circled points).
Since the majority of its closest neighbors are blue squares (Category
2) KNN predicts the new data point belongs to Category 2.
Example: Imagine you're deciding which fruit it is based on its shape and
size. You compare it to fruits you already know.
The value of k in KNN decides how many neighbors the algorithm looks
at when making a prediction.
Choosing the right k is important for good results.
If the data has lots of noise or outliers, using a larger k can make the
predictions more stable.
But if k is too large the model may become too simple and miss
important patterns and this is called underfitting.
So k should be picked carefully based on the data.
Open In App
1. Euclidean Distance
distance(x, X
d
2
i)
= ∑ (xj − Xi ) ]
j =1 j
2. Manhattan Distance
This is the total distance you would travel if you could only move along
horizontal and vertical lines like a grid or city streets. It’s also called
"taxicab distance" because a taxi can only drive along the grid-like streets
of a city.
n
d (x, y ) = ∑
∣xi − yi ∣
i=1
3. Minkowski Distance
1
n p
p
d (x, y ) = (∑
(xi − yi ) )
i=1
From the formula above, when p=2, it becomes the same as the Euclidean
distance formula and when p=1, it turns into the Manhattan distance
formula. Minkowski distance is essentially a flexible formula that can
represent either Euclidean or Manhattan distance depending on the value
of p.
The k data points with the smallest distances to the target point are
nearest neighbors.
When you want to classify a data point into a category like spam or not
spam, the KNN algorithm looks at the K closest points in the dataset.
These closest points are called neighbors. The algorithm then looks at
Open In App
which category the neighbors belong to and picks the one that appears
the most. This is called majority voting.
In regression, the algorithm still looks for the K closest points. But
instead of voting for a class in classification, it takes the average of the
values of those K neighbors. This average is the predicted value for the
new point for the algorithm.
1. Importing Libraries
1 import numpy as np
2 from collections import Counter
1 training_data = [[1, 2], [2, 3], [3, 4], [6, 7], [7,
8]]
2 training_labels = ['A', 'A', 'A', 'B', 'B']
3 test_point = [4, 5]
Open In App
4 k = 3
5. Prediction
1 prediction = knn_predict(training_data,
training_labels, test_point, k)
2 print(prediction)
Output:
The algorithm calculates the distances of the test point [4, 5] to all
training points selects the 3 closest points as k = 3 and determines their
labels. Since the majority of the closest points are labelled 'A' the test
point is classified as 'A'.
In machine learning we can also use Scikit Learn python library which
has in built functions to perform KNN machine learning model and for
that you refer to Implementation of KNN classifier using Sklearn.
Applications of KNN
Recommendation Systems: Suggests items like movies or products by
finding users with similar preferences.
Spam Detection: Identifies spam emails by comparing new emails to
known spam and non-spam examples.
Customer Segmentation: Groups customers by comparing their
shopping behavior to others.
Speech Recognition: Matches spoken words to known patterns to
convert them into text.
Advantages of KNN
Simple to use: Easy to understand and implement.
Open In App
No training step: No need to train as it just stores the data and uses it
during prediction.
Few parameters: Only needs to set the number of neighbors (k) and a
distance method.
Versatile: Works for both classification and regression problems.
Disadvantages of KNN
Slow with large data: Needs to compare every point during prediction.
Struggles with many features: Accuracy drops when data has too
many features.
Can Overfit: It can overfit especially when the data is high-dimensional
or not clean.
kartik Follow
57
Open In App
Similar Reads
r-Nearest neighbors
r-Nearest neighbors are a modified version of the k-nearest neighbors. The
issue with k-nearest neighbors is the choice of k. With a smaller k, the…
ML | K-means++ Algorithm
Clustering is one of the most common tasks in machine learning where we
group similar data points together. K-Means Clustering is one of the…
10 min read
Advertise with us
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal GfG Weekly Contest
Privacy Policy Offline Classroom Program
Careers DSA in JAVA/C++
In Media Master System Design
Contact Us Master CP
GfG Corporate Solution GeeksforGeeks Videos
Placement Training Program
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial
Open In App
Python Tutorial Computer Science
Python Programming Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design
Python Interview Question Engineering Maths
Open In App