How do you choose the distance metric for KNN?
mediumAnswer
- Euclidean is the default for continuous features that have been standardized.
- Manhattan (L1) is more robust to outliers and often used with sparse or high-dimensional data.
- Cosine distance for text / normalized feature vectors where direction matters more than magnitude.
- Mahalanobis when features have different scales and correlations (uses the inverse covariance).
- Hamming for binary/categorical features.
- Domain-specific metrics (e.g., edit distance for strings) when relevant.
Check yourself — multiple choice
- Euclidean is always best
- Match the metric to the feature type: Euclidean/Manhattan for continuous, cosine for text, Hamming for binary
- Distance metric never matters
- Only cosine works for KNN
The right metric depends on feature type: Euclidean/Manhattan/cosine/Hamming/etc.
#knn#features
Practise Supervised Learning
215 interview questions in this topic.
Related questions
- Which models need feature scaling and which don't?
- What is target leakage and how do you prevent it?
- What is the curse of dimensionality?
- What is VIF and when do you worry about multicollinearity?
- How do you handle strong multicollinearity in a linear model?
- What's the main risk of polynomial regression?