@kanaries/ml
API Reference/Neighbors

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

Detailed module guide

How to choose an algorithm

  1. Use k-Nearest Neighbors when you want a direct non-parametric baseline.
  2. Use KNeighborsRegressor or RadiusNeighborsRegressor for continuous targets.
  3. Use RadiusNeighborsClassifier when a fixed physical or semantic radius matters more than fixed k.
  4. Use NearestCentroid for a very fast linear-time classifier baseline.
  5. 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.