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:
| Point | Distance | Class |
|---|---|---|
| A | 1.2 | ๐ต |
| B | 1.8 | ๐ต |
| C | 2.1 | ๐ด |
| D | 3.5 | ๐ต |
| E | 4.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:
| Neighbour | Fruit |
|---|---|
| 1 | Apple |
| 2 | Orange |
| 3 | Apple |
| 4 | Apple |
| 5 | Orange |
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
- Very simple and easy to understand.
- Easy to implement.
- No complicated training process.
- Can be used for classification and regression.
- Can work well when similar data points belong to similar classes.
- Useful for small datasets.
12. Disadvantages of KNN
- Prediction can be slow for large datasets.
- It needs to calculate distances from the new point to many training points.
- Choosing the correct K is important.
- Sensitive to irrelevant features.
- Sensitive to the scale of features.
- 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.
| KNN | Clustering |
|---|---|
| Supervised learning | Unsupervised learning |
| Uses labelled data | Uses unlabelled data |
| Predicts class of new data | Finds natural groups |
| Example: Pass/Fail | Example: Customer groups |
| Uses nearest neighbours | Uses similarity/grouping techniques |
| KNN is an algorithm | K-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.
| KNN | K-Means |
|---|---|
| Supervised | Unsupervised |
| Uses labelled data | Uses unlabelled data |
| Classification/regression | Clustering |
| K = number of neighbours | K = number of clusters |
| Predicts new data | Creates groups |
| Uses distance to neighbours | Uses 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
