liveBook · Manning

K-nearest neighbors

K-nearest neighbors is a method that predicts a query’s class or value from the labels or values of its k closest training examples, as measured by a specified distance or similarity.

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 similar training examples as demonstrations for each language-model call rather than using one fixed prompt template.
How deep it goes
Explains when examples are encoded and selected, then shows optimizer configuration; does not specify a similarity metric.
Examples
Illustrates flight-query intent classification and a Python configuration that retrieves three demonstrations.
What you can do after
Apply it via DSPy KNNFewShot to compile a predictor with dynamically selected demonstrations.

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

Related concepts

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.