K-Nearest Neighbours (KNN)
K-Nearest Neighbours (KNN) is one of the simplest machine learning algorithms used for classification and prediction. It works by comparing new data with existing data and making decisions based on similarity.
📌 Simple idea:
“Tell me who your neighbours are, and I’ll tell you who you are.”
How KNN Works?
KNN follows a very simple process:
-
Choose a value of K (number of neighbours)
-
Measure the distance between the new data point and existing points
-
Find the K nearest neighbours
-
Take a majority vote (for classification)
-
Assign the final class
📌 Key point:
KNN does not learn beforehand. It makes decisions at the time of prediction.
Simple Example: Predicting Categories
👕 Example: T-Shirt Size Prediction
Suppose we want to predict T-shirt size based on height and weight.
| Height (cm) | Weight (kg) | Size |
|---|---|---|
| 160 | 55 | Small |
| 165 | 60 | Medium |
| 170 | 65 | Medium |
| 175 | 70 | Large |
| 180 | 75 | Large |
📌 New person:
Height = 168 cm, Weight = 63 kg
KNN:
-
Finds nearest people
-
Checks their sizes
-
Predicts the most common size → Medium
Python Implementation
Let’s implement KNN using Python and Scikit-Learn.
from sklearn.neighbors import KNeighboursClassifier
import numpy as np
# Dataset: [Height, Weight]
X = np.array([
[160, 55],
[165, 60],
[170, 65],
[175, 70],
[180, 75]
])
# Labels
y = ['Small', 'Medium', 'Medium', 'Large', 'Large']
# Create model
model = KNeighborsClassifier(n_neighbours=3)
model.fit(X, y)
# Predict new value
prediction = model.predict([[168, 63]])
print("Predicted T-shirt size:", prediction)
🔍 Explanation:
-
K = 3 nearest neighbours
-
Model checks closest data points
-
Returns the most common label
📌 Beginner Tip:
Choosing the right value of K is important. Small K can be noisy, large K can be slow.
Where KNN is Used
-
Recommendation systems
-
Image recognition
-
Pattern classification
-
Customer segmentation
Mini Summary
K-Nearest Neighbours (KNN) is a simple and powerful machine learning algorithm that makes decisions based on similarity. By comparing a new data point with its nearest neighbours, KNN predicts the correct category. Its step-by-step logic and easy implementation make it ideal for beginners learning machine learning.
Comments
Post a Comment