Home >Web Front-end >JS Tutorial >JavaScript natural classification algorithm implementation code_javascript skills

JavaScript natural classification algorithm implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:20:221425browse

Copy code The code is as follows:





content="initial-scale=1, maximum-scale=1,user-scalable=no">

<script><br>function getJenksBreaks(data,numclass) {<br>function sortNumber(a,b)//In javascript, the sort method of Array must Use this function, otherwise it will not be sorted by numerical size<br>{<br>return a - b<br>}<br>// int numclass;<br>var numdata = data.length;<br>data.sort( sortNumber); //Sort first <p>var mat1=new Array();<br>var mat2=new Array();<br>var st=new Array();</p> <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> <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> <P>mat2[j][i]=Number.MAX_VALUE;<BR>}<BR>}<BR>var v=0;</P> <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> <P>var val=parseInt(data[i3-1]);</P> <P>s2 = val * val;<BR>s1 = val;</P> <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> <p>if(l==200&&j==5) alert("l=" 200 ",j=" 5 ";mat2[200][5]=" mat1[l][j] "i3=" i3) ;<br>}<br>}<br>}<br>}</p> <p>mat1[l][1] = 1;<br>mat2[l][1] = v;<br>}</p> <p>var k = numdata;<br>var kclass=new Array();</p> <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> <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> <p>return kclass;<br>}</p> <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> <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>}</P> <P></script>





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn