Skip to content
Home ยป K-Nearest Neighbour (KNN)

K-Nearest Neighbour (KNN)


1. Definition

K-Nearest Neighbour (KNN) is a supervised machine learning algorithm that classifies a new data point based on the classes of its nearest neighbouring data points.

In very simple words:

KNN looks at the nearest K data points and decides the class of the new data point based on majority voting.

Example

Suppose we have students classified as:

  • ๐Ÿ”ต = Good student
  • ๐Ÿ”ด = Weak student

A new student comes in. KNN looks at the students closest to this new student.

If most nearby students are Good, the new student is classified as Good.

        ๐Ÿ”ต Good
   ๐Ÿ”ต        ๐Ÿ”ต

          ?  โ† New Student

   ๐Ÿ”ด        ๐Ÿ”ต
        ๐Ÿ”ด

Most nearby students = ๐Ÿ”ต Good
Therefore โ†’ New Student = Good

2. Meaning of K in KNN

K means the number of nearest neighbours that we consider.

For example:

  • K = 1 โ†’ consider 1 nearest neighbour
  • K = 3 โ†’ consider 3 nearest neighbours
  • K = 5 โ†’ consider 5 nearest neighbours

Suppose:

K = 5

KNN finds the 5 closest data points and checks their classes.

If:

3 โ†’ Class A
2 โ†’ Class B

Then:\[ \boxed{\text{New Data} = \text{Class A}} \]

because Class A has the majority.


3. How KNN Works

The working of KNN can be explained in the following steps:

          Training Data
                โ†“
        Select value of K
                โ†“
       Give new data point
                โ†“
 Calculate distance from all points
                โ†“
   Find K nearest data points
                โ†“
      Check their classes
                โ†“
       Majority Voting
                โ†“
        Final Prediction

4. Step 1: Select K

First, choose the value of K.

For example:\[ K=3 \]

This means the algorithm will consider the 3 nearest neighbours.


5. Step 2: Calculate Distance

KNN usually calculates the distance between the new point and existing data points.

The most common method is Euclidean Distance.

For two points:\[ A(x_1,y_1) \]

and\[ B(x_2,y_2) \]

the Euclidean distance is:\[ d=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2} \]

Example

Suppose:

New point:\[ A(2,3) \]

Existing point:\[ B(5,7) \]

Then:\[ d=\sqrt{(5-2)^2+(7-3)^2} \]\[ =\sqrt{3^2+4^2} \]\[ =\sqrt{25}=5 \]

So the distance between them is 5.


6. Step 3: Find the K Nearest Neighbours

After calculating distances from the new point to all training points, KNN selects the K points with the smallest distances.

For example:

PointDistanceClass
A1.2๐Ÿ”ต
B1.8๐Ÿ”ต
C2.1๐Ÿ”ด
D3.5๐Ÿ”ต
E4.2๐Ÿ”ด

If:\[ K=3 \]

we select:

A โ†’ ๐Ÿ”ต
B โ†’ ๐Ÿ”ต
C โ†’ ๐Ÿ”ด

7. Step 4: Majority Voting

Now count the classes.

Class ๐Ÿ”ต = 2
Class ๐Ÿ”ด = 1

Therefore:\[ \boxed{\text{Prediction = ๐Ÿ”ต}} \]

This is called majority voting.


8. Complete Example

Suppose we want to classify a fruit as:

  • ๐ŸŽ Apple
  • ๐ŸŠ Orange

We use features such as:

  • Weight
  • Size

A new fruit is given.

KNN finds its 5 nearest fruits:

NeighbourFruit
1Apple
2Orange
3Apple
4Apple
5Orange

Here:\[ K=5 \]

Votes:

Apple  = 3
Orange = 2

Therefore:\[ \boxed{\text{New Fruit = Apple}} \]


9. Choosing the Value of K

Choosing a suitable value of K is important.

Small K

For example:\[ K=1 \]

The algorithm can be highly affected by noise or an unusual data point.

Large K

A very large K may include points from other classes and make the classification less accurate.

