When would you use Mahalanobis distance instead of Euclidean distance in KNN?

Quick Overview

Asks when Mahalanobis distance is a better choice than Euclidean distance for k-nearest neighbors. Tests understanding of covariance, whitening, correlated features, covariance estimation in high dimensions and the link to metric learning.

When would you use Mahalanobis distance instead of Euclidean distance in KNN?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

When would you use Mahalanobis distance instead of Euclidean distance in KNN? ```hint Picture the point cloud Imagine the training data as a tilted ellipse because two features are correlated. Compare two points that Euclidean distance calls equally far from the center, and ask whether they are equally typical. ``` ```hint What the metric needs Mahalanobis distance depends on a matrix estimated from data. Think about what that matrix must satisfy and how many samples it takes to estimate it well. ``` ### What a Strong Answer Covers - The definition of Mahalanobis distance and its relation to Euclidean distance after whitening or standardization. - The data conditions where it improves the neighbor search, with an example. - Practical requirements for the covariance estimate: invertibility, sample size and fitting on training data only. - Limitations of a single global covariance and of a metric that ignores the labels. - How metric learning generalizes the idea, and the computational cost of using it. ### Follow-up Questions - With 500 features and 300 training samples, how would you compute Mahalanobis distances? - Whitening stretches directions with tiny variance. When does that hurt KNN, and what would you do about it? - How would you make Mahalanobis KNN fast on a few million points? - How do NCA or LMNN choose their matrix, and what do they need that plain Mahalanobis distance does not?

Overview: Asks when Mahalanobis distance is a better choice than Euclidean distance for k-nearest neighbors. Tests understanding of covariance, whitening, correlated features, covariance estimation in high dimensions and the link to metric learning.

PracHub logo
General
Nov 24, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

When would you use Mahalanobis distance instead of Euclidean distance in KNN?

What a Strong Answer Covers Guidance

  • The definition of Mahalanobis distance and its relation to Euclidean distance after whitening or standardization.
  • The data conditions where it improves the neighbor search, with an example.
  • Practical requirements for the covariance estimate: invertibility, sample size and fitting on training data only.
  • Limitations of a single global covariance and of a metric that ignores the labels.
  • How metric learning generalizes the idea, and the computational cost of using it.

Follow-up Questions Guidance

  • With 500 features and 300 training samples, how would you compute Mahalanobis distances?
  • Whitening stretches directions with tiny variance. When does that hurt KNN, and what would you do about it?
  • How would you make Mahalanobis KNN fast on a few million points?
  • How do NCA or LMNN choose their matrix, and what do they need that plain Mahalanobis distance does not?