@kanaries/ml
API Reference/Clustering

Clustering

Explore clustering algorithms in JavaScript and TypeScript with @kanaries/ml, including K-Means, DBSCAN, HDBSCAN, Mean Shift, OPTICS, and clustering utilities.

Module overview

The Clusters module groups unlabeled data into meaningful structure. It is useful for segmentation, exploratory analysis, anomaly-adjacent pattern discovery, and product workflows where you want to organize data before labels exist.

This module is a strong fit when:

  • you need customer, product, session, or event segmentation
  • you want to discover structure in embeddings or tabular features
  • you need to compare centroid-based clustering against density-based clustering

JavaScript implementation

@kanaries/ml provides clustering algorithms in JavaScript and TypeScript so teams can run segmentation logic directly in browser analysis tools and Node.js services. This is especially useful for interactive data products where clustering results need to feed straight into charts, filters, or downstream product actions without a separate Python backend.

If someone searches for "clustering in JavaScript", "K-Means in JavaScript", or "density clustering in TypeScript", this module is the right entry point.

Quick navigation

Clustering Algorithms Comparison

Compare different clustering algorithms on classic datasets. The datasets shown here are commonly used to demonstrate the strengths and weaknesses of different clustering approaches.

Algorithm Comparison Notes

K-Means

Assumes circular clusters and struggles with non-convex shapes. Works well when clusters are spherical and similar in size.

DBSCAN

Excellent for non-convex shapes and handles noise well. Requires tuning of epsilon and min_samples parameters.

OPTICS

Extension of DBSCAN that works with varying densities. Creates a reachability plot for cluster extraction.

Mean Shift

Finds clusters by shifting points towards modes of the data distribution. Automatically determines cluster count.

HDBSCAN

Hierarchical extension of DBSCAN that works well with varying densities and hierarchical cluster structures.

  • K-Means: centroid-based clustering when cluster count is known or estimated
  • DBScan: density-based clustering with explicit radius and noise labels
  • HDBSCAN: density-based clustering when cluster count is unknown and noise matters
  • Mean Shift: mode-seeking clustering without predefining cluster count
  • OPTICS: density-based clustering for irregular cluster shapes
  • k-means++ Initialization: improved centroid seeding for K-Means pipelines

Detailed module guide

How to choose an algorithm

  1. Start with K-Means when clusters are roughly compact and you can choose k.
  2. Use DBScan when you can choose a meaningful neighborhood radius.
  3. Use HDBSCAN or OPTICS when clusters are irregular or noisy.
  4. Use Mean Shift when you want clustering around dense modes without committing to a cluster count up front.

JavaScript deployment notes

  • Normalize features before clustering so distance behaves consistently.
  • For browser tools, run larger clustering jobs outside the main thread when responsiveness matters.
  • Benchmark both cluster quality and runtime cost because the best algorithm depends heavily on shape, scale, and noise.