K-Nearest Neighbors (KNN) Calculator

Read the Theory Guide →

Enter labeled training points with numeric features, set a query point and K, and calculate the predicted class. The solver shows each distance, ranks the training points, and lets the nearest K neighbors vote. Change the data, query point, or K and recalculate to see how the prediction changes.

What You Can Explore

  • Edit the table by changing numeric feature values and class labels, and by 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 get a vote. K must be at least 1 and no larger than the number of training rows.
  • Press Calculate KNN and follow the distances, ranked neighbors, selected neighbors, and final majority vote.
Core Mechanics
▼
  • The Lazy Learner: KNN does zero computation at training time; it simply memorizes the data. Because it has no model to "learn," it must scan the entire dataset for *every single* prediction you ask it to make.
  • Feature Sensitivity: KNN relies entirely on distance. If your features have different scales (e.g., age vs. income), the large-scale feature will completely dominate the calculation. Always *normalize* your data first!
  • The Curse of Dimensionality: As you add more features, the concept of "nearest" starts to break down because points become sparse. In high-dimensional spaces, distance metrics lose their meaning and KNN accuracy drops.
  • The KK Bias/Variance Tradeoff: Small KK values overfit to local noise (high variance). Large KK values smooth over real boundaries (high bias). Always pick an *odd* KK to guarantee a clear winner.
Initializing Workspace...

Understanding Your Result

The prediction comes from your training data, query point, and K. The solver first measures the distance from the query point to every training row, ranks the rows from nearest to farthest, and then uses the nearest K class labels to determine the prediction.

  • Predicted Class is the class with the most votes among the K nearest neighbors. Rows below the top K have no effect on the prediction.
  • Step 1 calculates the distance from the query point to every training row. Step 2 sorts those rows from nearest to farthest, and the first K rows become the neighbors.
  • Distances are calculated from the feature values only. The class label does not affect which rows are nearest.
  • After an edit, check the Step 2 ranking first. If the same neighbors remain but the prediction changes, the change came from their class labels rather than their distances.

Try These Experiments

Move the Query Point

Setup: Run the preloaded table with K = 3 and the query point (3, 7). The nearest rows are P3, P4, and P1, giving a predicted class of Good.

Action: Change the query point from (3, 7) to (7, 6), keep the same training data and K, and press Calculate KNN again.

What to Observe: The ranking changes to P1, P2, and P3 as the three nearest rows, with distances 1, 2, and 4.472. P1 and P2 are both Bad, so the predicted class changes to Bad.

Takeaway: Changing the query point changes which training rows are closest. Even with the same training data and the same K, a different query can therefore produce a different prediction.

Change a Neighbor's Class

Setup: Run the preloaded table with K = 3. P3, P4, and P1 are the three nearest neighbors, with classes Good, Good, and Bad.

Action: Change P4's class label from Good to Bad without changing any feature values, then calculate KNN again.

What to Observe: The distances and ranking stay exactly the same because the feature values did not change. The three neighbors are still P3, P4, and P1, but their classes are now Good, Bad, and Bad, so the prediction changes to Bad.

Takeaway: Feature values decide which points become neighbors, while class labels decide what those neighbors vote for. A label change can therefore change the prediction without changing any distance.

Add a Close Point and Change K

Setup: Run the preloaded table with K = 3 and the query point (3, 7). The prediction is Good because P3, P4, and P1 are the three nearest neighbors.

Action: Add a new training row with features (3, 6) and class Bad. Run KNN with K = 3, then change K to 1 and run it again.

What to Observe: The new point is only 1 unit from the query, so it becomes the nearest neighbor. With K = 3, the selected neighbors are the new Bad point, P3 Good, and P4 Good, so the prediction remains Good. With K = 1, only the new Bad point votes, so the prediction becomes Bad.

Takeaway: K controls how many nearby points influence the prediction. A small K gives more influence to the closest points, while a larger K allows more neighbors to contribute to the vote.