K-Nearest Neighbors & Decision Boundaries
Witness the Bias-Variance tradeoff live: how neighborhood size $K$ shifts the classifier from overfitting to underfitting
Non-Parametric Estimation & Majority Voting
Unlike parametric models (e.g. Logistic Regression) that assume a specific functional form, the K-Nearest Neighbors (KNN) algorithm makes no prior structural assumptions about the data distribution. Given a query point $\mathbf{x} \in \mathbb{R}^d$, KNN locates the set $\mathcal{N}_K(\mathbf{x})$ of the $K$ closest training samples and estimates class probabilities via local empirical frequencies:
$$P(Y = c \mid \mathbf{x}) = \frac{1}{K} \sum_{i \in \mathcal{N}_K(\mathbf{x})} \mathbb{I}(y_i = c)$$
The predicted label is the plurality winner: $\hat{y} = \arg\max_c P(Y = c \mid \mathbf{x})$.
Interactive Classification Workbench
Optimal GeneralizationWhen $K=1$, the decision boundary tightly wraps around every single training observation. Training error is zero, but variance is extreme: any slight label noise creates an isolated "island" in feature space that devastates test-set generalization.
As $K$ approaches sample size $N$, local neighborhood context is lost. The model votes become dominated by the global class balance, causing high bias. Complex geometric boundaries (like interlocking spirals or moons) are smoothed out into flat planes.
Theoretical Foundations & Nuances
1. The Curse of Dimensionality
In high-dimensional spaces ($d \gg 10$), the volume of space grows exponentially. All data points become approximately equidistant from one another ($\lim_{d \to \infty} \frac{\text{dist}_{\max} - \text{dist}_{\min}}{\text{dist}_{\min}} \to 0$), severely degrading distance-based metrics.
2. Computational Complexity
KNN is a lazy learner: training cost is $O(1)$, but inference cost is $O(n \cdot d)$ per query because all pairwise distances must be calculated. In production systems, $k$-d trees or Approximate Nearest Neighbor (ANN) indexes like HNSW are used.
3. Metric Sensitivity
Switching between Euclidean ($L_2$) and Manhattan ($L_1$) changes the shape of equidistant contours from circles to diamonds. In grid-like feature spaces (such as city blocks or taxicab routing), $L_1$ often outperforms $L_2$.