Heim >Web-Frontend >js-Tutorial >javascript自然分类法算法实现代码_javascript技巧

javascript自然分类法算法实现代码_javascript技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:20:221446Durchsuche

复制代码 代码如下:






content="initial-scale=1, maximum-scale=1,user-scalable=no">
自然分类法算法
<script><BR>function getJenksBreaks(data,numclass) {<BR>function sortNumber(a,b)//在javascript里,Array的sort方法,必须用这个函数,否则不是按数字大小排序<BR>{<BR>return a - b<BR>}<BR>// int numclass;<BR>var numdata = data.length;<BR>data.sort(sortNumber); //先排序 <P>var mat1=new Array();<BR>var mat2=new Array();<BR>var st=new Array(); <P>for (var j = 0; j <= numdata; j++){<BR>mat1[j]=new Array();<BR>mat2[j]=new Array();<BR>st[j]=0;<BR>for(var i=0;i<=numclass;i++){<BR>mat1[j][i]=0;<BR>mat2[j][i]=0;<BR>}<BR>} <P>for (var i = 1; i <= numclass; i++) {<BR>mat1[1][i] = 1;<BR>mat2[1][i] = 0;<BR>for (var j = 2; j <= numdata; j++){ <P>mat2[j][i]=Number.MAX_VALUE;<BR>}<BR>}<BR>var v=0; <P>for (var l = 2; l <= numdata; l++) {<BR>var s1=0;<BR>var s2=0;<BR>var w=0;<BR>var i3=0;<BR>for (var m = 1; m <= l; m++) {<BR>i3 = l - m + 1; <P>var val=parseInt(data[i3-1]); <P>s2 += val * val;<BR>s1 += val; <P>w++;<BR>v = s2 - (s1 * s1) / w;<BR>var i4 = i3 - 1;<BR>if (i4 != 0) {<BR>for (var j = 2; j <= numclass; j++) {<BR>if (mat2[l][j] >= (v + mat2[i4][j - 1])) {<BR>mat1[l][j] = i3;<BR>mat2[l][j] = v + mat2[i4][j - 1]; <P>if(l==200&&j==5) alert("l="+200+",j="+5+";mat2[200][5]="+mat1[l][j]+"i3="+i3);<BR>}<BR>}<BR>}<BR>} <P>mat1[l][1] = 1;<BR>mat2[l][1] = v;<BR>} <P>var k = numdata;<BR>var kclass=new Array(); <P>/* int[] kclass = new int[numclass]; */<BR>kclass[numclass - 1] = parseInt(data[data.length-1]);<BR>/* kclass[numclass - 1] = (Integer) data.get(data.size() - 1); */ <P>for (var j = numclass; j >= 2; j--) {<BR>var id = parseInt(mat1[k][j]) - 2;<BR>kclass[j - 2] = parseInt( data[id]);<BR>k = parseInt( mat1[k][j] - 1);<BR>} <P>return kclass;<BR>} <P>function doit(){<BR>var data = new Array( 9, 9, 9, 9, 8, 1, 9, 7, 6, 7, 10, 7, 7, 8, 5, 7, 8, 2,<BR>6, 6, 9, 10, 2, 2, 3, 9, 9, 1, 9, 1, 6, 7, 1, 9, 6, 5, 2, 8, 6,<BR>6, 6, 4, 1, 4, 10, 9, 6, 9, 9, 7, 10, 9, 6, 6, 6, 9, 3, 2, 5,<BR>10, 3, 9, 6, 1, 2, 1, 2, 7, 5, 2, 6, 6, 2, 7, 9, 9, 5, 4, 6, 4,<BR>1, 1, 5, 1, 4, 4, 2, 6, 9, 7, 3, 10, 4, 4, 9, 5, 5, 3, 5, 3,<BR>161, 136, 22, 113, 48, 114, 166, 33, 159, 182, 135, 22, 174,<BR>191, 60, 176, 1, 155, 62, 142, 185, 84, 140, 27, 111, 176, 189,<BR>24, 60, 29, 139, 20, 70, 172, 90, 120, 121, 173, 57, 138, 86,<BR>180, 143, 155, 35, 10, 136, 158, 26, 29, 136, 97, 193, 194,<BR>104, 147, 182, 44, 84, 139, 176, 16, 130, 168, 109, 182, 87,<BR>78, 115, 75, 32, 88, 131, 168, 89, 25, 160, 60, 72, 92, 129,<BR>159, 99, 5, 192, 6, 53, 15, 62, 176, 73, 129, 128, 34, 190, 95,<BR>19, 19, 196, 30 ); <P>/* var list = new Array();<BR>for(int i = 0; i < data.length; i++){<BR>list.add(data[i]);<BR>} */<BR>//Collections.sort(list);<BR>var text=document.getElementById("text");<BR>text.value="";<BR>var grade = getJenksBreaks(data, 5);<BR>for (var i = 0; i < grade.length; i++) <BR>text.value=text.value+grade[i]+","; <P>} <P></script>





Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn