RegressionSample

k-nearest neighbours regression

Predicts by averaging the targets of the k closest training points.

Synthetic data, generated and trained in your browser.

Try this

  1. 01Set k = 1: the curve becomes a staircase that passes through every point.
  2. 02Raise k to 20: the curve flattens and misses the peaks of the wave.
  3. 03On the Outliers data, compare uniform and distance weighting near the outliers.

How it works

  1. 1Store the training data; there is no training step.
  2. 2For a new x, find the k points with the closest inputs.
  3. 3Average their targets, optionally weighting closer points more.

What the controls teach

Neighbours k
How many neighbours vote. Small k follows every point; large k smooths towards the mean.
Weighting
Uniform treats all k neighbours equally; by distance lets the closest ones dominate.

Use it when

  • Local patterns with no simple formula
  • Small, low-dimensional data

Avoid it when

  • High-dimensional inputs
  • Large datasets, where every prediction scans all points

More in Regression