K Selection Interactive Simulator¶
Description¶
An interactive visualization demonstrating how the choice of k affects KNN decision boundaries and prediction behavior.
Learning Objectives¶
- Analyze how k value affects the bias-variance tradeoff in KNN
- Understand why k selection is critical for KNN performance
- Observe how different k values create different decision boundaries
- Recognize the relationship between k=1 and Voronoi diagrams
How to Use¶
- Adjust k: Use the slider to change the number of neighbors (1-25)
- Drag Test Point: Click and drag the red test point to see predictions change
- Show Voronoi: Toggle to visualize Voronoi cells when k=1
- Add Noise: Add outlier points to see how noise affects small k values
- Reset: Clear noise points and return to original data
Key Concepts¶
Small k (k=1)¶
- High variance, low bias
- Decision boundary follows training data closely
- Sensitive to noise and outliers
- Creates Voronoi diagram partitions
Large k (k>20)¶
- Low variance, high bias
- Smooth decision boundaries
- May be too simple (underfit)
- Less affected by individual points
Optimal k¶
- Balances bias and variance
- Often between 3-15 for many datasets
- Should be determined by cross-validation
Interactive Features¶
- Real-time Decision Boundaries: See how k affects class regions
- Neighbor Connections: Lines show which points influence prediction
- Vote Breakdown: See exact vote counts for each class
- Distance Display: View distances to nearest neighbors
- Warning Indicators: Alerts for extreme k values