Explain a KNN Prediction: Neighbours, Distances and Vote Counts

Quick Overview

Implement a function that explains a single k-nearest-neighbour classification by returning the neighbours, their labels and distances, per-class vote counts and the final prediction. It tests vectorised distance computation in NumPy, deterministic tie-breaking and careful input validation.

Explain a KNN Prediction: Neighbours, Distances and Vote Counts

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `explain_prediction(X_train, y_train, query, k)` for one query point using Euclidean distance. Return a dict with keys `indices`, `labels`, `distances` (the k nearest neighbours, ascending distance, distance ties broken by lower training index), `classes` (sorted unique training labels), `vote_counts` (int array aligned with `classes`, zero for absent classes) and `prediction` (majority label, vote ties go to the smallest label). Example: ```python X_train = [[0,0], [3,4], [1,0], [0,2], [5,5]] y_train = [1, 0, 1, 0, 0] explain_prediction(X_train, y_train, query=[0,0], k=3) # indices=[0,2,3], labels=[1,1,0], distances=[0.,1.,2.] # classes=[0,1], vote_counts=[1,2], prediction=1 ``` ```hint Tie rules Look for NumPy operations whose ordering guarantees already match both tie rules, so you do not need hand-written tie-breaking code. ``` ### Constraints & Assumptions - `X_train` is 2-D with shape (n, d), `y_train` has length n, `query` has length d. - Labels may be integers or strings. - A `k` outside `[1, n]`, or a non-integer `k`, raises `ValueError`. ### What a Strong Answer Covers - Vectorised Euclidean distances and neighbours returned in ascending distance, with ties going to the lower training index. - Vote counts over every training class in sorted order, including zeros for classes that no neighbour has. - A prediction that follows the smallest-label tie rule and works for string labels. - Input validation that rejects out-of-range and non-integer `k` with `ValueError`. - The time and space cost of the computation. ### Follow-up Questions - How would the explanation change if neighbours voted with inverse-distance weights? - How would you explain predictions for a batch of queries without a Python loop over them? - Why can the nearest neighbours be a misleading explanation when features are on different scales, and what would you do about it?

Overview: Implement a function that explains a single k-nearest-neighbour classification by returning the neighbours, their labels and distances, per-class vote counts and the final prediction. It tests vectorised distance computation in NumPy, deterministic tie-breaking and careful input validation.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement explain_prediction(X_train, y_train, query, k) for one query point using Euclidean distance. Return a dict with keys indices, labels, distances (the k nearest neighbours, ascending distance, distance ties broken by lower training index), classes (sorted unique training labels), vote_counts (int array aligned with classes, zero for absent classes) and prediction (majority label, vote ties go to the smallest label).

Example:

X_train = [[0,0], [3,4], [1,0], [0,2], [5,5]]
y_train = [1, 0, 1, 0, 0]
explain_prediction(X_train, y_train, query=[0,0], k=3)
# indices=[0,2,3], labels=[1,1,0], distances=[0.,1.,2.]
# classes=[0,1], vote_counts=[1,2], prediction=1

Constraints & Assumptions

  • X_train is 2-D with shape (n, d), y_train has length n, query has length d.
  • Labels may be integers or strings.
  • A k outside [1, n] , or a non-integer k , raises ValueError .

What a Strong Answer Covers Guidance

  • Vectorised Euclidean distances and neighbours returned in ascending distance, with ties going to the lower training index.
  • Vote counts over every training class in sorted order, including zeros for classes that no neighbour has.
  • A prediction that follows the smallest-label tie rule and works for string labels.
  • Input validation that rejects out-of-range and non-integer k with ValueError .
  • The time and space cost of the computation.

Follow-up Questions Guidance

  • How would the explanation change if neighbours voted with inverse-distance weights?
  • How would you explain predictions for a batch of queries without a Python loop over them?
  • Why can the nearest neighbours be a misleading explanation when features are on different scales, and what would you do about it?