![]() |
VOOZH | about |
K-Nearest Neighbors (KNN) is a supervised learning algorithm that classifies new data points based on the closest existing labeled examples. To measure how “close” samples are, KNN relies on distance metrics that quantify similarity among feature values. Choosing an appropriate metric improves classification accuracy, robustness and generalization.
Some common reasons distance metrics are important in KNN are:
Euclidean distance measures the straight-line distance between two points in continuous numerical space. It works best when all features are continuous and similarly scaled.
Formula:
Where, p and q are data points.
Properties
Manhattan distance computes by summing absolute differences across dimensions. Useful when features represent directions, steps or grid-based movement.
Where p and q are data points.
Properties
Minkowski Distance is generalized version of both Euclidean and Manhattan distances. Controlled by a parameter p.
Where,
Chebyshev Distance measures the maximum absolute difference between two points across all features. It focuses on the largest deviation among dimensions.
Where p and q are data points.
Properties
Cosine Similarity measures the angle between two vectors instead of magnitude, capturing how similar their direction is.
Formula
Where,
Range: -1 to 1
Properties
| Distance Metric | When to Use | Not Ideal When | Use Case Scenario |
|---|---|---|---|
| Euclidean Distance | Data is continuous and evenly scaled | Features vary greatly in scale | Image recognition, sensor data |
| Manhattan Distance | High-dimensional or grid-based data | Features are highly correlated | City-block routing, clustering |
| Minkowski Distance | Need flexible distance tuning of parameter p | Unsure how to choose p | Generalized KNN experiments |
| Chebyshev Distance | Max difference matters across dimensions | Small variations are important | Chessboard moves, quality control |
| Cosine Similarity | Angle matters more than magnitude | Numerical size matters | Text similarity, embeddings, recommendations |