Manhattan Distance (KNN)
Step-by-step Manhattan distance calculations
1. Target Data (নতুন Sample)
2. K Value (Neighbors)
3. Sample Points
Step 1: Manhattan Distance formula ব্যবহার করবো
grid-like data বা অনেকগুলো dimension থাকলে Manhattan distance ব্যবহার করা হয়।
d = |x₁ − x₂| + |y₁ − y₂|
💡 সহজ ব্যাখ্যা:
(x₁, y₁)হলো আমাদের Target Data বা নতুন Point।(x₂, y₂)হলো আগে থেকে দেয়া Sample Data।- আমরা প্রতিটি Sample এর সাথে নতুন Point টির
xএবংyএর পার্থক্য (বিয়োগফল) বের করে তার পরম মান (absolute value) যোগ করে Distance বের করি।
এই formula দিয়ে নতুন point থেকে সব sample এর distance বের করবো।
Step 2: সব sample এর distance বের করি
Sample 1 → (7, 7) → Bad
|7 − 3| = 4) এবং Y এর পার্থক্য (|7 − 7| = 0) বের করে যোগ করা হলো।Distance
= |7 − 3| + |7 − 7|
= |4| + |0|
= 4 + 0
= 4
Sample 2 → (7, 4) → Bad
|7 − 3| = 4) এবং Y এর পার্থক্য (|4 − 7| = 3) বের করে যোগ করা হলো।Distance
= |7 − 3| + |4 − 7|
= |4| + |-3|
= 4 + 3
= 7
Step 3: Exam Answer Format (Summary Table)
পরীক্ষার খাতায় ঠিক এই ফরম্যাটে টেবিল করে উত্তর লিখলে ভালো নম্বর পাওয়া যাবে:
| Name | X Value | Y Value | Class | Distance |
|---|---|---|---|---|
| Sample 1 | 7 | 7 | Bad | |7−3| + |7−7| = 4 |
| Sample 2 | 7 | 4 | Bad | |7−3| + |4−7| = 7 |
Conclusion (for K = 3)
যেহেতু K = 3, তাই আমরা সবচেয়ে কাছের (কম distance) 3 টি neighbor বা sample নিবো:
- 1Sample 1 — Distance: 4 (Bad)
- 2Sample 2 — Distance: 7 (Bad)
✅ চূড়ান্ত সিদ্ধান্ত (Final Prediction):
উপরের এই 3 টি Nearest Neighbors এর মধ্যে যে Label টি সবচেয়ে বেশিবার আছে (Majority Vote), আমাদের নতুন Point টির Label ও সেটিই হবে!
💡 Rule of Thumb (কোনটা কখন ব্যবহার করবো?)
- Use Euclidean distance if your data is continuous and evenly scaled.
- Use Manhattan distance if your data has many dimensions or is grid-like.
- For most business or simple classification problems, Euclidean distance is more appropriate and widely used.
How Manhattan Distance works in analytics
Manhattan Distance is also called city-block distance because it measures how far two points are if you can only move horizontally and vertically, like walking through city streets. The formula is simple: |x1 - x2| + |y1 - y2| (and similarly for more dimensions). Instead of squaring differences like Euclidean distance, Manhattan distance adds absolute differences. This makes it less sensitive to extreme outliers in one feature and often more robust when feature values behave independently.
In business analytics, this metric is useful when each feature contributes additively to similarity. Example: customer segmentation using recency, frequency, and monetary score bands; supply-chain comparison using delivery delay and damage count; retail store profiling with staffing gap, stockout incidents, and complaint volume. In these cases, the absolute gap itself is often more meaningful than squared gap penalties.
For KNN classification, Manhattan distance can perform better than Euclidean in high-dimensional tabular data, where Euclidean tends to over-penalize larger coordinate differences. A good practice is to test both metrics after scaling features consistently, then compare validation performance and interpretability.