Concepts 🚀 Project 🧮 Labs
🧮 Labs / ML Advanced / Interactive: KNN — drag K and watch the boundary change

Interactive: KNN — drag K and watch the boundary change

DRAG the K slider. SEE the decision boundary morph from jagged (K=1) to smooth (K=20). SEE how each K affects the classification of a new point.

interactive.lab v3.1
Drag the sliders — see the graph update in real-time
Live interactive chart drag sliders → graph updates
If you see this, the interactive chart is loading...
If it doesn't appear in 2 seconds, press Ctrl+Shift+R to hard refresh.
🧠 How it works

INTERACTIVE KNN: K is the most important parameter — it controls the bias-variance tradeoff. K=1 (OVERFIT): every training point is its own nearest neighbor. Boundary is JAGGED — captures noise as if it were signal. 100% train accuracy, terrible test accuracy. K=5-10 (GOOD): smooth boundary, captures the real pattern. The boundary follows the general shape of the data. K=20 (UNDERFIT): boundary is too smooth. Ignores local structure — predicts the majority class everywhere. RULE OF THUMB: K = √(n_samples). Always use ODD K (prevents ties). DRAG the slider and watch the green X change color as it crosses the boundary!

✏️ Try these changes
  • Set K=1 — extreme overfitting
    → boundary is jagged, green X prediction depends on exact position
  • Set K=25 — extreme underfitting
    → boundary is almost flat, predicts majority class everywhere
  • Set K=7 — good balance
    → smooth boundary that captures the real pattern