ML0055 KNN Regression

Please explain how KNN Regression works.

Answer

K-Nearest Neighbors (KNN) Regression is a simple, instance-based algorithm that predicts a continuous output by finding the K nearest training points and averaging their target values. Its simplicity makes it easy to understand and implement, but performance is sensitive to K and the distance metric, and prediction is computationally expensive on large datasets since every query compares against all training samples. Like its classification sibling, KNN regression is a “lazy learner”: it builds no explicit model and simply memorizes the training data.

(1) Instance-Based: No explicit model is learned; predictions come from stored training data and similarity.
(2) Distance-Based: Find the K nearest neighbors of the query point (commonly Euclidean distance).
(3) Averaging Neighbors: The prediction is the mean of those neighbors’ target values.
(4) Sensitive To K And Metric: Small K tracks noise; large K oversmooths.
(5) No Training Phase: All computation happens during prediction.

KNN regression with K equals five predicting a jagged step like curve through noisy sinusoidal training points

Figure 1: KNN regression (K=5) on a noisy sine wave: each prediction averages the 5 nearest targets, producing a locally-flat, step-like curve that follows the trend but clips the peaks and troughs, the signature of local averaging.

Mathematical Formulation:
\hat{y} = \frac{1}{K} \sum_{i=1}^{K} y_i
\hat{y}(x_q) = \frac{\sum_{i=1}^{K} w_i \cdot y_i}{\sum_{i=1}^{K} w_i}
w_i = \frac{1}{d(x_q, x_i) + \epsilon}

Where:

  • \hat{y} is the prediction for the query point; y_i the target of the i-th nearest neighbor; K the number of neighbors (first formula: uniform average).
  • In the weighted variant, w_i is inversely proportional to the distance d(x_q, x_i) between query and neighbor: closer points influence the prediction more.
  • \epsilon is a small constant avoiding division by zero when a neighbor sits exactly on the query.

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *