K-Nearest Neighbors (KNN)
K-Nearest Neighbors (KNN)
K-Nearest Neighbors (KNN) is a simple, intuitive supervised learning algorithm used for:
- Classification (e.g., spam vs not spam)
- Regression (predicting continuous values)
It is a lazy learning algorithm, meaning:
It does not build an explicit model during training—it stores the data and makes decisions at prediction time.
๐ง Core Idea
“A data point is likely to have the same label as its nearest neighbors.”
When a new data point comes in:
- Find the K closest points in the training data
- Look at their labels
- Assign the majority label (for classification)
⚙️ How KNN Works (Step-by-Step)
Step 1: Choose K
- Number of neighbors to consider
- Example: K = 3, 5, 7
Step 2: Compute Distance
The most common distance metric is Euclidean distance:
Other distance measures:
- Manhattan distance
- Minkowski distance
- Cosine similarity (for text data)
Step 3: Find Nearest Neighbors
- Compute distance from new point to all training points
- Sort and select K smallest distances
Step 4: Make Prediction
For Classification:
- Use majority voting
For Regression:
- Take average of neighbors
๐ Example (Simple Classification)
Dataset
| Student | Study Hours | Result |
|---|---|---|
| A | 2 | Fail |
| B | 4 | Fail |
| C | 6 | Pass |
| D | 8 | Pass |
Predict for New Student:
- Study Hours = 5
- Choose K = 3
Step 1: Compute Distance
| Point | Distance from 5 |
|---|---|
| 2 | 3 |
| 4 | 1 |
| 6 | 1 |
| 8 | 3 |
Step 2: Pick 3 Nearest
→ (4, 6, 2)
Labels:
- Fail, Pass, Fail
Step 3: Majority Vote
๐ Fail = 2
๐ Pass = 1
✅ Prediction: FAIL
๐ฏ Choosing the Value of K
| K Value | Behavior |
|---|---|
| Small K (e.g., 1) | Sensitive to noise (overfitting) |
| Large K | Smooth decision boundary (underfitting) |
๐ Common practice:
- Use odd K for binary classification
- Choose K using cross-validation
๐ Important Concept: Feature Scaling
KNN depends on distance → scale matters!
Example:
- Salary (₹) vs Age (years)
๐ Use:
- Min-Max Scaling
- Standardization
๐งฎ KNN Regression
For ordinary KNN regression, take the average:
๐ฐ Predicted price = ₹55 lakh
⚖️ Weighted KNN
In ordinary KNN, every neighbor gets equal importance.
In Weighted KNN, closer neighbors get more importance.
๐ A common weight is:
where di is the distance from the new observation.
Example
Suppose the three neighbors are:
| Neighbor | Distance | Price | Weight |
|---|---|---|---|
| A | 0.5 | ₹50 lakh | 2.0 |
| B | 1.0 | ₹45 lakh | 1.0 |
| C | 2.0 | ₹30 lakh | 0.5 |
Notice:
๐ต Closer → higher weight
๐ Farther → lower weight
The weighted prediction is:
∑wi2+1+0.5 =160/3.5=45.71๐ฐ Weighted KNN prediction = ₹45.71 lakh
๐ KNN vs Weighted KNN
| KNN | Weighted KNN | |
|---|---|---|
| ๐ฅ Neighbors | K closest | K closest |
| ⚖️ Importance | Equal | Based on distance |
| ๐ Regression | Simple average | Weighted average |
| ๐ท️ Classification | Majority vote | Weighted vote |
| ๐ Closest point | Same importance | More important |
⭐ Easy way to remember
KNN:
๐ฅ All neighbors have an equal vote.
Weighted KNN:
๐ Closer neighbors have a stronger vote.
Ordinary KNN vs Weighted KNN
| Feature | KNN | Weighted KNN |
|---|---|---|
| Neighbor importance | Equal | Depends on distance |
| Close neighbor | Same importance | Higher importance |
| Distant neighbor | Same importance | Lower importance |
| Regression | Average of neighbors | Weighted average |
| Classification | Majority vote | Weighted vote |
⚡ Advantages
- ✅ Simple to understand and implement
- ✅ No training phase
- ✅ Works well with small datasets
- ✅ Non-parametric (no assumptions about data distribution)
⚠️ Disadvantages
- ❌ Slow for large datasets (computationally expensive)
- ❌ Sensitive to irrelevant features
- ❌ Requires proper scaling
- ❌ Memory intensive (stores all data)
๐ Real-World Applications
- ๐ง Spam detection
- ๐ฅ Medical diagnosis
- ๐ฏ Recommendation systems
- ๐ผ️ Image classification
- ๐ง Pattern recognition
๐งฉ KNN vs Naรฏve Bayes (Quick Contrast)
| Aspect | KNN | Naรฏve Bayes |
|---|---|---|
| Type | Instance-based | Probabilistic |
| Training | None | Required |
| Speed | Slow at prediction | Fast |
| Assumption | None | Feature independence |
๐ Final Summary
- KNN is a distance-based algorithm
- It predicts using similarity
- Works by majority voting of neighbors
Comments
Post a Comment