Concept 3

Choosing K controls how local the model is

Small \(K\) listens to very local evidence. Large \(K\) smooths the prediction across a wider neighborhood.

Small K vs large K

K valueBehaviorRisk
Small \(K\)Flexible, local boundaryNoise and overfitting
Large \(K\)Smoother, more stableUnderfitting
\[ K=1 \Rightarrow \text{prediction follows the nearest point} \]
\[ K=n \Rightarrow \text{prediction follows the global majority or average} \]
small K: wiggly large K: smooth

Small \(K\) can overreact. Large \(K\) can oversmooth.

Bias-variance intuition

KBiasVarianceModel behavior
SmallLowHighFits local noise
MediumBalancedBalancedOften best validation performance
LargeHighLowToo smooth
Choosing \(K\) is model tuning. We should not choose it by looking at test performance repeatedly.

Overfitting and underfitting in KNN

Prediction stability

If one mislabeled training point is closest to the query, which value of \(K\) is most likely to make a wrong prediction: 1, 3, or 5?

\(K=1\), because the model blindly trusts the closest single point.

KNN overfits when it reacts too strongly to individual training points. This usually happens when \(K\) is very small.

KNN underfits when it averages over too many neighbors. This usually happens when \(K\) is very large.

CaseWhat happensClassroom intuition
\(K=1\)Every point is controlled by the nearest single example.One noisy neighbor can flip the answer.
\(K=3\) or \(K=5\)A small local group votes.Noise is reduced while locality is preserved.
Very large \(K\)The model listens to too many faraway points.Local patterns get washed out.
\[ \text{Small }K \Rightarrow \text{low bias, high variance} \]
\[ \text{Large }K \Rightarrow \text{high bias, low variance} \]

Outlier sensitivity

Noisy neighbor question

If one nearby point is mislabeled, should one neighbor be allowed to decide the prediction alone?

Usually no. This is why \(K=1\) is flexible but risky.

Small \(K\) makes KNN sensitive to outliers and mislabeled examples. Increasing \(K\) or using distance-weighted voting can reduce the influence of one bad point.

SettingEffect of one outlier
\(K=1\)Can fully control prediction.
\(K=5\)Must outvote several neighbors.
Distance-weighted KNNOutlier matters only if it is very close.
K=1 follows blue outlier; K=5 predicts red

A larger neighborhood can protect the prediction from a single noisy point.

Visualization: K = 1, 3, and 5

The same query point can receive different predictions when \(K\) changes.

K = 1 predicts blue K = 3 predicts red K = 5 predicts red nearest one is blue 2 red vs 1 blue 3 red vs 2 blue

Increasing \(K\) increases the neighborhood size. The prediction becomes less sensitive to one nearby point.

Decision boundary changes with K

Boundary shape question

Which boundary should be smoother: \(K=1\) or \(K=15\)?

\(K=15\), because each prediction averages a larger neighborhood.

K = 1 K = 5 K = 15 very local and jagged balanced local pattern smooth and less flexible

KNN can make nonlinear boundaries, but \(K\) controls how sharply those boundaries react to the data.

KNN regression smoothing

Smoothing question

For house-price prediction, would \(K=1\) or \(K=25\) produce a smoother price curve?

\(K=25\), because it averages across more neighboring houses.

K = 1K = 7K = 25 memorizes noisecaptures trendmay oversmooth

For regression, increasing \(K\) smooths the prediction curve. Too small can overfit; too large can underfit.

How do we choose K?

  1. Split data into train and validation, or use cross-validation.
  2. Try candidate values: \(K=1,3,5,7,\ldots\).
  3. Evaluate using the right metric.
  4. Pick the \(K\) with best validation performance.
  5. Evaluate final model once on test data.

For classification, odd values of \(K\) are often used to reduce ties in binary classification.

best validation K train validation K value validation score

Choose \(K\) using validation or cross-validation performance.

Effective methods to choose K

MethodHow it worksWhen useful
Validation curveTry many \(K\) values and plot validation score.Best classroom method because the bias-variance pattern is visible.
Cross-validationAverage performance across folds for each \(K\).More reliable when data is not very large.
Grid searchTune \(K\), distance metric, and weights together.Useful for real projects.
Elbow regionChoose a stable \(K\) where score stops improving significantly.Useful when many nearby \(K\) values perform similarly.
Domain constraintPrefer a \(K\) that makes sense operationally.Useful when explainability matters.

A practical starting range is odd values such as:

\[ K\in\{1,3,5,7,9,11,15,21,31\} \]

Some people use \(\sqrt{n}\) as a rough starting point, but it is only a heuristic. Validation performance should make the final decision.

If several \(K\) values perform almost equally well, prefer the simpler and more stable option, usually the slightly larger \(K\).

Parameter tuning

KNN has more than one hyperparameter.

HyperparameterChoicesEffect
`n_neighbors`1, 3, 5, 7, ...Controls locality
`metric`Euclidean, Manhattan, cosineDefines similarity
`weights`uniform, distanceWhether closer neighbors count more
`p` in Minkowski1 or 2 commonlyManhattan vs Euclidean
For classification, tune \(K\) using the metric that matches the problem: accuracy for balanced classes, F1/recall/precision for imbalanced classes, and RMSE/MAE for regression.

Distance-weighted KNN

Weighted vote

If one neighbor is extremely close and four neighbors are far away, should every neighbor have equal importance?

Not always. Distance weighting lets closer neighbors count more.

Sometimes closer neighbors should influence the prediction more.

\[ w_i=\frac{1}{d(x,x_i)+\epsilon} \]

For regression:

\[ \hat{y}=\frac{\sum_{i\in N_K(x)}w_i y_i}{\sum_{i\in N_K(x)}w_i} \]

For classification, use weighted voting rather than simple majority voting.

KNN with imbalanced classes

Imbalance question

If 95% of customers do not churn, what class is likely to appear more often in most neighborhoods?

The majority class. KNN can miss minority-class regions when the neighborhood is dominated by majority examples.

Class imbalance affects KNN because voting depends on local counts. If minority-class examples are rare, even a minority-like query can be surrounded by many majority-class points.

MethodHow it helps
Use recall/F1/PR-AUCEvaluates minority detection properly.
Distance weightingVery close minority neighbors can matter more.
ResamplingChanges the neighborhood composition during training.
Tune threshold from probabilitiesUses local vote fraction more carefully.
minority signal can be outvoted locally

A local neighborhood can still be majority-heavy even near a minority-like point.

Curse of dimensionality

Dimension question

If we keep adding features, will “nearest” always become more meaningful?

No. In high dimensions, points often become similarly far away, so neighborhoods become less reliable.

KNN works best when distance captures similarity. In high dimensions, data becomes sparse and the nearest point may not be much closer than the farthest point.

\[ \text{distance contrast}=\frac{d_{max}-d_{min}}{d_{min}} \]

As dimensionality grows, this contrast often shrinks. That means nearest and farthest examples can start looking almost equally far.

1D2DHigh dimensions near and far are obviousgeometry still visibledistances become less distinct

More features are useful only if they add signal. Too many weak features can make distance less meaningful.

Common remedies include feature selection, dimensionality reduction, better embeddings, and choosing a metric that matches the data.

Fast nearest-neighbor search for large datasets

Brute-force KNN compares the query point with every training point.

\[ \text{brute force cost per query} \approx O(n\cdot p) \]

When \(n\) grows large, prediction can become slow. We can speed it up using smarter search structures or approximate search.

MethodIdeaBest forLimitation
KD-treeSplit space using feature axes.Low-dimensional numeric data.Weak in high dimensions.
Ball treeGroup points into nested balls.Medium dimensions and non-axis-aligned neighborhoods.Still weak when dimension is very high.
Approximate nearest neighborsSearch quickly and accept near-best neighbors.Very large datasets, embeddings, recommendations.May miss the exact nearest neighbor.
Dimensionality reductionReduce features before neighbor search.High-dimensional noisy data.Can lose information.
Feature selectionRemove irrelevant features.Any KNN problem with noisy columns.Needs careful validation.

In scikit-learn, `KNeighborsClassifier` supports:

`algorithm`Meaning
`brute`Compare with all training points.
`kd_tree`Use KD-tree search.
`ball_tree`Use Ball-tree search.
`auto`Let the library choose.
query partition space, search fewer regions

Tree-based methods avoid checking every point when the geometry is favorable.

For text embeddings or very high-dimensional data, approximate nearest-neighbor libraries such as FAISS, HNSW, Annoy, or ScaNN are often more effective than exact KD-tree search.
Previous: Distances Next: Imputation