Skip to content

Commit

Permalink
do eslint fix and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyfok committed Feb 24, 2018
1 parent 2b8b994 commit e33b080
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 -----');
Expand Down
2 changes: 1 addition & 1 deletion lib/KMeansEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
16 changes: 12 additions & 4 deletions test/KMeansEngine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const _ = require('underscore');
const chai = require('chai');

const should = chai.should();
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit e33b080

Please sign in to comment.