Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String prediction(double[] xt, int K) {
- double[] distance = new double[m];
- for (int i = 0; i < m; i++) {
- distance[i] = distance(x[i],xt);
- }
- int indice = 0;
- double[][] kMin = new double[K][2];
- for(int i = 0; i < K; i++){
- double min = Integer.MAX_VALUE;
- for(int j = 0; j < distance.length; j++){
- if(distance[j] < min){
- min = distance[j];
- kMin[i][0] = min;
- kMin[i][1] = j;
- indice = j;
- }
- }
- distance[indice] = Integer.MAX_VALUE;
- }
- // Initialisation HashMap
- HashMap<String,Integer> comptage = new HashMap<>();
- for (int i = 0; i < m; i++) {
- if(!comptage.containsKey(y[i])){
- comptage.put(y[i], 0);
- }
- }
- int indiceK;
- for(int i = 0; i < kMin.length; i++){
- indiceK = (int)kMin[i][1];
- comptage.put(y[indiceK], comptage.get(y[indiceK])+1);
- }
- int maxCorrespondance = Integer.MIN_VALUE;
- String etiquette = null;
- for (String clef : comptage.keySet()) {
- if(comptage.get(clef) > maxCorrespondance){
- maxCorrespondance = comptage.get(clef);
- etiquette = clef;
- }
- }
- return etiquette;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement