Comparing version 0.9.5 to 0.9.6
"use strict"; | ||
(function($) { | ||
var skmeans = require("./main.js"); | ||
$.skmeans = skmeans; | ||
})(window); | ||
(function() { | ||
var root = this | ||
var previous_skmeans = root.skmeans; | ||
var skmeans = require('./main.js'); | ||
if( typeof exports !== 'undefined' ) { | ||
if( typeof module !== 'undefined' && module.exports ) { | ||
exports = module.exports = skmeans; | ||
} | ||
exports.skmeans = skmeans; | ||
} | ||
if(typeof window !== 'undefined') { | ||
window.skmeans = skmeans; | ||
} | ||
}).call(this); |
@@ -17,2 +17,14 @@ module.exports = { | ||
mandist(v1,v2,sqrt) { | ||
var len = v1.length; | ||
var sum = 0; | ||
for(let i=0;i<len;i++) { | ||
sum += Math.abs((v1[i]||0) - (v2[i]||0)); | ||
} | ||
// Square root not really needed | ||
return sqrt? Math.sqrt(sum) : sum; | ||
}, | ||
/** | ||
@@ -19,0 +31,0 @@ * Unidimensional distance |
@@ -12,3 +12,3 @@ module.exports = function(grunt) { | ||
files: { | ||
'dist/skmeans.js': ['browser.js'] | ||
'dist/browser/skmeans.js': ['browser.js'] | ||
} | ||
@@ -23,5 +23,13 @@ } | ||
dist: { | ||
files: { | ||
'dist/skmeans.js': 'dist/skmeans.js' | ||
} | ||
files: [ | ||
{ | ||
expand: true, | ||
src: ['*.js'], | ||
dest: 'dist/node', | ||
ext: '.js' | ||
}, | ||
{ | ||
'dist/browser/skmeans.js': ['dist/browser/skmeans.js'], | ||
} | ||
] | ||
} | ||
@@ -35,7 +43,7 @@ }, | ||
files: { | ||
'dist/skmeans.min.js' : ['dist/skmeans.js'] | ||
'dist/browser/skmeans.min.js' : ['dist/browser/skmeans.js'] | ||
} | ||
} | ||
}, | ||
clean: ['dist/*.js','dist/*.map'] | ||
clean: ['dist/browser/*.js','dist/browser/*.map'] | ||
}); | ||
@@ -42,0 +50,0 @@ |
@@ -0,0 +0,0 @@ const |
29
main.js
@@ -7,2 +7,3 @@ /*jshint esversion: 6 */ | ||
eudist = Distance.eudist, | ||
mandist = Distance.mandist, | ||
dist = Distance.dist, | ||
@@ -27,2 +28,3 @@ kmrand = ClusterInit.kmrand, | ||
var len = data.length, vlen = data[0].length, multi = vlen>0; | ||
var count = []; | ||
@@ -50,2 +52,5 @@ if(!initial) { | ||
do { | ||
// Reset k count | ||
init(k,0,count); | ||
// For each value in data, find the nearest centroid | ||
@@ -62,10 +67,10 @@ for(let i=0;i<len;i++) { | ||
} | ||
idxs[i] = idx; | ||
idxs[i] = idx; // Index of the selected centroid for that value | ||
count[idx]++; // Number of values for this centroid | ||
} | ||
// Recalculate centroids | ||
var count = [], sum = [], old = [], dif = 0; | ||
var sum = [], old = [], dif = 0; | ||
for(let j=0;j<k;j++) { | ||
// Multidimensional or unidimensional | ||
count[j] = 0; | ||
sum[j] = multi? init(vlen,0,sum[j]) : 0; | ||
@@ -81,5 +86,5 @@ old[j] = ks[j]; | ||
for(let i=0;i<len;i++) { | ||
let idx = idxs[i], // Centroid for that item | ||
vsum = sum[idx], // Sum values for this centroid | ||
vect = data[i]; // Current vector | ||
let idx = idxs[i], // Centroid for that item | ||
vsum = sum[idx], // Sum values for this centroid | ||
vect = data[i]; // Current vector | ||
@@ -90,3 +95,2 @@ // Accumulate value on the centroid for current vector | ||
} | ||
count[idx]++; // Number of values for this centroid | ||
} | ||
@@ -96,12 +100,12 @@ // Calculate the average for each centroid | ||
for(let j=0;j<k;j++) { | ||
let ksj = ks[j], // Current centroid | ||
sumj = sum[j], // Accumulated centroid values | ||
oldj = old[j], // Old centroid value | ||
cj = count[j]; // Number of elements for this centroid | ||
let ksj = ks[j], // Current centroid | ||
sumj = sum[j], // Accumulated centroid values | ||
oldj = old[j], // Old centroid value | ||
cj = count[j]; // Number of elements for this centroid | ||
// New average | ||
for(let h=0;h<vlen;h++) { | ||
//ksj[h] = (sumj[h]+oldj[h])/(cj+1) || 0; // New centroid | ||
ksj[h] = (sumj[h])/(cj) || 0; // New centroid | ||
} | ||
// Find if centroids have moved | ||
@@ -124,3 +128,2 @@ if(conv) { | ||
sum[idx] += data[i]; | ||
count[idx]++; | ||
} | ||
@@ -127,0 +130,0 @@ // Calculate the average for each centroid |
{ | ||
"name": "skmeans", | ||
"version": "0.9.5", | ||
"version": "0.9.6", | ||
"description": "Super fast simple k-means and k-means++ clustering for unidimiensional and multidimensional data. Works in node and browser", | ||
@@ -8,3 +8,3 @@ "author": "David Gómez Matarrodona <solzimer@gmail.com>", | ||
"email": "solzimer@gmail.com", | ||
"main": "main.js", | ||
"main": "dist/node/main.js", | ||
"license": "MIT", | ||
@@ -11,0 +11,0 @@ "repository": "solzimer/skmeans", |
@@ -0,0 +0,0 @@ # skmeans |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
@@ -0,0 +0,0 @@ const skmeans = require("../main.js"); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
31
92085
1103