JSPM

  • Created
  • Published
  • Downloads 891476
  • Score
    100M100P100Q185282F
  • License MIT

Super fast simple kmeans clustering for unidimiensional data

Package Exports

  • skmeans

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (skmeans) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

skmeans

Super fast simple k-means implementation for unidimiensional and multidimensional data. Works on nodejs and browser.

Installation

npm install skmeans

Usage

NodeJS

const skmeans = require("skmeans");

var data = [1,12,13,4,25,21,22,3,14,5,11,2,23,24,15];
var res = skmeans(data,3);

Browser

<!doctype html>
<html>
<head>
    <script src="skmeans.js"></script>
</head>
<body>
    <script>
        var data = [1,12,13,4,25,21,22,3,14,5,11,2,23,24,15];
        var res = skmeans(data,3);

        console.log(res);
    </script>
</body>
</html>

Results

{
    it: 2,
    k: 3,
    idxs: [ 2, 0, 0, 2, 1, 1, 1, 2, 0, 2, 0, 2, 1, 1, 0 ],
    centroids: [ 13, 23, 3 ]
}

API

skmeans(data,k,[centroids],[iterations])

Calculates unidimiensional k-means clustering on data. Parameters are:

  • data Unidimiensional or multidimensional array of values to be clustered. for unidimiensional data, takes the form of a simple array [1,2,3.....,n]. For multidimensional data, takes a NxM array [[1,2],[2,3]....[n,m]]
  • k Number of clusters
  • centroids Optional. Initial centroid values. If not provided, the algorith will try to choose an apropiate ones.
  • iterations Optional. Maximum number of iterations. If not provided, it will be set to 10000.

The function will return an object with the following data:

  • it The number of iterations performed until the algorithm has converged
  • k The cluster size
  • centroids The value for each centroid of the cluster
  • idxs The index to the centroid corresponding to each value of the data array