Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. String prediction(double[] xt, int K) {
  2. double[] distance = new double[m];
  3. for (int i = 0; i < m; i++) {
  4. distance[i] = distance(x[i],xt);
  5. }
  6.  
  7. int indice = 0;
  8. double[][] kMin = new double[K][2];
  9. for(int i = 0; i < K; i++){
  10. double min = Integer.MAX_VALUE;
  11. for(int j = 0; j < distance.length; j++){
  12. if(distance[j] < min){
  13. min = distance[j];
  14. kMin[i][0] = min;
  15. kMin[i][1] = j;
  16. indice = j;
  17. }
  18. }
  19. distance[indice] = Integer.MAX_VALUE;
  20. }
  21.  
  22. // Initialisation HashMap
  23. HashMap<String,Integer> comptage = new HashMap<>();
  24. for (int i = 0; i < m; i++) {
  25. if(!comptage.containsKey(y[i])){
  26. comptage.put(y[i], 0);
  27. }
  28. }
  29.  
  30. int indiceK;
  31. for(int i = 0; i < kMin.length; i++){
  32. indiceK = (int)kMin[i][1];
  33. comptage.put(y[indiceK], comptage.get(y[indiceK])+1);
  34. }
  35.  
  36. int maxCorrespondance = Integer.MIN_VALUE;
  37. String etiquette = null;
  38. for (String clef : comptage.keySet()) {
  39. if(comptage.get(clef) > maxCorrespondance){
  40. maxCorrespondance = comptage.get(clef);
  41. etiquette = clef;
  42. }
  43. }
  44. return etiquette;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement