SPSS command: SPSS KNN

Nearest neighbor analysis (KNN) in your browser

Nearest neighbor analysis classifies or predicts each case from the k most similar cases in a training sample.

Free · No account · Files stay on your device · Updated 18 October 2026

What it tells you

Crosstabs runs SPSS KNN on your device with adjusted normalization, Euclidean or city-block distance, k chosen by V-fold cross-validation, optional forward feature selection, and error rates on a random holdout sample.

  • How accurately the target can be classified (percent incorrect) or predicted (sum of squares error and relative error) from the chosen features.
  • Which k works best, from the cross-validation error at each k in the range you set.
  • Which features matter, from forward feature selection and the predictor importance chart.
  • Where the model confuses categories, in the training and holdout classification tables.

When to use it

Use it when

  • Assigning new respondents to existing segments from a few profile variables, without assuming a linear boundary.
  • A quick, assumption-light benchmark before fitting logistic or discriminant models.
  • Predicting a scale value (spend, score) as the mean or median of similar cases.

Use something else when

  • You need to explain how each predictor changes the outcome: KNN gives no coefficients; use logistic regression or a decision tree.
  • Many irrelevant features: distances become noisy; turn on feature selection or reduce dimensions first.
  • Survey weights must be respected: KNN ignores case weights and says so.
  • Very small training samples, especially when some categories have only a few cases.

How to run it in Crosstabs

  1. 01

    Open your data

    Open the file in the workspace and switch to Statistics mode.

  2. 02

    Choose Nearest neighbor analysis

    In the Machine learning group, pick Nearest neighbor analysis (KNN).

  3. 03

    Set target and features

    Under Variables choose the Target measurement (Categorical (classification) or Scale (prediction)), the Target, any Categorical features and Scale features; keep Normalize scale features (adjusted normalization) ticked.

  4. 04

    Choose the neighbors

    Under Neighbors keep Automatically select k with a Minimum k and Maximum k, or untick it and set Number of neighbors (k); choose Euclidean or City block distance and, optionally, Weight features by importance when computing distances.

  5. 05

    Feature selection and partitions

    Optionally tick Perform forward feature selection with a Stopping rule. Under Partitions set Training sample (%), Cross-validation folds and Random seed, then click Run.

Worked example

Computed by the Crosstabs engine from the synthetic data below when this page was built — not typed in.

Data

150 synthetic customers with annual income (thousands), share of income spent (%) and plan tier (basic, plus, pro) as features, and a three-way segment (Saver, Balanced, Spender) as the target. A seeded random partition puts about 70% of cases in training and 30% in holdout.

Model Summary
SettingValue
Number of nearest neighbors (k)5
DistanceEuclidean
Features in the modelincome, spend_share, plan
Training — Percent Incorrect28.2%
Holdout — Percent Incorrect27.7%

Training cases are predicted from their nearest other training cases (leave-one-out); holdout cases from the training cases.

Error Log for k Selection
kPercent Incorrect
331.0%
436.9%
529.0%
633.0%
731.9%
832.9%
932.9%

Error is averaged over 5 cross-validation folds; ties go to the smaller k.

Classification — Holdout
ObservedBalancedSaverSpenderPercent Correct
Balanced71458.3%
Saver110090.9%
Spender701770.8%
Overall Percent31.9%23.4%44.7%72.3%

How to read it

Five-fold cross-validation over k = 3 to 9 picked k = 5. With that k, 27.7% of the 47 holdout customers are misclassified (72.3% correct), against a leave-one-out training error of 28.2%. Because the holdout cases played no part in choosing k, the holdout error is the honest estimate of how the model will do on new customers.

Output tables explained

Case Processing Summary
Cases in training and holdout, valid cases and those excluded for a missing target or feature.
Model Summary
The k used, the distance, the features in the model and the error in training (leave-one-out) and holdout: percent incorrect for a categorical target, sum of squares error for a scale target.
Classification
Observed by predicted counts with percent correct, for the training and the holdout partitions (categorical targets).
Error Log for k Selection
Cross-validation error at each candidate k, averaged over the folds; ties go to the smaller k. With feature selection on, it is the leave-one-out training error.
Feature Selection Error Log
The order in which features entered and the error after each step, followed by a predictor importance chart.

Assumptions and notes

  • Similar cases have similar targets: the distance on the chosen features must be meaningful for the problem.
  • With adjusted normalization, scale features are rescaled to −1…1 over the training cases so that no feature dominates by its units; categorical features are one-of-c coded.
  • Cases with a missing target or feature are excluded; case weights are not used.
  • The training error is computed leave-one-out; the holdout error is the honest estimate for new cases.

How the results are validated

KNN is checked against a Python reference that finds neighbours with scikit-learn's brute-force NearestNeighbors on an independently encoded matrix and re-implements the SPSS voting, tie-breaking, Laplace probability, k-selection and feature-selection rules. Across fixed k, cross-validated k with city-block distance, both stopping rules, importance weighting and mean or median scale prediction, the neighbour sets and predicted categories are identical and probabilities and errors agree to at least 8 decimal places. Validation evidence

Questions

How is k chosen?

With Automatically select k, every k from Minimum k to Maximum k is scored by V-fold cross-validation on the training cases and the lowest error wins, ties going to the smaller k. The seed makes the folds reproducible.

Euclidean or city-block distance?

Euclidean is the default and suits most data. City block sums absolute differences, so one feature with a large gap counts less than under Euclidean distance; try both and compare the holdout error.

Why is the training error close to the holdout error?

Training cases are predicted leave-one-out — each from its nearest other training cases — so the training error is not the trivially perfect fit a case would get as its own neighbour.

Can KNN predict a numeric target?

Yes. Set Target measurement to Scale (prediction); the prediction is the mean or median of the k neighbours, and the panel reports sum of squares error and the relative error.

Is my data uploaded?

No. Distances, cross-validation and feature selection all run in a Web Worker in your browser.

Run Nearest Neighbor Analysis (KNN) on your own data

Open a .sav, CSV or Excel file. Everything runs in your browser; the file never leaves your device.

Open the workspace

SPSS is a trademark of IBM Corp. Crosstabs is not affiliated with or endorsed by IBM.