diff --git a/README.md b/README.md index e458551..9fae4e9 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ Most of the other implementations available in npm take a N x M matrix (a 2d arr ## What's New +#### 1.4.0 + +Support options to provide initial centroids. See details in [pull request](https://github.com/stanleyfok/kmeans-engine/pull/2) + #### 1.3.0 Update to newer version of [vector-object](https://www.npmjs.com/package/vector-object) @@ -62,6 +66,7 @@ const engineers = [ // accepted options: // k: number of clusters // maxIterations (optional): max number of iterations +// initialCentroids (optional): an array of initial centroids in length of k // debug (optional): show debug message in console or not, default is false kmeans.clusterize(engineers, { k: 4, maxIterations: 5, debug: true }, (err, res) => { console.log('----- Results -----'); diff --git a/lib/KMeansEngine.js b/lib/KMeansEngine.js index 1667ae8..e59684e 100644 --- a/lib/KMeansEngine.js +++ b/lib/KMeansEngine.js @@ -45,7 +45,7 @@ class KMeansEngine { if (this.initialCentroids !== undefined) { if (!_.isArray(this.initialCentroids) || (this.initialCentroids.length !== this.k)) { - throw new Error('Initial centroids should be array of length equal to cluster size') + throw new Error('Initial centroids should be array of length equal to cluster size'); } else { // Initial centroids should be array of objects for (let i = 0; i < this.initialCentroids.length; i += 1) { diff --git a/package.json b/package.json index 86d0bbb..61a11ce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kmeans-engine", - "version": "1.3.1", + "version": "1.4.0", "description": "Scalable kmeans clustering algorithm in js using objects as input vectors, tailor made for sparse matrix", "homepage": "https://github.com/stanleyfok/kmeans-engine", "repository": { diff --git a/test/KMeansEngine.js b/test/KMeansEngine.js index a2b284c..fe021b9 100644 --- a/test/KMeansEngine.js +++ b/test/KMeansEngine.js @@ -1,4 +1,3 @@ -const _ = require('underscore'); const chai = require('chai'); const should = chai.should(); @@ -124,8 +123,13 @@ describe('KMeansEngine', () => { }); it('should return same result given same initial centroids', (done) => { - const initialCentroids = [{x: 1, y: 0}, {x: 0, y: 1}, {x: 1, y: 1}]; - kmeans.clusterize(set2, { k: 3, maxIterations: 1, initialCentroids: initialCentroids}, (err1, res1) => { + const initialCentroids = [{ x: 1, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 1 }]; + + kmeans.clusterize(set2, { + k: 3, + maxIterations: 1, + initialCentroids, + }, (err1, res1) => { should.not.exist(err1); res1.should.to.have.property('iterations'); res1.should.to.have.property('clusters'); @@ -136,7 +140,11 @@ describe('KMeansEngine', () => { cluster1.should.to.have.property('vectorIds'); }); - kmeans.clusterize(set2, { k: 3, maxIterations: 1, initialCentroids: initialCentroids }, (err2, res2) => { + kmeans.clusterize(set2, { + k: 3, + maxIterations: 1, + initialCentroids, + }, (err2, res2) => { should.not.exist(err2); res2.should.to.have.property('iterations'); res2.should.to.have.property('clusters');