Multivariate & ML · Machine Learning

k-Nearest Neighbors (k-NN)

Classify or predict with k-nearest neighbors: cross-validated auto-selection of k with the bias-variance score curve, honest CV metrics, and a decision-boundary plot for two predictors.

In the menus Multivariate & ML › Machine Learning › k-Nearest Neighbors (k-NN)

Also searched as knn k nearest neighbors nearest neighbour nearest neighbor

When to use it

Predict a category or a number from numeric predictors when you expect the relationship to be local and possibly very non-linear, and you do not need an interpretable equation. A categorical (or two-level) outcome is classified by majority vote of the k nearest rows; a continuous outcome is predicted by averaging them.

When not to: More than about 10 predictors (the curse of dimensionality makes 'nearest' stop being meaningful), a need for an interpretable coefficient or equation (k-NN has none to offer), unscaled predictors left unstandardized, or very large datasets where per-prediction search cost becomes a practical bottleneck.

Step by step

  1. Open it from the menu

    Click Multivariate & ML in the menu bar; under Machine Learning choose k-Nearest Neighbors (k-NN).

  2. Fill in the form

    Choose the columns: What you want to predict, Columns that may explain it. The form lists only columns whose type fits each field.

  3. Check the options

    The options start at their defaults: Neighbors (k), Auto-select k, Neighbor weights, Standardize predictors, CV folds. Change one only when your question needs it.

  4. Run the analysis

    Press Run analysis. The Result Card opens on its Results tab.

What to choose in the form

Fields of the k-Nearest Neighbors (k-NN) form
What you want to predict Outcome requiredOne categorical or binary or numeric column. Outcome to predict. Categorical or two-level → classification; continuous numeric → regression.
Columns that may explain it Predictors requiredOne numeric column. One or more numeric predictors (distances are computed on these).
Neighbors (k)Number of nearest neighbors. Used when auto-select is off. Default: 5.
Auto-select kPick k by cross-validation over k = 1, 3, …, 25 and report the score curve (ties go to the larger, smoother k). Default: on.
Neighbor weightsuniform = plain majority vote / average; distance = closer neighbors count more (1/d). Default: uniform.
Standardize predictorsz-score predictors inside each CV fold so large-unit variables do not dominate the distance. Strongly recommended. Default: on.
CV foldsCross-validation folds (stratified for classification), seeded for reproducibility. Default: 5.

How to read the card

The Results tab starts with what to know first (rows left out, checks that did not pass), then the interpretation in words and the Key Results; charts and assumption checks follow further down. Learn More explains the method in plain words. Read a Result Card covers all four tabs.

A worked example

Classifying penguin species from bill length and flipper length, with auto-select k searching 1 through 25 via 5-fold cross-validation.

Result: The search's best score occurs at k = 5 (selection_score = 96.2% accuracy); the nested cross-validation estimate for the whole procedure is 95.9%, with the outer folds choosing k = 13, 7, 5, 5 and 3.

The gap between 96.2% and 95.9% is the optimism introduced by searching 13 candidate k values and reporting the winner; it is small here because the curve is flat near its peak, but 95.9% is the number that honestly reflects how the whole k-selection procedure would perform on new penguins. The spread of k across outer folds is a reminder that the 'best k' itself is uncertain.

From the Statistics Library.

The code tabs

Code gives this analysis as Python, R and MATLAB code (and SAS where the tab offers it), reading the data the way KernelStats did. Export sends the card to a report or to slides. See The Code tab.

Understand the statistics

Ask Oobi

Type one of these into Ask Oobi beside your data:

  • Can bill and flipper measurements alone identify a penguin's species?
  • Explain the k-Nearest neighbors (k-NN) in plain words.
  • What does overfitting mean?

Last reviewed November 16, 2026.

Report an unclear step