Skip to content

Commit

Permalink
expose the class creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Philmod committed Oct 22, 2012
1 parent 68bdadf commit ae5fcc1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -25,11 +25,15 @@ for (var i = 0 ; i < data.length ; i++)
vectors[i] = [ data[i]['size'] , data[i]['revenue']];

var kmeans = require('kmeans');
new kmeans.clusterize(vectors, {k: 4}, function(err,res) {
kmeans.clusterize(vectors, {k: 4}, function(err,res) {
if (err) console.error(err);
else console.log('%o',res);
});
```
## Intputs
- vectors is a nXm array (n [lines] : number of points, m [columns] : number of dimensions)
- options object
- k : number of clusters

## Outputs
An array of objects (one for each cluster) with the following properties:
Expand Down
8 changes: 5 additions & 3 deletions lib/kmeans.js
Expand Up @@ -32,10 +32,12 @@ exports.version = '0.0.1';


/**
* Expose `clusterize`.
* Expose 'clusterize' which creates a new 'clusterize' class.
*/

exports.clusterize = clusterize;
exports.clusterize = function(vector, options, callback) {
return new clusterize(vector, options, callback);
};


/**
Expand Down Expand Up @@ -212,4 +214,4 @@ Group.prototype.distanceObjects = function(self) {
return this;
};


exports._class = clusterize;
6 changes: 5 additions & 1 deletion package.json
@@ -1,9 +1,13 @@
{
"name": "node-kmeans"
, "version": "0.0.1"
, "description": "Node.js implementation of the clustering algorithm k-means"
, "description": "Node.js asynchronous implementation of the clustering algorithm k-means"
, "keywords": ["k-means", "clustering"]
, "author": "Philmod <philippe.modard@gmail.com>"
, "homepage": "http://github.com/philmod/node-kmeans"
, "engines": {
"node": ">= v0.6.0"
}
, "dependencies": {
"underscore": "1.4.2"
}
Expand Down
23 changes: 21 additions & 2 deletions test/index.js
Expand Up @@ -9,7 +9,7 @@ var kmeans = require('../')
/**
* Data (expressly very separated)
*/
var data = [
var data2D = [
[-10, 5],
[-11, 6],
[-10.5, 6.5],
Expand All @@ -28,8 +28,27 @@ var data = [
[41,-200],
[40.25,-198]
];
var data3D = [
[-10, 5, 100],
[-11, 6, 101],
[-10.5, 6.5, 102],
[-9.5, 5.5, 103],
[-9.75, 6.25, 104],

new kmeans.clusterize(data, {k: 3}, function(err,res) {
[200, 12, -11],
[205, 11.8, -10.8],
[202, 11.5, -10],
[208, 11, -12],
[198, 11.15, -11],

[40, -200, 568],
[38, -190, 578],
[39.5, -205, 556],
[41, -200, 561],
[40.25, -198, 562]
];

kmeans.clusterize(data3D, {k: 3}, function(err,res) {
if (err) console.error(err);
else console.log('%o',res);
});

0 comments on commit ae5fcc1

Please sign in to comment.