Classification · Beginner

K-Nearest Neighbors

See distances, the nearest K samples, votes, and the local decision boundary.

KNN stores training samples and predicts from nearby examples. It does not learn iterative parameters, so ModelOrigen visualizes real distances, neighbors, votes, and the resulting boundary instead of inventing epochs.

Step by step

  1. Store the training samples.
  2. Choose a query point.
  3. Measure distance from the query to every training sample.
  4. Sort by distance and keep the closest K.
  5. Combine uniform or distance-weighted votes.
  6. Return the predicted class and inspect the local boundary.

Core formulas

Euclidean distance

d(x,z) = √Σ(xⱼ − zⱼ)²

Straight-line distance in feature space.

Distance weighting

weightᵢ = 1 / max(dᵢ, ε)

Closer neighbors can receive more influence when distance weighting is enabled.

When to use K-Nearest Neighbors

  • The dataset is not huge and local similarity is meaningful.
  • A nonlinear local boundary is useful.
  • You want a simple instance-based baseline.

Related interactive lessons

Primary references

scikit-learn Nearest Neighbors