Comparing version 0.5.1 to 0.5.2
@@ -25,2 +25,4 @@ var distances = require("./distance"); | ||
function kmeans(points, k, distance, snapshotPeriod, snapshotCb) { | ||
k = k || Math.max(2, Math.ceil(Math.sqrt(points.length / 2))); | ||
distance = distance || "euclidean"; | ||
@@ -30,3 +32,3 @@ if (typeof distance == "string") { | ||
} | ||
var centroids = randomCentroids(points, k); | ||
@@ -36,3 +38,3 @@ var assignment = new Array(points.length); | ||
var iterations = 0; | ||
var iterations = 0; | ||
var movement = true; | ||
@@ -49,7 +51,7 @@ while (movement) { | ||
var assigned = []; | ||
assignment.forEach(function(centroid, index) { | ||
if (centroid == j) { | ||
assigned.push(points[index]); | ||
for (var i = 0; i < assignment.length; i++) { | ||
if (assignment[i] == j) { | ||
assigned.push(points[i]); | ||
} | ||
}); | ||
} | ||
@@ -68,3 +70,3 @@ if (!assigned.length) { | ||
newCentroid[g] = sum / assigned.length; | ||
if (newCentroid[g] != centroid[g]) { | ||
@@ -77,3 +79,3 @@ movement = true; | ||
} | ||
if (snapshotCb && (iterations++ % snapshotPeriod == 0)) { | ||
@@ -80,0 +82,0 @@ snapshotCb(clusters); |
{ | ||
"name": "clusterfck", | ||
"description": "K-means and hierarchical clustering", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"author": "Heather Arthur <fayearthur@gmail.com>", | ||
@@ -14,3 +14,3 @@ "repository": { | ||
"dependencies" : { | ||
}, | ||
@@ -17,0 +17,0 @@ "devDependencies": { |
@@ -11,3 +11,3 @@ # Clusterfck | ||
``` | ||
Or grab the [browser file] (http://harthur.github.com/clusterfck/demos/colors/clusterfck.js) | ||
Or grab the [browser file](http://harthur.github.com/clusterfck/demos/colors/clusterfck.js) | ||
@@ -24,14 +24,18 @@ | ||
[250, 255, 253], | ||
[100, 54, 255] | ||
[0, 30, 70], | ||
[200, 0, 23], | ||
[100, 54, 100], | ||
[255, 13, 8] | ||
]; | ||
var clusters = clusterfck.kmeans(colors, 2); | ||
var clusters = clusterfck.kmeans(colors, 3); | ||
``` | ||
The second argument to `kmeans` is the number of clusters you want. It returns an array of the clusters, for this example: | ||
The second argument to `kmeans` is the number of clusters you want (default is `Math.sqrt(n/2)` where `n` is the number of vectors). It returns an array of the clusters, for this example: | ||
```javascript | ||
[ | ||
[[20, 20, 80], [22, 22, 90], [100, 54, 255]] | ||
[[250, 255, 253]], | ||
[[200,0,23], [255,13,8]], | ||
[[20,20,80], [22,22,90], [0,30,70], [100,54,100]], | ||
[[250,255,253]] | ||
] | ||
@@ -38,0 +42,0 @@ ``` |
@@ -7,7 +7,10 @@ var clusterfck = require("./lib/clusterfck"); | ||
[250, 255, 253], | ||
[100, 54, 255] | ||
[0, 30, 70], | ||
[200, 0, 23], | ||
[100, 54, 100], | ||
[255, 13, 8], | ||
]; | ||
var clusters = clusterfck.hcluster(colors); | ||
var clusters = clusterfck.kmeans(colors, 3); | ||
console.log(JSON.stringify(clusters, null, 3)); | ||
console.log(JSON.stringify(clusters, null)); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
164472
2399
87