@thi.ng/k-means
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -6,2 +6,13 @@ # Change Log | ||
## [0.2.5](https://github.com/thi-ng/umbrella/compare/@thi.ng/k-means@0.2.4...@thi.ng/k-means@0.2.5) (2021-08-04) | ||
### Bug Fixes | ||
* **k-means:** update initKmeanspp() ([dd0d965](https://github.com/thi-ng/umbrella/commit/dd0d9654b1aacce8a4bbbd921f2ce44d0eaa276a)) | ||
## [0.2.4](https://github.com/thi-ng/umbrella/compare/@thi.ng/k-means@0.2.3...@thi.ng/k-means@0.2.4) (2021-07-27) | ||
@@ -8,0 +19,0 @@ |
@@ -72,6 +72,7 @@ import { assert } from "@thi.ng/api"; | ||
while (centroidIDs.length < k) { | ||
let probs = samples.map((p) => dist.from(metric(p, centroids[argmin(p, centroids)])) ** 2); | ||
let probs = samples.map((p) => dist.from(metric(p, centroids[argmin(p, centroids, dist)])) ** | ||
2); | ||
let id; | ||
do { | ||
id = weightedRandom(indices, probs)(); | ||
id = weightedRandom(indices, probs, rnd)(); | ||
} while (centroidIDs.includes(id)); | ||
@@ -78,0 +79,0 @@ centroidIDs.push(id); |
@@ -50,6 +50,7 @@ 'use strict'; | ||
while (centroidIDs.length < k) { | ||
let probs = samples.map((p) => dist.from(metric(p, centroids[distance.argmin(p, centroids)])) ** 2); | ||
let probs = samples.map((p) => dist.from(metric(p, centroids[distance.argmin(p, centroids, dist)])) ** | ||
2); | ||
let id; | ||
do { | ||
id = random.weightedRandom(indices, probs)(); | ||
id = random.weightedRandom(indices, probs, rnd)(); | ||
} while (centroidIDs.includes(id)); | ||
@@ -56,0 +57,0 @@ centroidIDs.push(id); |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/api"),require("@thi.ng/distance"),require("@thi.ng/random"),require("@thi.ng/vectors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/api","@thi.ng/distance","@thi.ng/random","@thi.ng/vectors"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.kMeans={}),e.thi.ng.api,e.thi.ng.distance,e.thi.ng.random,e.thi.ng.vectors)}(this,(function(e,t,n,i,s){"use strict";const r=(e,s,r=n.DIST_SQ,o=i.SYSTEM)=>{const a=s.length;t.assert(a>=e,`insufficient samples for k=${e}`);const h=[o.int()%a],d=[s[h[0]]],l=new Array(a).fill(0).map(((e,t)=>t)),u=r.metric;for(;h.length<e;){let e,t=s.map((e=>r.from(u(e,d[n.argmin(e,d)]))**2));do{e=i.weightedRandom(l,t)()}while(h.includes(e));h.push(e),d.push(s[e])}return h},o=(e,t,i,s)=>{let r=!1;for(let o=0,a=e.length;o<a;o++){const a=n.argmin(e[o],t,s);a!==i[o]&&(i[o]=a,r=!0)}return r},a=(e,t)=>{const n=[];for(let i=0,s=t.length;i<s;i++){const s=t[i];(n[s]||(n[s]={id:s,centroid:e[s],items:[]})).items.push(i)}return n},h=e=>{const t=s.zeroes(e);let n=0;return{update:e=>{s.add(t,t,e),n++},finish:()=>n?s.mulN(t,t,1/n):void 0}};e.initKmeanspp=r,e.kmeans=(e,s,d)=>{let{dist:l,initial:u,maxIter:g,rnd:f,strategy:c}=Object.assign({dist:n.DIST_SQ,maxIter:32,strategy:h},d);const m=s.length,p=s[0].length,y=u||r(e,s,l,f);t.assert(y.length===e,"wrong number of initial centroids");const b=y.map((e=>s[e])),v=[];let S=!0;e:for(;S&&g-- >0;){S=o(s,b,v,l);for(let t=0;t<e;t++){const e=c(p);for(let n=0;n<m;n++)t===v[n]&&e.update(s[n]);const n=e.finish();if(n)b[t]=n;else{const e=y.length;if(i.uniqueIndices(1,m,y,void 0,f),y.length===e)break e;b[t]=s[y[e]],S=!0}}}return a(b,v)},e.means=h,e.meansLatLon=()=>{let e=0,t=0,n=0;return{update:([i,s])=>{e+=i<0?i+360:i,t+=s,n++},finish:()=>{if(n)return e/=n,e>180&&(e-=360),t/=n,[e,t]}}},e.medians=()=>{const e=[];return{update:t=>e.push(t),finish:()=>e.length?s.median([],e):void 0}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/api"),require("@thi.ng/distance"),require("@thi.ng/random"),require("@thi.ng/vectors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/api","@thi.ng/distance","@thi.ng/random","@thi.ng/vectors"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.kMeans={}),e.thi.ng.api,e.thi.ng.distance,e.thi.ng.random,e.thi.ng.vectors)}(this,(function(e,t,n,i,s){"use strict";const r=(e,s,r=n.DIST_SQ,o=i.SYSTEM)=>{const a=s.length;t.assert(a>=e,`insufficient samples for k=${e}`);const h=[o.int()%a],d=[s[h[0]]],l=new Array(a).fill(0).map(((e,t)=>t)),u=r.metric;for(;h.length<e;){let e,t=s.map((e=>r.from(u(e,d[n.argmin(e,d,r)]))**2));do{e=i.weightedRandom(l,t,o)()}while(h.includes(e));h.push(e),d.push(s[e])}return h},o=(e,t,i,s)=>{let r=!1;for(let o=0,a=e.length;o<a;o++){const a=n.argmin(e[o],t,s);a!==i[o]&&(i[o]=a,r=!0)}return r},a=(e,t)=>{const n=[];for(let i=0,s=t.length;i<s;i++){const s=t[i];(n[s]||(n[s]={id:s,centroid:e[s],items:[]})).items.push(i)}return n},h=e=>{const t=s.zeroes(e);let n=0;return{update:e=>{s.add(t,t,e),n++},finish:()=>n?s.mulN(t,t,1/n):void 0}};e.initKmeanspp=r,e.kmeans=(e,s,d)=>{let{dist:l,initial:u,maxIter:g,rnd:f,strategy:c}=Object.assign({dist:n.DIST_SQ,maxIter:32,strategy:h},d);const m=s.length,p=s[0].length,y=u||r(e,s,l,f);t.assert(y.length===e,"wrong number of initial centroids");const b=y.map((e=>s[e])),v=[];let S=!0;e:for(;S&&g-- >0;){S=o(s,b,v,l);for(let t=0;t<e;t++){const e=c(p);for(let n=0;n<m;n++)t===v[n]&&e.update(s[n]);const n=e.finish();if(n)b[t]=n;else{const e=y.length;if(i.uniqueIndices(1,m,y,void 0,f),y.length===e)break e;b[t]=s[y[e]],S=!0}}}return a(b,v)},e.means=h,e.meansLatLon=()=>{let e=0,t=0,n=0;return{update:([i,s])=>{e+=i<0?i+360:i,t+=s,n++},finish:()=>{if(n)return e/=n,e>180&&(e-=360),t/=n,[e,t]}}},e.medians=()=>{const e=[];return{update:t=>e.push(t),finish:()=>e.length?s.median([],e):void 0}},Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@thi.ng/k-means", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Configurable k-means & k-medians (with k-means++ initialization) for n-D vectors", | ||
@@ -41,6 +41,6 @@ "module": "./index.js", | ||
"dependencies": { | ||
"@thi.ng/api": "^7.1.6", | ||
"@thi.ng/distance": "^0.3.4", | ||
"@thi.ng/random": "^2.4.2", | ||
"@thi.ng/vectors": "^6.0.3" | ||
"@thi.ng/api": "^7.1.7", | ||
"@thi.ng/distance": "^0.3.5", | ||
"@thi.ng/random": "^2.4.3", | ||
"@thi.ng/vectors": "^6.0.4" | ||
}, | ||
@@ -69,3 +69,3 @@ "files": [ | ||
}, | ||
"gitHead": "ad6ac106b39a18735187a1b59313e06f5d7a9116" | ||
"gitHead": "a85a4dd69e086a18d97d22204667c6b5334b01e0" | ||
} |
@@ -45,3 +45,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (gzipped, pre-treeshake): ESM: 928 bytes / CJS: 987 bytes / UMD: 1.07 KB | ||
Package sizes (gzipped, pre-treeshake): ESM: 930 bytes / CJS: 991 bytes / UMD: 1.07 KB | ||
@@ -48,0 +48,0 @@ ## Dependencies |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
45707
407
Updated@thi.ng/api@^7.1.7
Updated@thi.ng/distance@^0.3.5
Updated@thi.ng/random@^2.4.3
Updated@thi.ng/vectors@^6.0.4