Nearest-Neighbor Algorithms
Explore k-nearest neighbors, radius neighbors, centroid classifiers, Ball Tree, and KD Tree in JavaScript and TypeScript with @kanaries/ml.
Module overview
The Neighbors module supports instance-based learning and efficient nearest-neighbor search. It is useful for similarity tasks, recommendation flows, retrieval systems, and classifiers that reason directly from nearby examples instead of learning a compact parametric model.
This module is a strong fit when:
- prediction or ranking depends on nearby examples
- you need repeated nearest-neighbor queries over a relatively stable dataset
- you want to compare a simple KNN baseline with explicit search structures
JavaScript implementation
@kanaries/ml provides nearest-neighbor algorithms in JavaScript and TypeScript so teams can keep indexing, lookup, and local prediction inside the same browser or Node.js runtime as the rest of the product logic. This is especially useful for recommendation tools, search experiences, and interactive similarity workflows that need low-friction integration with application code.
If someone searches for "k-nearest neighbors in JavaScript", "KD Tree in JavaScript", or "Ball Tree in TypeScript", this module is the right entry point.
Quick navigation
- k-Nearest Neighbors: direct classification from nearby labeled examples
- KNeighborsRegressor: numeric prediction from the nearest labeled examples
- RadiusNeighborsClassifier: classification from all examples inside a distance radius
- RadiusNeighborsRegressor: numeric prediction from all examples inside a distance radius
- NearestCentroid: fast classification by closest class centroid
- Ball Tree: faster repeated nearest-neighbor search in metric spaces
- KD Tree: efficient spatial indexing for repeated lookup on suitable data
Detailed module guide
How to choose an algorithm
- Use k-Nearest Neighbors when you want a direct non-parametric baseline.
- Use KNeighborsRegressor or RadiusNeighborsRegressor for continuous targets.
- Use RadiusNeighborsClassifier when a fixed physical or semantic radius matters more than fixed
k. - Use NearestCentroid for a very fast linear-time classifier baseline.
- Use Ball Tree or KD Tree when repeated query speed matters.
JavaScript deployment notes
- Scale numeric features before distance-based prediction or lookup.
- Build tree-based indexes once and reuse them when the reference dataset changes slowly.
- Keep nearest-neighbor search close to the application layer when retrieval results immediately drive UI or product logic.
XGBoost Regressor
Learn what XGBoost Regressor does, when to use it, and how to run XGBoostRegressor in JavaScript or TypeScript with @kanaries/ml for browser and Node.js applications.
k-Nearest Neighbors
Learn what k-Nearest Neighbors does, when to use it, and how to run KNearestNeighbors in JavaScript or TypeScript with @kanaries/ml for browser and Node.js applications.