java - How to make a loop for determining class KNN Method -


i'm trying create program knn method , i'm stuck in loop determining class. here's coding :

public static void main(string[] args) {     int titikx, titiky, k;     int[] titikxl = new int[]{1, 1, 3, 2, 4, 6}; //first feature     int[] titiky1 = new int[]{1, 3, 1, 5, 3, 2}; //second feature     arraylist<double> nx = new arraylist<double>(), ny = new arraylist<double>(),      fn = new arraylist<double>(), arclass1 = new arraylist<double>(),      arclass2 = new arraylist<double>();      //input hew data's features , k     scanner input = new scanner(system.in);     system.out.println("input first feature : ");     titikx = input.nextint();     system.out.println("input second feature : ");     titiky = input.nextint();     system.out.println("input k : ");     k = input.nextint();      //count distance between new data , training data     int = 0, j = 0;     while(i < titikxl.length || j <titiky1.length){         nx.add(math.pow(titikx - titikxl[i], 2));         ny.add(math.pow(titiky - titiky1[j], 2));         i++;         j++;     }      system.out.println(nx);     system.out.println(ny);      //convert arraylist array     double[] nxarray = nx.toarray(new double[nx.size()]);     double[] nyarray = ny.toarray(new double[ny.size()]);      //sum array of distance first feature , second feature result      int ii = 0, jj = 0;     while (ii < nxarray.length || jj < nyarray.length){         fn.add(math.sqrt(nxarray[ii] + nyarray[jj]));         ii++;         jj++;     }      system.out.println(fn);     double[] fnarray = fn.toarray(new double[fn.size()]);     double[] oldfnarray = fnarray; //array result before sort ascending      //ascending array     for(int id1 = 0; id1 < fnarray.length; id1++){         for(int id2 = id1 + 1; id2 < fnarray.length; id2++){             if(fnarray[id1]>fnarray[id2]){                 double temp = fnarray[id2];                 fnarray[id2] = fnarray[id1];                 fnarray[id1] = temp;             }         }     }      for(int id = 0; id < fnarray.length; id++){         system.out.print(fnarray[id] + " ");     }     system.out.println();      double[] classa = new double[]{oldfnarray[0], oldfnarray[1], oldfnarray[2]};     double[] classb = new double[]{oldfnarray[3], oldfnarray[4], oldfnarray[5]};      //determining class new data belongs     for(int idb = 0; idb < classa.length; idb++){         for(int idc = 0; idc < classb.length; idc++){             for(int ida = 0; ida < fnarray.length; ida++){                 while(ida < k){                     if (classa[idb] == fnarray[ida]){                         arclass1.add(fnarray[ida]);                     }                      if (classb[idc] == fnarray[ida]){                         arclass2.add(fnarray[ida]);                     }                 }                 }         }     }      if(arclass1.size() < arclass2.size()){         system.out.println("the class b");     } else{         system.out.println("the class a");     } } 

and result :

input first feature : 2 input second feature : 3 input k : 3 [1.0, 1.0, 1.0, 0.0, 4.0, 16.0] //distance feature x [4.0, 0.0, 4.0, 4.0, 0.0, 1.0] //distance feature y [2.23606797749979, 1.0, 2.23606797749979, 2.0, 2.0, 4.123105625617661] //result 1.0 2.0 2.0 2.23606797749979 2.23606797749979 4.123105625617661 //ascended result //looping forever  build stopped (total time: 35 seconds) 

as can see, in section determining class, when enter loop program seems doing last loop forever. try modified capable could, yet still there's no result error , running forever. please help. thank kindly.

the problem endless while loop - ida's value never changes. suggest modifying entire code block because more complicated needs be.

before coming solution, let's determine know:

  • the nearest k-neighbors (the sorted distances array, indexes smaller k)
  • which neighbors belong class

it becomes obvious in order determine class, need to:

  • check every neighbor (outer loop) whether in class a (we need first inner loop) or class b (we need second inner loop)
  • if neighbor found in class a, increase counter class a, otherwise class b
  • compare counters: if counter class a greater, feature belongs in class a, otherwise in class b

to better understand going on, k-nn classification algorithm described comprehensively in this tutorial.

code (same structure yours, though renamed variables , simplified parts readability):

import java.util.arrays; import java.util.scanner;  public class knn {      public static void main(string[] args) {         int[] feature1 = new int[] { 1, 1, 3, 2, 4, 6 };         int[] feature2 = new int[] { 1, 3, 1, 5, 3, 2 };          //input hew data's features , k         scanner input = new scanner(system.in);          system.out.println("input first feature : ");         int newfeature1 = input.nextint();          system.out.println("input second feature : ");         int newfeature2 = input.nextint();          system.out.println("input k : ");         int k = input.nextint();          input.close();          //count distance between new data , training data         double[] distances1 = new double[feature1.length];         double[] distances2 = new double[feature2.length];          (int = 0; < distances1.length; i++) {             distances1[i] = math.pow(newfeature1 - feature1[i], 2);         }          (int = 0; < distances2.length; i++) {             distances2[i] = math.pow(newfeature2 - feature2[i], 2);         }          system.out.println("distance between first feature , first feature training data: " + arrays.tostring(distances1));         system.out.println("distance between second feature , second feature training data: " + arrays.tostring(distances2));          //sum array of distance first feature , second feature result         double[] distancesums = new double[distances1.length];          (int = 0; < distances1.length; i++) {             distancesums[i] = math.sqrt(distances1[i] + distances2[i]);         }          system.out.println("distance sums: " + arrays.tostring(distancesums));          // sort array ascending         double[] distancesumssorted = new double[distancesums.length];         system.arraycopy(distancesums, 0, distancesumssorted, 0, distancesums.length);         arrays.sort(distancesumssorted);          system.out.println("sorted distance sums: " + arrays.tostring(distancesumssorted));          double[] classamembers = new double[] { distancesums[0], distancesums[1], distancesums[2] };         double[] classbmembers = new double[] { distancesums[3], distancesums[4], distancesums[5] };          //determining class new data belongs         int classacounts = 0;         int classbcounts = 0;          (int = 0; < k; i++) {             // check if nearest neighbor belongs class             (int j = 0; j < classamembers.length; j++) {                 if (distancesumssorted[i] == classamembers[j]) {                     classacounts++;                     break;                 }             }             // check if nearest neighbor belongs class b             (int j = 0; j < classbmembers.length; j++) {                 if (distancesumssorted[i] == classbmembers[j]) {                     classbcounts++;                     break;                 }             }         }          system.out.println("class members: " + arrays.tostring(classamembers));         system.out.println("class b members: " + arrays.tostring(classbmembers));          system.out.println("counts class a: " + classacounts);         system.out.println("counts class b: " + classbcounts);          if (classacounts < classbcounts){             system.out.println("the class b.");         }         else {             system.out.println("the class a.");         }     } } 

for example data program outputs:

input first feature : 2 input second feature : 3 input k : 3 distance between first feature , first feature training data: [1.0, 1.0, 1.0, 0.0, 4.0, 16.0] distance between second feature , second feature training data: [4.0, 0.0, 4.0, 4.0, 0.0, 1.0] distance sums: [2.23606797749979, 1.0, 2.23606797749979, 2.0, 2.0, 4.123105625617661] sorted distance sums: [1.0, 2.0, 2.0, 2.23606797749979, 2.23606797749979, 4.123105625617661] class members: [2.23606797749979, 1.0, 2.23606797749979] class b members: [2.0, 2.0, 4.123105625617661] counts class a: 1 counts class b: 2 class b. 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -