KNN Regressor From Scratch

Quick Overview

Implement a k-nearest-neighbours regressor from scratch in NumPy that predicts the mean target of the k closest training points by Euclidean distance. It tests vectorized distance computation, a deterministic tie rule for equidistant neighbours, and the cost of brute-force prediction.

KNN Regressor From Scratch

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `KNNRegressor(k=3)` with `fit(X, y)`, which stores the training data and returns `self`, and `predict(X)`. For each query row, find the `k` nearest training points by Euclidean distance and predict the arithmetic mean of their targets. Example: ```python X = [[0.0], [1.0], [2.0], [10.0]] y = [0.0, 1.0, 2.0, 100.0] KNNRegressor(k=2).fit(X, y).predict([[0.4], [9.0]]) # array([0.5, 51.0]) ``` ```hint Watch the tie rule The tie rule depends on how you pick the k smallest distances in each row. Check whether the NumPy routine you use keeps equal values in their original index order. ``` ### Constraints & Assumptions - `X` is 2-D with shape `(n, d)` and `y` has length `n`. Targets may be integers. - `1 <= k <= n`. - Distance ties break toward the lower training index. - `predict` returns a 1-D float array of shape `(n_queries,)`. ### What a Strong Answer Covers - Lazy fitting: `fit` stores `X` and `y` as arrays and returns `self`. - A vectorized query-by-train Euclidean distance matrix. - Neighbour selection that follows the tie rule toward the lower training index. - The mean of the k targets returned as floats with shape `(n_queries,)`. - The time and memory cost of brute-force prediction. ### Follow-up Questions - How would you add distance-weighted predictions, and what happens when a query coincides with a training point? - How would you choose `k`, and how does it trade bias against variance? - What changes when the training set has millions of rows? - Why should features be scaled before fitting, and how?

Overview: Implement a k-nearest-neighbours regressor from scratch in NumPy that predicts the mean target of the k closest training points by Euclidean distance. It tests vectorized distance computation, a deterministic tie rule for equidistant neighbours, and the cost of brute-force prediction.

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

Implement KNNRegressor(k=3) with fit(X, y), which stores the training data and returns self, and predict(X). For each query row, find the k nearest training points by Euclidean distance and predict the arithmetic mean of their targets.

Example:

X = [[0.0], [1.0], [2.0], [10.0]]
y = [0.0, 1.0, 2.0, 100.0]
KNNRegressor(k=2).fit(X, y).predict([[0.4], [9.0]])
# array([0.5, 51.0])

Constraints & Assumptions

  • X is 2-D with shape (n, d) and y has length n . Targets may be integers.
  • 1 <= k <= n .
  • Distance ties break toward the lower training index.
  • predict returns a 1-D float array of shape (n_queries,) .

What a Strong Answer Covers Guidance

  • Lazy fitting: fit stores X and y as arrays and returns self .
  • A vectorized query-by-train Euclidean distance matrix.
  • Neighbour selection that follows the tie rule toward the lower training index.
  • The mean of the k targets returned as floats with shape (n_queries,) .
  • The time and memory cost of brute-force prediction.

Follow-up Questions Guidance

  • How would you add distance-weighted predictions, and what happens when a query coincides with a training point?
  • How would you choose k , and how does it trade bias against variance?
  • What changes when the training set has millions of rows?
  • Why should features be scaled before fitting, and how?