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.
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
- K-means clusteringK-means clustering is an algorithm that partitions data into a specified number of clusters by iteratively assigning observations to the nearest centroid and updating each centroid to the mean of its assigned observations.
- kernel functionA kernel function is a mathematical function that measures similarity between inputs, often by representing an inner product in an implicit feature space.
- normal distributionA normal distribution is a continuous probability distribution with a symmetric, bell-shaped density determined by its mean and variance.
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.
- lazy evaluationuses · Lazy evaluation is an evaluation strategy that defers computing an expression until its value is needed, potentially avoiding the computation entirely.
- ODINalternative to · ODIN (outlier detection using in-degree number) is an outlier-detection algorithm that scores observations by their incoming nearest-neighbor relationships.
- 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.
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.


