What distance metric is appropriate for binary or categorical feature vectors in KNN?

Quick Overview

Choose a distance metric for k-nearest neighbors when the features are binary or categorical. Tests whether Euclidean distance on label codes is meaningful, how shared zeros should count toward similarity, how one-hot encoding changes distances, and how to handle mixed feature types.

What distance metric is appropriate for binary or categorical feature vectors in KNN?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

What distance metric is appropriate for binary or categorical feature vectors in KNN? ```hint Think about what a match means Consider what Euclidean distance assumes about the numbers it compares, and ask whether two vectors that both have a 0 in some position should count as more similar because of it. ``` ### What a Strong Answer Covers - Whether Euclidean distance on integer label codes is meaningful - The metric to use for symmetric binary or raw categorical features, and why - How to treat positions where both vectors are 0, and the metric that fits that choice - How one-hot encoding plus Euclidean distance compares with the chosen metric, and what it costs - What to use when numeric and categorical features are mixed ### Follow-up Questions - With 10,000 sparse item columns, which users does Hamming distance rank as a given user's nearest neighbors, and why is that a problem? - How would you handle an ordinal feature such as shirt size (S, M, L, XL)? - One categorical feature has 5,000 levels. How does that affect KNN, and what would you do about it? - How would you weight features in Gower's distance when some matter more than others?

Overview: Choose a distance metric for k-nearest neighbors when the features are binary or categorical. Tests whether Euclidean distance on label codes is meaningful, how shared zeros should count toward similarity, how one-hot encoding changes distances, and how to handle mixed feature types.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

What distance metric is appropriate for binary or categorical feature vectors in KNN?

What a Strong Answer Covers Guidance

  • Whether Euclidean distance on integer label codes is meaningful
  • The metric to use for symmetric binary or raw categorical features, and why
  • How to treat positions where both vectors are 0, and the metric that fits that choice
  • How one-hot encoding plus Euclidean distance compares with the chosen metric, and what it costs
  • What to use when numeric and categorical features are mixed

Follow-up Questions Guidance

  • With 10,000 sparse item columns, which users does Hamming distance rank as a given user's nearest neighbors, and why is that a problem?
  • How would you handle an ordinal feature such as shirt size (S, M, L, XL)?
  • One categorical feature has 5,000 levels. How does that affect KNN, and what would you do about it?
  • How would you weight features in Gower's distance when some matter more than others?