What numerical safeguard is needed for inverse-distance weights when a training point exactly matches the query?

Quick Overview

Asks how to keep inverse-distance weights in k-nearest neighbors well defined when a training point sits exactly at the query. Tests numerical reasoning about division by zero, floating-point distances, ties between duplicate points, and how weighted predictions behave near training data.

What numerical safeguard is needed for inverse-distance weights when a training point exactly matches the query?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

In distance-weighted k-nearest neighbors, each neighbor's label or target is weighted by `1/d`, where `d` is its distance to the query, and the prediction is the weighted vote or weighted average. What numerical safeguard is needed for inverse-distance weights when a training point exactly matches the query? ```hint Plug in a zero Work out what the weight, the sum of weights and the final weighted average become when one distance is exactly zero, then decide what prediction you would want in that case. ``` ```hint Exact may not be exact Think about how the distance is computed in floating point, and whether an identical point always comes out at exactly zero. ``` ### What a Strong Answer Covers - What goes wrong numerically when a distance is zero, traced through to the final prediction - Ways to keep the weights and the prediction well defined, and what each one predicts for the matching point - How to choose any constant the fix introduces - Edge cases such as conflicting duplicates at distance zero and near-zero distances under higher powers of the distance ### Follow-up Questions - How does scikit-learn's KNeighborsClassifier with weights="distance" handle a zero distance? - Two training points sit exactly at the query with different labels. What should the classifier return? - Distances computed with the dot-product expansion of the squared distance can come out tiny but nonzero, or slightly negative, for identical points. How does that affect the safeguard? - How would you choose epsilon when the features are not scaled?

Overview: Asks how to keep inverse-distance weights in k-nearest neighbors well defined when a training point sits exactly at the query. Tests numerical reasoning about division by zero, floating-point distances, ties between duplicate points, and how weighted predictions behave near training data.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

In distance-weighted k-nearest neighbors, each neighbor's label or target is weighted by 1/d, where d is its distance to the query, and the prediction is the weighted vote or weighted average. What numerical safeguard is needed for inverse-distance weights when a training point exactly matches the query?

What a Strong Answer Covers Guidance

  • What goes wrong numerically when a distance is zero, traced through to the final prediction
  • Ways to keep the weights and the prediction well defined, and what each one predicts for the matching point
  • How to choose any constant the fix introduces
  • Edge cases such as conflicting duplicates at distance zero and near-zero distances under higher powers of the distance

Follow-up Questions Guidance

  • How does scikit-learn's KNeighborsClassifier with weights="distance" handle a zero distance?
  • Two training points sit exactly at the query with different labels. What should the classifier return?
  • Distances computed with the dot-product expansion of the squared distance can come out tiny but nonzero, or slightly negative, for identical points. How does that affect the safeguard?
  • How would you choose epsilon when the features are not scaled?