Home / Classification

K-Nearest Neighbors

Drag the target point to see how the KNN algorithm classifies it based on proximity.

Parameters

3
5

Visualization

Drag the target circle to classify.

Real-time Analysis

Result: Based on the 5 nearest neighbors, the target is classified as:

Predicted Class
--

Neighbor Votes

Inside K-Nearest Neighbors (KNN)

The K-Nearest Neighbors (KNN) algorithm is one of the simplest and most intuitive classification algorithms in machine learning. Its core idea is based on the saying, "You are known by the company you keep." To classify a new, unknown data point, KNN looks at the 'K' closest data points from the training set and makes a prediction based on a majority vote. This lab lets you explore this process interactively.

How KNN Works: A Simple Democracy

Imagine you have a new student (the target point) and you want to predict which class they belong to. KNN doesn't learn a "model" in the traditional sense. Instead, it follows a simple, lazy procedure at prediction time.

1. Find the Neighbors

First, the algorithm calculates the distance (usually Euclidean distance) from the new target point to every single point in the training data. It then identifies the 'K' points that are closest to the target—these are its "nearest neighbors." In the visualization, a circle is drawn around the target to show the neighborhood, and lines connect to the K neighbors within it.

2. Hold a Vote

Next, the algorithm looks at the classes of these K neighbors and holds a vote. Each neighbor gets one vote for its class. The class with the most votes wins, and that becomes the prediction for the new target point. The "Neighbor Votes" panel on the right shows this process in real-time.

Guided Experiments

The most crucial parameter in KNN is 'K'. Let's see how it affects the outcome.

  1. The Effect of a Small K (K=1): Set the "Neighbors (K)" slider to 1. Now, drag the target point around the canvas. Notice that the prediction is extremely volatile; it changes to the class of the single closest point. This creates a very complex and jagged decision boundary, which is a classic sign of high variance and overfitting. The model is too sensitive to individual data points, including noise.
  2. The Effect of a Large K (K=20): Now, set the "Neighbors (K)" slider to its maximum value, 20. Drag the target point again. You'll see that the prediction is much more stable and changes less frequently. The decision boundary becomes much smoother. However, if K is too large, the model might become too generalized and ignore local patterns, leading to high bias and underfitting. For example, a small cluster of one class might be "outvoted" by the more dominant class in the wider neighborhood.
  3. Finding a "Good" K: Set K to a moderate value, like 5 or 7. Drag the target point to a boundary region between two classes. With K=5, if three neighbors are Class A and two are Class B, the target will be classified as A. Now, slowly increase K. You might find a point where the prediction flips because the expanding neighborhood now includes more points from Class B. This demonstrates the trade-off: a good K balances the influence of local patterns with overall stability.
  4. The "Curse of Dimensionality": While this 2D visualization is simple, imagine if we had 100 features (dimensions). In high-dimensional space, the concept of "distance" becomes less meaningful. All points tend to be far away from each other, making it difficult to find truly "close" neighbors. This is a major challenge for KNN in practice.

Key Takeaways

  • Lazy Learning: KNN is a "lazy" algorithm because it doesn't do any work during the training phase. It simply stores the entire training dataset. All the computation happens at prediction time.
  • Non-Parametric: It makes no assumptions about the underlying data distribution. This allows it to learn complex, non-linear decision boundaries.
  • Computationally Expensive at Prediction: Because it has to compare the new point to every single training point, making predictions can be slow for large datasets.
  • Importance of Feature Scaling: Since KNN relies on distance, features with larger scales can dominate the distance calculation. It's crucial to scale your data (e.g., using normalization or standardization) before applying KNN.
Cheat sheet

K-Nearest Neighbors

The K-Nearest Neighbors (KNN) algorithm is one of the simplest and most intuitive classification algorithms in machine learning. Its core idea is based on the saying, "You are known by the company you keep." To classify a new, unknown data point, KNN looks at the 'K' closest data points from the training set and makes a prediction based on a majority vote. This lab lets you explore this process interactively.

MACHINE LEARNING · vizlearn.in/machine_learning/knn.html