Javascript natural Classification Algorithm Implementation Code

Source: Internet
Author: User
Tags array sort

Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta http-equiv = "X-UA-Compatible" content = "IE = 7, IE = 9, IE = 10">
<Meta name = "viewport"
Content = "initial-scale = 1, maximum-scale = 1, user-scalable = no">
<Title> natural classification algorithm </title>
<Script>
Function getJenksBreaks (data, numclass ){
Function sortNumber (a, B) // in javascript, this function must be used for the Array sort method, otherwise it is not sorted by number size
{
Return a-B
}
// Int numclass;
Var numdata = data. length;
Data. sort (sortNumber); // sort first

Var mat1 = new Array ();
Var mat2 = new Array ();
Var st = new Array ();

For (var j = 0; j <= numdata; j ++ ){
Mat1 [j] = new Array ();
Mat2 [j] = new Array ();
St [j] = 0;
For (var I = 0; I <= numclass; I ++ ){
Mat1 [j] [I] = 0;
Mat2 [j] [I] = 0;
}
}

For (var I = 1; I <= numclass; I ++ ){
Mat1 [1] [I] = 1;
Mat2 [1] [I] = 0;
For (var j = 2; j <= numdata; j ++ ){

Mat2 [j] [I] = Number. MAX_VALUE;
}
}
Var v = 0;

For (var l = 2; l <= numdata; l ++ ){
Var s1 = 0;
Var s2 = 0;
Var w = 0;
Var i3 = 0;
For (var m = 1; m <= l; m ++ ){
I3 = l-m + 1;

Var val = parseInt (data [i3-1]);

S2 + = val * val;
S1 + = val;

W ++;
V = s2-(s1 * s1)/w;
Var i4 = i3-1;
If (i4! = 0 ){
For (var j = 2; j <= numclass; j ++ ){
If (mat2 [l] [j]> = (v + mat2 [i4] [j-1]) {
Mat1 [l] [j] = i3;
Mat2 [l] [j] = v + mat2 [i4] [j-1];

If (l = 200 & j = 5) alert ("l =" + 200 + ", j =" + 5 + "; mat2 [2, 200] [5] = "+ mat1 [l] [j] +" i3 = "+ i3 );
}
}
}
}

Mat1 [l] [1] = 1;
Mat2 [l] [1] = v;
}

Var k = numdata;
Var kclass = new Array ();

/* Int [] kclass = new int [numclass]; */
Kclass [numclass-1] = parseInt (data [data. length-1]);
/* Kclass [numclass-1] = (Integer) data. get (data. size ()-1 );*/

For (var j = numclass; j> = 2; j --){
Var id = parseInt (mat1 [k] [j])-2;
Kclass [j-2] = parseInt (data [id]);
K = parseInt (mat1 [k] [j]-1 );
}

Return kclass;
}

Function doit (){
Var data = new Array (9, 9, 9, 9, 8, 1, 9, 7, 6, 7, 10, 7, 7, 8, 5, 7, 8, 2,
6, 6, 9, 10, 2, 2, 3, 9, 9, 1, 9, 1, 6, 7, 1, 9, 6, 5, 2, 8, 6,
6, 6, 4, 1, 4, 10, 9, 6, 9, 9, 7, 10, 9, 6, 6, 9, 3, 2, 5,
10, 3, 9, 6, 1, 2, 1, 2, 7, 5, 2, 6, 6, 2, 7, 9, 9, 5, 4, 6, 4,
1, 1, 5, 1, 4, 4, 2, 6, 9, 7, 3, 10, 4, 4, 9, 5, 5, 3, 5, 3,
161,136, 22,113, 48,114,166, 33,159,182,135, 22,174,
191, 60,176, 1,155, 62,142,185, 84,140, 27,111,176,189,
24, 60, 29,139, 20, 70,172, 90,120,121,173, 57,138, 86,
180,143,155, 35, 10,136,158, 26, 29,136, 97,193,194,
104,147,182, 44, 84,139,176, 16,130,168,109,182, 87,
78,115, 75, 32, 88,131,168, 89, 25,160, 60, 72, 92,129,
159, 99, 5,192, 6, 53, 15, 62,176, 73,129,128, 34,190, 95,
19, 19,196, 30 );

/* Var list = new Array ();
For (int I = 0; I <data. length; I ++ ){
List. add (data [I]);
}*/
// Collections. sort (list );
Var text = document. getElementById ("text ");
Text. value = "";
Var grade = getJenksBreaks (data, 5 );
For (var I = 0; I <grade. length; I ++)
Text. value = text. value + grade [I] + ",";

}

</Script>
</Head>

<Body>
<Button type = "button" onclick = "doit ()"> natural classification </button>

<Input id = "text"> </input>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.