AlgoViz

K-Nearest Neighbors Visualization

K Value:
3
Placement Mode:
Class Selection:
Show Grid
Show Decision Boundary
Class 1
Class 2
Class 3
Class 4
Test Point (colored by predicted class)

How It Works

K-Nearest Neighbors (KNN) is a simple supervised machine learning algorithm used for classification and regression:

  1. When a new point needs to be classified, the algorithm finds the K closest training points
  2. It assigns the new point to the class that appears most frequently among its K nearest neighbors
  3. The distance between points is typically calculated using the Euclidean distance
  4. The choice of K affects the decision boundary smoothness - lower values create more complex boundaries

Interactive Features

  • Add training points of different classes by clicking on the canvas
  • Add test points to see how they get classified
  • Adjust the K value to see how it affects classification
  • Toggle the decision boundary visualization to see classification regions
  • Generate random points to quickly test the algorithm