4.0.0 • Published 5 months ago

k-means-pp v4.0.0

Weekly downloads
-
License
Unlicense
Repository
github
Last release
5 months ago

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
3.2.0

5 months ago

3.1.0

5 months ago

3.0.1

5 months ago

4.0.0

5 months ago

2.3.1

10 months ago

2.2.1

10 months ago

2.2.0

10 months ago

2.1.0

10 months ago

2.0.1

10 months ago

1.0.0

11 months ago