# skmeans

> Super fast simple k-means and k-means++ clustering for unidimiensional and multidimensional data. Works in node and browser

Latest version **0.11.3** (published 2020-01-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install skmeans
pnpm add skmeans
yarn add skmeans
bun add skmeans
```

## Health

**Score 18/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.11.3 |
| Published | 2020-01-29 |
| First published | 2017-06-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/skmeans) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 105.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | David Gómez Matarrodona |
| Maintainers | solzimer |
| Keywords | math, k-means, k-means++, kmeans++, kmeans, simple, cluster, fast, unidimiensional, multidimensional |

## Links

- npm: https://www.npmjs.com/package/skmeans
- Repository: https://github.com/solzimer/skmeans
- Homepage: https://github.com/solzimer/skmeans#readme
- Issues: https://github.com/solzimer/skmeans/issues
- npm.io page: https://npm.io/package/skmeans

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.11.3 (latest) — 2020-01-29
- 0.11.2 — 2019-09-13
- 0.11.1 — 2019-09-13
- 0.11.0 — 2019-08-27
- 0.10.2 — 2019-05-24
- 0.10.1 — 2019-04-13
- 0.10.0 — 2019-03-02
- 0.9.8 — 2018-07-14
- 0.9.7 — 2017-08-03
- 0.9.6 — 2017-08-03
- 0.9.5 — 2017-07-21
- 0.9.4 — 2017-07-20
- 0.9.3 — 2017-07-19
- 0.9.2 — 2017-07-18
- 0.9.1 — 2017-07-18
- … 17 more at https://npm.io/package/skmeans/versions

## README

# skmeans

Super fast simple k-means and [k-means++](https://en.wikipedia.org/wiki/K-means%2B%2B) implementation for unidimiensional and multidimensional data. Works on nodejs and browser.

## Installation
```
npm install skmeans
```

## Usage
### NodeJS
```javascript
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
```html
<!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
```javascript
{
	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 and multidimensional 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. Alternative values can be:
  * **"kmrand"** Cluster initialization will be random, but with extra checking, so there will no be two equal initial centroids.
  * **"kmpp"** The algorythm will use the [k-means++](https://en.wikipedia.org/wiki/K-means%2B%2B) cluster initialization method.
* **iterations** Optional. Maximum number of iterations. If not provided, it will be set to 10000.
* **distance function** Optional. Custom distance function. Takes two points as arguments and returns a scalar number.

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
* **test** Function to test new point membership

## Examples
```javascript
// k-means with 3 clusters. Random initialization
var res = skmeans(data,3);

// k-means with 3 clusters. Initial centroids provided
var res = skmeans(data,3,[1,5,9]);

// k-means with 3 clusters. k-means++ cluster initialization
var res = skmeans(data,3,"kmpp");

// k-means with 3 clusters. Random initialization. 10 max iterations
var res = skmeans(data,3,null,10);

// k-means with 3 clusters. Custom distance function
var res = skmeans(data,3,null,null,(x1,x2)=>Math.abs(x1-x2));

// Test new point
var res = skmeans(data,3,null,10);
res.test(6);

// Test new point with custom distance
var res = skmeans(data,3,null,10);
res.test(6,(x1,x2)=>Math.abs(x1-x2));
```

---
_Source: https://npm.io/package/skmeans · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
