liveBook · Manning

K-nearest neighbors

Also called knn

K-nearest neighbors is a method that predicts an instance’s class or value from those of its k closest training examples, according to a distance or similarity measure.

How 3 Manning books explain it

Outlier Detection in Python

A k-nearest-neighbors algorithm scores a record using its average distance to its k nearest neighboring records.

How it is used
Scores records for distance-based outlier detection, including unusual values in multidimensional data.
How deep it goes
Explains average-distance scoring, contrasts kth-neighbor scoring, and implements neighbor queries; leaves selection of a reliable k unresolved.
Examples
Uses two synthetic clusters and prepared baseball records to plot scores and examine flagged points.
What you can do after
Apply it via scikit-learn’s BallTree to score records, then inspect score distributions and flagged rows.

Read how Outlier Detection in Python explains k-nearest neighbors →

Causal Inference for Data Science

K-nearest neighbors is a prediction method that uses the outcomes of the closest training observations to predict an input's outcome.

How it is used
Uses outcome predictions from nearby propensity-score observations to estimate treatment effects and average treatment effects.
How deep it goes
Explains separate-group models, counterfactual predictions, averaging, and cross-validated k selection; stops short of broader algorithmic internals.
Examples
Uses treated and control patient datasets containing outcomes and propensity scores, with individual treatment effects averaged into an ATE.
What you can do after
Apply kNN to estimate counterfactual outcomes and calculate an average treatment effect from treated and control data.

Read how Causal Inference for Data Science explains k-nearest neighbors →

Building LLM Applications with DSPy

The k examples most similar to a query according to a measure of similarity.

How it is used
Selects input-specific demonstrations for language-model prompts instead of using a fixed set for every call.
How deep it goes
Explains training-set storage and encoding, followed by similarity-based selection at call time.
Examples
Uses a flight-time utterance to illustrate selection of similar intent-classification examples.
What you can do after
Apply it via DSPy’s KNNFewShot with a sentence-transformer encoder and a training set.

Read how Building LLM Applications with DSPy explains k-nearest neighbors →

Also covered in

What to know first

Related concepts

Questions about k-nearest neighbors

What is k-nearest neighbors?

K-nearest neighbors is a method that predicts an instance’s class or value from those of its k closest training examples, according to a distance or similarity measure.

Which Manning books explain k-nearest neighbors?

Outlier Detection in Python, Causal Inference for Data Science and Building LLM Applications with DSPy.

What should I know before learning k-nearest neighbors?

Manning books that teach k-nearest neighbors usually introduce K-means clustering, kernel function and normal distribution first.

What is the difference between k-nearest neighbors and ODIN?

K-nearest neighbors is a method that predicts an instance’s class or value from those of its k closest training examples, according to a distance or similarity measure. ODIN (outlier detection using in-degree number) is an outlier-detection algorithm that scores observations by their incoming nearest-neighbor relationships.

What is k-nearest neighbors a kind of?

K-nearest neighbors is a kind of global detector. A detector that evaluates how unusual an observation is relative to a global standard derived from the full dataset.

What is k-nearest neighbors also called?

K-nearest neighbors is also called knn.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.