K-Nearest Neighbors (KNN) Regression Calculator

Read the Theory Guide →

Enter training points with numeric features and target values, set a query point and K, and calculate a prediction. The solver shows every distance, ranks the nearest neighbors, and averages their target values. Change K, the query point, or any data value and recalculate to compare the result.

What You Can Explore

  • Edit the table of numeric features and target values by changing cells and adding or removing rows.
  • Set the query point by entering its feature values in the Target row at the bottom of the table.
  • Change K to choose how many of the nearest training points contribute to the prediction.
  • Press Calculate Prediction and follow the distances, ranked neighbors, and final average step by step.
Core Mechanics
▼
  • Same Start, Different End: Like Classification, it begins by finding the KK nearest neighbors. The only difference is that instead of a category, you are predicting a continuous number.
  • Average, Don't Vote: Since you aren't choosing a class, you take the *mean* (average) of your KK neighbors' values. The result is a specific numerical prediction, not a count.
  • Outlier Sensitivity: In Classification, one rogue neighbor rarely changes the outcome. In Regression, a single extreme neighbor can dramatically pull your average away from the truth.
  • The KK Smoothness Tradeoff: A small KK captures local detail but is jittery and noisy. A large KK produces smooth predictions but can drift too far toward the global average, losing all local accuracy.
Initializing Workspace...

Understanding Your Result

The prediction is calculated from your training data, query point, and K. The solver first measures the distance from the query point to every training row, ranks those rows from nearest to farthest, and then averages the target values of the nearest K rows.

  • Predicted Value is the plain average of the K nearest target values. Every selected neighbor contributes equally to the prediction, regardless of its exact distance.
  • Step 1 calculates distance using only the feature columns. The target value of a training row does not affect how close that row is to the query point.
  • Step 2 orders the training rows by distance. Only the first K rows become neighbors and contribute to the final prediction; rows below that cutoff have no effect.
  • If you change a feature value or the query point, check the ranking first because the nearest neighbors may change. If the selected neighbors stay the same, changing their target values changes the prediction without changing the ranking.

Try These Experiments

Raise K to Include Every Row

Setup: Run the preloaded table with the query point (3, 7) and K = 3. The predicted value is about 43.33, using the targets 55, 45, and 30 from P5, P3, and P4.

Action: Change K from 3 to 5, which includes all five training rows, and press Calculate Prediction again.

What to Observe: The prediction becomes 57 because all five target values are now averaged. Compare this with 43.33 from the first run, where only the three nearest rows were used.

Takeaway: As K grows, more training points influence the prediction. When K equals the number of training rows, every target is included, so the query point no longer affects which rows are selected and the prediction becomes the overall mean target value.

Move the Query Point Onto a Training Row

Setup: Run the preloaded table with K = 3 and note that P1, at (7, 7) with target 85, is fourth nearest to the query point (3, 7).

Action: Change the query point from (3, 7) to (7, 7), the same feature values as P1, and run the calculation again.

What to Observe: P1 now has distance 0 and becomes the nearest neighbor, followed by P2 and P5. The prediction is 70 because the three selected target values are 85, 70, and 55.

Takeaway: An exact feature match makes a training row the nearest neighbor, but KNN regression still averages K neighbors. With K greater than 1, the matching row's target is combined with the targets of the other selected neighbors.

Inflate One Neighbor's Target

Setup: Run the preloaded table with K = 3. The three nearest neighbors are P5, P3, and P4, with target values 55, 45, and 30.

Action: Change P3's target value from 45 to 450 while keeping its features (3, 4) unchanged, then calculate the prediction again.

What to Observe: The distances and neighbor ranking stay the same because P3's features did not change. The prediction rises from about 43.33 to about 178.33 because the final average now uses 55, 450, and 30.

Takeaway: The features decide which rows become neighbors, while their target values decide the final average. An extreme target among the selected neighbors can therefore have a large effect on the prediction without changing the neighbor ranking.