Density Based Clustering Algorithms
This JavaScript package provides implementations of popular cluster analysis algorithms, including DBSCAN (Density-Based Spatial Clustering of Applications with Noise), OPTICS (Ordering Points To Identify the Clustering Structure), and K-Means. Currently at version 1.3.0, the package appears to be in a maintenance state, primarily supporting CommonJS environments as indicated by its examples and `bower` installation instructions, which suggest it hasn't fully adopted modern ESM practices or aggressive development cycles. A key differentiator is its direct, unopinionated implementation of these core algorithms for data mining and statistical analysis. While DBSCAN and OPTICS are true density-based methods, K-Means is included for broader utility, though it's important to note it does not rely on density for clustering. The library focuses on providing these fundamental tools for data scientists and developers working with clustering tasks.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ECMAScript Module (ESM) context without proper transpilation or configuration.fixEnsure your Node.js project is configured for CommonJS (e.g., `"type": "commonjs"` in `package.json` or by using `.cjs` extension for files). For browser environments, use a bundler. -
TypeError: clustering is not a constructor
cause Incorrectly trying to instantiate `new clustering()` instead of `new clustering.DBSCAN()` (or OPTICS/KMEANS).fixAccess the specific clustering algorithm class (e.g., `DBSCAN`, `OPTICS`, `KMEANS`) as a property of the `clustering` object before instantiating it, like `new clustering.DBSCAN()`. -
Error: Invalid parameters for algorithm run.
cause Providing an incorrect number or type of arguments to the `run` method of DBSCAN, OPTICS, or KMEANS.fixRefer to the documentation for the specific algorithm's `run` method. DBSCAN and OPTICS typically expect `(dataset, neighborhoodRadius, minPoints)`, while KMEANS expects `(dataset, numberOfClusters)`.
Warnings
- gotcha When using the OPTICS algorithm, the initial `clusters` array returned by `run()` is often nearly identical to DBSCAN's output. To fully leverage OPTICS for varying densities and hierarchical structures, you *must* analyze the `reachability plot` generated by `optics.getReachabilityPlot()`.
- gotcha The K-MEANS algorithm is included in this library but it is *not* a density-based clustering method. Its inclusion is for completeness, but users should be aware that it operates on different principles (centroid-based partitioning) compared to DBSCAN and OPTICS.
- deprecated The documentation mentions `bower install density-clustering` for browser usage. Bower is largely deprecated and not recommended for modern web development. Using `npm` with a bundler (like Webpack or Rollup) is the current standard for browser integration.
Install
-
npm install density-clustering -
yarn add density-clustering -
pnpm add density-clustering
Imports
- DBSCAN
import { DBSCAN } from 'density-clustering';const clustering = require('density-clustering'); const dbscan = new clustering.DBSCAN(); - OPTICS
import OPTICS from 'density-clustering/optics';
const clustering = require('density-clustering'); const optics = new clustering.OPTICS(); - KMEANS
const KMEANS = require('density-clustering').KMEANS;const clustering = require('density-clustering'); const kmeans = new clustering.KMEANS();
Quickstart
const dataset = [
[1,1],[0,1],[1,0],
[10,10],[10,13],[13,13],
[54,54],[55,55],[89,89],[57,55]
];
const clustering = require('density-clustering');
const dbscan = new clustering.DBSCAN();
// parameters: 5 - neighborhood radius, 2 - number of points in neighborhood to form a cluster
const clusters = dbscan.run(dataset, 5, 2);
console.log('Clusters:', clusters);
console.log('Noise points:', dbscan.noise);
/* Expected Output:
Clusters: [
[0,1,2],
[3,4,5],
[6,7,9]
]
Noise points: [ 8 ]
*/