k-means-pp

4.0.0 • Public • Published

k-means-pp.js

A JS/TS implementation of the k-means and k-means++ clustering algorithm.

  • Implements both k-means and k-means++ algorithms
  • Supports multi-dimensional data points
  • Works in any JavaScript environment, including browsers, Node.js, Deno and more

Installation

deno
deno add @ppz/k-means-pp
npm
npm install k-means-pp

Usage

basic
// import { k_means_pp, k_means } from '@ppz/k-means-pp' // install from deno
import { k_means_pp, k_means } from 'k-means-pp' // install from npm

const dimension = 3
const points = [
  [1,2,3],
  [0, 270, 103],
  [3,4,5],
  [0,0,0],
  [100, 200, 1],
  [0, 310, 120],
  [10, 320, 90],
  [100, 201, 3],
  [0, 300, 100],
  [1000, 2000, 1],
]

const range = calc_range(dimension, points)

const [clusters, count] = k_means_pp({
  dimension,
  points,
  k: 8,
  range,
})
console.log(clusters.map(c => c.mean), count)

const [clusters_pp, count_pp] = k_means({
  dimension,
  points,
  k: 8,
  range,
})
console.log(clusters_pp.map(c => c.mean), count_pp)
customize distance quantification
import { k_means_pp } from 'k-means-pp' // install from npm

const dimension = 3
const points = [
  [1,2,3],
  [0, 270, 103],
  [3,4,5],
  [0,0,0],
  [100, 200, 1],
  // ...
]

const [clusters, count] = k_means_pp({
  dimension,
  points,
  k: 3,
  range,
  quantify(dimension, a_point, b_point) {
    let squared = 0
    for(let i=0; i<dimension; i++)
      squared += (a_point[i] - b_point[i]) **2
    return squared
  },
})

API

DEV

test
deno test
build for npm
deno task npm
cd npm
npm publish --access public

Package Sidebar

Install

npm i k-means-pp

Weekly Downloads

9

Version

4.0.0

License

Unlicense

Unpacked Size

15.6 kB

Total Files

19

Last publish

Collaborators

  • ppzreboot