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.
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
- KNN optimizerused by · A DSPy optimizer that selects demonstrations dynamically by retrieving the k training examples most similar to each language-model call's inputs.
- radius detectoralternative to · A radius detector identifies or scores observations according to the number or arrangement of neighboring observations within a specified distance radius.
- propensity score methodimplemented by · A propensity-score method is a causal-inference technique that estimates treatment effects by matching, weighting, stratifying, or otherwise adjusting units according to their observed treatment probabilities.
- parameterconfigured by · A model parameter is a numerical value, such as a weight or bias, that is learned or set to determine how a model computes outputs.
- lazy evaluationuses · Lazy evaluation is an evaluation strategy that defers computing an expression or value until its result is needed, potentially avoiding the computation entirely.
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.


