Visualizing stable/unstable equilibria in K-Means unsupervised learning algorithm. Each run follows the path of the mean converging as shown here
| function randomPoints(_num, _dist, _xR, _yR) { | |
| if(arguments.length<2) _dist = 'irwinHall' | |
| if(!_xR||!_yR) { | |
| var xRange = yRange = [0,1] | |
| }else{ | |
| var xRange = d3.range(_xR[0],_xR[1]) | |
| var yRange = d3.range(_yR[0],_yR[1]) | |
| } | |
| if(_dist == 'uniform'){ | |
| x = d3.range(0,_num).map(function () { | |
| return d3.shuffle(xRange)[0] | |
| }) | |
| y = d3.range(0,_num).map(function () { | |
| return d3.shuffle(yRange)[0] | |
| }) | |
| }else{ | |
| x = d3.range(0,_num).map(function () { | |
| if(['bates','irwinHall'].indexOf(_dist)>-1){ | |
| rnd = d3.random[_dist](_dist=='bates'?8:1)() | |
| rnd = Math.round(rnd*d3.max(xRange)) | |
| }else if(_dist=='normal'){ | |
| rnd = d3.random[_dist](d3.mean(xRange),d3.mean(xRange)/3)() | |
| }else if(_dist=='logNormal'){ | |
| rnd = d3.random[_dist]()() | |
| rnd = Math.round(rnd*d3.mean(xRange)/3) | |
| } | |
| return rnd | |
| }).map(function (d) {return Math.abs(d)}) | |
| y = d3.range(0,_num).map(function () { | |
| if(['bates','irwinHall'].indexOf(_dist)>-1){ | |
| rnd = d3.random[_dist](_dist=='bates'?8:1)() | |
| rnd = Math.round(rnd*d3.max(yRange)) | |
| }else if(_dist=='normal'){ | |
| rnd = d3.random[_dist](d3.mean(yRange),d3.mean(yRange)/3)() | |
| }else if(_dist=='logNormal'){ | |
| rnd = d3.random[_dist]()() | |
| rnd = Math.round(rnd*d3.mean(yRange)/3) | |
| } | |
| return rnd | |
| }).map(function (d) {return Math.abs(d)}) | |
| } | |
| return d3.zip(x,y).map(function(d){return {x:d[0],y:d[1]}}) | |
| } | |
| function clusterPoints(numC,numPts,_dist,_xR,_yR){ | |
| var clusters = [] | |
| d3.range(0,numC).forEach(function (i) { | |
| cPts = randomPoints(4,'uniform',_xR,_yR) | |
| c={} | |
| c.dist = _dist | |
| c.xRange = d3.extent(cPts.map(function(p){return p.x})) | |
| c.yRange = d3.extent(cPts.map(function(p){return p.y})) | |
| c.points = randomPoints(numPts,c.dist,c.xRange,c.yRange) | |
| clusters.push(c) | |
| }) | |
| // clusters.reduce(function (prev,curr) {return prev.concat(curr)}) | |
| return clusters | |
| } |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>K-means</title> | |
| <style media="screen"> | |
| body{ | |
| margin: 0; | |
| } | |
| svg{ | |
| overflow: visible; | |
| } | |
| .test,.actual{ | |
| opacity: .4; | |
| } | |
| .test circle{ | |
| fill:rgba(255, 255, 255, 0); | |
| stroke:#aaa; | |
| stroke-width:1px; | |
| opacity: .5; | |
| } | |
| .lines line{ | |
| stroke:#aaa; | |
| stroke-width:1px; | |
| stroke-opacity:.25; | |
| } | |
| .mean{ | |
| /*opacity: 0;*/ | |
| /*fill:rgba(255, 255, 255, 0);*/ | |
| stroke-width:3px; | |
| } | |
| .meanPath{ | |
| fill:rgba(255, 255, 255, 0); | |
| stroke:#333; | |
| stroke-width:1px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <svg></svg> | |
| </body> | |
| <script src="lodash.min.js"></script> | |
| <script src="/blacki/raw/b83b3d4139257a353b8a/d3.min.js"></script> | |
| <script src="/blacki/raw/b83b3d4139257a353b8a/dat.gui.min.js"></script> | |
| <script src="/blacki/raw/b83b3d4139257a353b8a/d3-jetpack.js"></script> | |
| <script src="/blacki/raw/b83b3d4139257a353b8a/d3-starterkit.js"></script> | |
| <script src="d3-randompoints.js"></script> | |
| <script src="script.js"></script> | |
| </html> |
View Raw
(Sorry about that, but we can’t show files that are this big right now.)
View Raw
(Sorry about that, but we can’t show files that are this big right now.)
View Raw
(Sorry about that, but we can’t show files that are this big right now.)
View Raw
(Sorry about that, but we can’t show files that are this big right now.)
View Raw
(Sorry about that, but we can’t show files that are this big right now.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

