K-nearest-neighbour classification
Predict a sample from the labels or values of nearby training examples.
The white point is an unlabelled query in the same feature space.
Distance is computed in the chosen feature space. Scaling the features changes the neighbourhood.
Only the five closest samples vote in this K = 5 classification example.
The query receives the mint class, with 5 of five votes.
Aggregate their labels or numeric responses.
Distances and votes are computed from the displayed two-dimensional sample set.
Try it on an image
Experiment at pixel level
Train a colour classifier from the rectangle (foreground) and image border (background). Classify pixels by their nearest training colours.
The engine loads on your first run. Your images stay in this browser.
Scroll over either image to zoom at the pointer. Use the scrollbars to pan both views over the same relative area. Zoom is relative to the input; pixel coordinates belong to each image. Warps can change scene correspondence.
Pixel inspector RGBA · native values · matched scale · 9 × 9 output pixels
Select a pixel
Select a pixel
When to use it
Build a simple classifier for small descriptor sets or establish a baseline before fitting a more complex model.
How it works
- 01Store training feature vectors and responses.
- 02Find the closest K vectors to a query.
- 03Aggregate their labels or numeric responses.
query → K nearest examples → vote or average
What to tune
K trades local sensitivity against smoothing; isClassifier selects classification or regression behavior.
Where it breaks down
Prediction depends strongly on feature scaling and distance. Query cost and memory grow with the stored training data.
TypeScript API
Open an entry for its exact overloads, parameter descriptions, result ownership and pinned upstream source.
All of these calls execute on the CPU. Native objects need explicit disposal. See matrices and ownership and build compatibility.
Related methods
K-means clustering
Partition feature vectors around a fixed number of learned centres.
Support vector machines
Learn a decision boundary that balances class separation against margin violations.
Random forests
Combine decisions from trees that split feature space into regions.
Principal component analysis
Find orthogonal directions that explain the greatest variance in centred data.