Therefore, a suitable K value is normally selected through experimentation or validation.

Important point

Usually, an odd value of K such as:\[ K=3,\;5,\;7 \]

is preferred for binary classification because it reduces the chance of a tie.


10. KNN is a Lazy Learning Algorithm

This is an important MCA exam point.

KNN is called a lazy learning algorithm because it does not build a detailed model during the training phase.

It mainly stores the training data.

When a new data point arrives, it performs the distance calculations and makes the prediction.

Training:
Data โ†’ Store Data

Prediction:
New Data
   โ†“
Calculate distances
   โ†“
Find neighbours
   โ†“
Vote
   โ†“
Prediction

Therefore:

KNN does most of its work at prediction time rather than during training.


11. Advantages of KNN

  1. Very simple and easy to understand.
  2. Easy to implement.
  3. No complicated training process.
  4. Can be used for classification and regression.
  5. Can work well when similar data points belong to similar classes.
  6. Useful for small datasets.

12. Disadvantages of KNN

  1. Prediction can be slow for large datasets.
  2. It needs to calculate distances from the new point to many training points.
  3. Choosing the correct K is important.
  4. Sensitive to irrelevant features.
  5. Sensitive to the scale of features.
  6. Large datasets require more memory because training data is stored.

13. Feature Scaling in KNN

Feature scaling is important because KNN uses distance.

Suppose we have:

Age:       20 โ€“ 60
Income:    โ‚น20,000 โ€“ โ‚น10,00,000

Income has much larger numerical values than age.

Therefore, income may dominate the distance calculation.

Methods such as:

  • Normalization
  • Standardization

can be used to bring features to comparable scales.


14. KNN Classification vs Clustering

This distinction is very important for your exam.

KNNClustering
Supervised learningUnsupervised learning
Uses labelled dataUses unlabelled data
Predicts class of new dataFinds natural groups
Example: Pass/FailExample: Customer groups
Uses nearest neighboursUses similarity/grouping techniques
KNN is an algorithmK-Means is a common clustering algorithm

Example

KNN:

Existing labelled customers
       โ†“
New customer
       โ†“
Find nearest customers
       โ†“
Predict category

Clustering:

Unlabelled customers
       โ†“
Find similarities
       โ†“
Create groups
       โ†“
Customer Segment 1, 2, 3...

15. Applications of KNN

KNN can be used in:

  • Pattern recognition
  • Image classification
  • Handwriting recognition
  • Recommendation systems
  • Medical diagnosis
  • Customer classification
  • Fraud detection
  • Student performance prediction
  • Text classification

Digital Marketing Example

Suppose we have customers labelled:

Customer A โ†’ Will Buy
Customer B โ†’ Will Buy
Customer C โ†’ Will Not Buy
Customer D โ†’ Will Buy

For a new customer, KNN finds similar customers.

If most similar customers purchased the product:\[ \boxed{\text{New Customer โ†’ Likely to Buy}} \]


16. KNN vs K-Means

Since your syllabus places KNN under Clustering, this difference is especially useful.

KNNK-Means
SupervisedUnsupervised
Uses labelled dataUses unlabelled data
Classification/regressionClustering
K = number of neighboursK = number of clusters
Predicts new dataCreates groups
Uses distance to neighboursUses distance from cluster centroids

Easy memory

KNN โ†’ K Neighbours โ†’ Prediction

K-Means โ†’ K Groups โ†’ Clustering


17. Exam-Ready Answer

K-Nearest Neighbour (KNN) is a supervised machine learning algorithm used mainly for classification and regression. It predicts the class of a new data point by finding the K nearest data points in the training dataset. The distance between data points is commonly calculated using Euclidean distance. For classification, KNN uses majority voting among the K nearest neighbours. A small value of K may make the model sensitive to noise, while a very large value may reduce accuracy. KNN is simple to understand and implement but can be slow for large datasets.

Important Formula

\[ \boxed{d=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}} \]

Remember:

KNN = Find K nearest points โ†’ Check their classes โ†’ Majority vote โ†’ Predict