@enigmaoffline/kmeans-clustering
v1.0.2
Published
K-Means Clustering for Unsupervised Learning
Downloads
5
Maintainers
Readme
K-Means Clustering
K-Means Clustering algorithm with integrated upper limit for iteration count
Install
Installing with npm
npm install --save @enigmaoffline/kmeans-clustering
Usage
const Cluster = require("@enigmaoffline/kmeans-clustering");
const dataPoints = [
[2, 10],
[2, 5],
[8, 4],
[5, 8],
[7, 5],
[6, 4],
[1, 2],
[4, 9],
];
const cluster = new Cluster(3, dataPoints);
cluster.setDistanceMethod(Cluster.DIST.MANHATTAN);
cluster.setDecimalPoints(2);
cluster.setLimit(300);
cluster.getClusters().then((res) => console.log(JSON.stringify(res)));
/*
res = {
"centroids": [[3.67, 9], [1.5, 3.5], [7, 4.33]],
"clusters": [[[2, 10], [5, 8], [4, 9]], [[2, 5], [1, 2]], [[8, 4], [7, 5], [6, 4]]]
}
*/
| Function | Functionality | | :------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | constructor() | takes two parameters1) number of clusters (k)2) datapoints | | setDistanceMethod() | sets the distance calculation method, either euclideandistance, or manhattan distance. | | setDecimalPoints() | sets the number of decimal points centroids are roundedto on return. | | setLimit() | sets the upper limit of iterations the algorithm will runbefore it quits even though thevalues have yet to converge. | | getClusters() | async function that groups datapoints into k clustersterminates on either1) all values converge and no changes happen2) iteration count exceeds upper limit |
LICENSE - MIT - Lo Chung Tin