UMAP-JS Dimension Reduction

1.4.0 · active · verified Tue Apr 21

UMAP-JS is a JavaScript implementation of the Uniform Manifold Approximation and Projection (UMAP) algorithm, a popular dimension reduction technique for both visualization and general non-linear dimension reduction. Currently stable at version 1.4.0, the library offers synchronous, asynchronous, and step-by-step fitting methods, allowing for flexible integration into various application contexts. Key differentiators from the original Python implementation include the use of a random embedding to seed the optimization step, rather than a spectral embedding, which yields comparable results for smaller datasets but avoids complex eigenvalue/eigenvector computations in JavaScript. The library also lacks specialized functionality for angular distances or sparse data representations. Releases appear to follow an irregular schedule, with the last publish about two years ago, but the project is maintained by PAIR-code, indicating ongoing support.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing UMAP, generating sample data, performing synchronous dimension reduction, and transforming new data points.

import { UMAP } from 'umap-js';

// Generate some sample high-dimensional data
const numSamples = 1000;
const numDimensions = 10;
const data: number[][] = [];
for (let i = 0; i < numSamples; i++) {
  const sample: number[] = [];
  for (let j = 0; j < numDimensions; j++) {
    sample.push(Math.random() * 100);
  }
  data.push(sample);
}

// Initialize UMAP with custom parameters
const umap = new UMAP({
  nComponents: 2,   // Reduce to 2 dimensions for visualization
  nNeighbors: 10,   // Number of neighbors to consider
  minDist: 0.05,    // Controls how tightly points are clustered together
  spread: 1.0       // Controls the overall scale of the embedding
});

console.log('Starting UMAP fitting...');
// Perform synchronous fitting
const embedding = umap.fit(data);

console.log('UMAP fitting complete. First 5 embedded points:');
console.log(embedding.slice(0, 5));

// Example of transforming new data after fitting
const additionalData = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]];
const transformedPoints = umap.transform(additionalData);
console.log('Transformed additional points:');
console.log(transformedPoints);

view raw JSON →