---
title: "Clustering in JavaScript with @kanaries/ml"
description: "Explore clustering algorithms in JavaScript and TypeScript with @kanaries/ml, including K-Means, DBSCAN, HDBSCAN, Mean Shift, OPTICS, and clustering utilities."
canonical_url: "https://ml.kanaries.net/docs/apis/clusters"
markdown_url: "https://ml.kanaries.net/docs/apis/clusters/index.html.md"
---
# Clustering in JavaScript

## 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

> The HTML version includes an interactive comparison of clustering algorithms. The agent-readable algorithm links and selection guidance continue below. [Open the HTML page](https://ml.kanaries.net/docs/apis/clusters).

- [K-Means](/docs/apis/clusters/kmeans.md): centroid-based clustering when cluster count is known or estimated
- [DBScan](/docs/apis/clusters/dbscan.md): density-based clustering with explicit radius and noise labels
- [HDBSCAN](/docs/apis/clusters/hdbscan.md): density-based clustering when cluster count is unknown and noise matters
- [Mean Shift](/docs/apis/clusters/meanShift.md): mode-seeking clustering without predefining cluster count
- [OPTICS](/docs/apis/clusters/optics.md): density-based clustering for irregular cluster shapes
- [k-means++ Initialization](/docs/apis/clusters/kmeansPlusPlus.md): improved centroid seeding for K-Means pipelines
- [Advanced Clustering](/docs/apis/clusters/advancedClustering.md): Birch, Affinity Propagation, and Bisecting K-Means

## Detailed module guide

### How to choose an algorithm

1. Start with [K-Means](/docs/apis/clusters/kmeans.md) when clusters are roughly compact and you can choose `k`.
2. Use [DBScan](/docs/apis/clusters/dbscan.md) when you can choose a meaningful neighborhood radius.
3. Use [HDBSCAN](/docs/apis/clusters/hdbscan.md) or [OPTICS](/docs/apis/clusters/optics.md) when clusters are irregular or noisy.
4. Use [Mean Shift](/docs/apis/clusters/meanShift.md) 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.
