Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

skmeans

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

skmeans - npm Package Compare versions

Comparing version 0.3.1 to 0.4.0

dist/skmeans.js.map

244

dist/skmeans.js

@@ -1,134 +0,156 @@

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";
(function($) {
var skmeans = require("./main.js");
$.skmeans = skmeans;
})(window);
(function e(t, n, r) {
function s(o, u) {
if (!n[o]) {
if (!t[o]) {
var a = typeof require == "function" && require;if (!u && a) return a(o, !0);if (i) return i(o, !0);var f = new Error("Cannot find module '" + o + "'");throw f.code = "MODULE_NOT_FOUND", f;
}var l = n[o] = { exports: {} };t[o][0].call(l.exports, function (e) {
var n = t[o][1][e];return s(n ? n : e);
}, l, l.exports, e, t, n, r);
}return n[o].exports;
}var i = typeof require == "function" && require;for (var o = 0; o < r.length; o++) {
s(r[o]);
}return s;
})({ 1: [function (require, module, exports) {
"use strict";
},{"./main.js":2}],2:[function(require,module,exports){
"use strict";
(function ($) {
var skmeans = require("./main.js");
$.skmeans = skmeans;
})(window);
}, { "./main.js": 2 }], 2: [function (require, module, exports) {
/*jshint esversion: 6 */
const MAX = 10000;
var MAX = 10000;
/**
* Euclidean distance
*/
function eudist(v1,v2) {
var len = v1.length;
var sum = 0;
/**
* Euclidean distance
*/
function eudist(v1, v2) {
var len = v1.length;
var sum = 0;
for(let i=0;i<len;i++) {
var d = (v1[i]||0) - (v2[i]||0);
sum += d*d;
for (var i = 0; i < len; i++) {
var d = (v1[i] || 0) - (v2[i] || 0);
sum += d * d;
}
return Math.sqrt(sum);
}
return Math.sqrt(sum);
}
/**
* Manhattan distance
*/
function mandist(v1,v2) {
var len = v1.length;
var sum = 0;
/**
* Manhattan distance
*/
function mandist(v1, v2) {
var len = v1.length;
var sum = 0;
for(let i=0;i<len;i++) {
sum += Math.abs((v1[i]||0) - (v2[i]||0));
for (var i = 0; i < len; i++) {
sum += Math.abs((v1[i] || 0) - (v2[i] || 0));
}
return sum;
}
return sum;
}
/**
* Inits an array with values
*/
function init(len,val) {
var v = [];
for(let i=0;i<len;i++) v.push(val);
return v;
}
/**
* Inits an array with values
*/
function init(len, val) {
var v = [];
for (var i = 0; i < len; i++) {
v.push(val);
}return v;
}
function skmeans(data,k,initial,maxit) {
var ks = [], idxs = [], len = data.length;
var conv = false, it = maxit || MAX;
var multi = data[0].length;
function skmeans(data, k, initial, maxit) {
"use strict";
if(!initial) {
for(let i=0;i<k;i++) {
ks.push(data[Math.floor(Math.random()*len)]);
}
}
else {
ks = initial;
}
var ks = [],
idxs = [],
len = data.length;
var conv = false,
it = maxit || MAX;
var multi = data[0].length;
do {
// For each value in data, find the nearest centroid
for(let i=0;i<len;i++) {
let min = Infinity, idx = 0;
for(let j=0;j<k;j++) {
// Multidimensional or unidimensional
var dist = multi? eudist(data[i],ks[j]) : Math.abs(data[i]-ks[j]);
if(dist<min) {
min = dist;
idx = j;
if (!initial) {
for (var i = 0; i < k; i++) {
ks.push(data[Math.floor(Math.random() * len)]);
}
} else {
ks = initial;
}
idxs[i] = idx;
}
// Recalculate centroids
var count = [], sum = [], old = [], dif = 0;
for(let j=0;j<k;j++) {
// Multidimensional or unidimensional
count[j] = 0;
sum[j] = multi? init(multi,0) : 0;
old[j] = ks[j];
}
do {
// For each value in data, find the nearest centroid
for (var _i = 0; _i < len; _i++) {
var min = Infinity,
idx = 0;
for (var j = 0; j < k; j++) {
// Multidimensional or unidimensional
var dist = multi ? eudist(data[_i], ks[j]) : Math.abs(data[_i] - ks[j]);
if (dist < min) {
min = dist;
idx = j;
}
}
idxs[_i] = idx;
}
// If multidimensional
if(multi) {
// Sum values and count for each centroid
for(let i=0;i<len;i++) {
for(let h=0;h<multi;h++) {
sum[idxs[i]][h] += data[i][h];
// Recalculate centroids
var count = [],
sum = [],
old = [],
dif = 0;
for (var _j = 0; _j < k; _j++) {
// Multidimensional or unidimensional
count[_j] = 0;
sum[_j] = multi ? init(multi, 0) : 0;
old[_j] = ks[_j];
}
count[idxs[i]]++;
}
// Calculate de average for each centroid
// and de distance between old and new centroids
for(let j=0;j<k;j++) {
for(let h=0;h<multi;h++) {
ks[j][h] = sum[j][h]/count[j] || 0;
dif += old[j][h] - ks[j][h];
// If multidimensional
if (multi) {
// Sum values and count for each centroid
for (var _i2 = 0; _i2 < len; _i2++) {
for (var h = 0; h < multi; h++) {
sum[idxs[_i2]][h] += data[_i2][h];
}
count[idxs[_i2]]++;
}
// Calculate de average for each centroid
// and de distance between old and new centroids
for (var _j2 = 0; _j2 < k; _j2++) {
for (var _h = 0; _h < multi; _h++) {
ks[_j2][_h] = sum[_j2][_h] / count[_j2] || 0;
dif += old[_j2][_h] - ks[_j2][_h];
}
}
}
}
}
// If unidimensional
else {
// Sum values and count for each centroid
for(let i=0;i<len;i++) {
sum[idxs[i]] += data[i];
count[idxs[i]]++;
}
// Calculate de average for each centroid
// and de distance between old and new centroids
for(let j=0;j<k;j++) {
ks[j] = sum[j]/count[j] || 0;
dif += old[j] - ks[j];
}
}
// If unidimensional
else {
// Sum values and count for each centroid
for (var _i3 = 0; _i3 < len; _i3++) {
sum[idxs[_i3]] += data[_i3];
count[idxs[_i3]]++;
}
// Calculate de average for each centroid
// and de distance between old and new centroids
for (var _j3 = 0; _j3 < k; _j3++) {
ks[_j3] = sum[_j3] / count[_j3] || 0;
dif += old[_j3] - ks[_j3];
}
}
conv = (dif==0) || (--it<=0);
}while(!conv);
conv = dif === 0 || --it <= 0;
} while (!conv);
return {
it : MAX-it,
k : k,
idxs : idxs,
centroids : ks
}
}
return {
it: MAX - it,
k: k,
idxs: idxs,
centroids: ks
};
}
module.exports = skmeans;
},{}]},{},[1]);
module.exports = skmeans;
}, {}] }, {}, [1]);
//# sourceMappingURL=skmeans.js.map

@@ -1,2 +0,2 @@

"use strict";
/*jshint esversion: 6 */

@@ -42,2 +42,4 @@ const MAX = 10000;

function skmeans(data,k,initial,maxit) {
"use strict";
var ks = [], idxs = [], len = data.length;

@@ -113,3 +115,3 @@ var conv = false, it = maxit || MAX;

conv = (dif==0) || (--it<=0);
conv = (dif===0) || (--it<=0);
}while(!conv);

@@ -122,5 +124,5 @@

centroids : ks
}
};
}
module.exports = skmeans;
{
"name": "skmeans",
"version": "0.3.1",
"version": "0.4.0",
"description": "Super fast simple kmeans clustering for unidimiensional data",

@@ -21,3 +21,12 @@ "author": "David Gómez Matarrodona <solzimer@gmail.com>",

"dependencies": {},
"devDependencies": {}
"devDependencies": {
"babel-preset-es2015": "^6.24.1",
"grunt": "^1.0.1",
"grunt-babel": "^6.0.0",
"grunt-browserify": "^5.0.0",
"grunt-contrib-clean": "^1.1.0",
"grunt-contrib-uglify": "^3.0.1",
"load-grunt-tasks": "^3.5.2",
"uglify": "^0.1.5"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc