https://github.com/tmcw/k-means
k-means clustering
https://github.com/tmcw/k-means
Last synced: over 1 year ago
JSON representation
k-means clustering
- Host: GitHub
- URL: https://github.com/tmcw/k-means
- Owner: tmcw
- License: bsd-2-clause
- Created: 2012-08-03T14:20:36.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2017-04-10T21:08:16.000Z (over 9 years ago)
- Last Synced: 2025-03-16T01:55:49.966Z (over 1 year ago)
- Language: JavaScript
- Homepage: http://macwright.org/2012/09/16/k-means.html
- Size: 773 KB
- Stars: 21
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## k-means-cluster
[k-means clustering](http://en.wikipedia.org/wiki/K-means)
in configurable dimensions, in-browser and with node.js.
In order to support [learning about the algorithm](http://macwright.org/2012/09/16/k-means.html),
this library is split into very small bits, so you can use each
step separately.
```javascript
var kmeans = require('k-means-cluster');
var d = [1, 2, 3];
// take a random sample of the array d
var s = kmeans.sample(d, 1);
console.log(s);
// [1]
// Get the euclidean distance between two points represented as
// arrays
console.log(kmeans.dist([0, 0], [0, 1]), 1);
// 1
```
In order to support a variety of data, accessors are used in functions like
`means_clusters(x, means, distance, val)` (though with good defaults -
distance defaults to euclidean, val defaults to `function(x) { return x; }`).
```javascript
var c = kmeans.means_clusters([3], [3, 4])
console.log(c.length);
// 1
```
## Usage
npm install --save k-means-cluster
## Testing
npm test
## License is [WTFPL](http://sam.zoy.org/wtfpl/)