Exemple #1
0
  /**
   * Knn evaluation for classification
   *
   * @return
   */
  public static int evaluacionKNNClass(
      int nvec,
      double conj[][],
      double real[][],
      int nominal[][],
      boolean nulos[][],
      int clases[],
      double ejemplo[],
      double ejReal[],
      int ejNominal[],
      boolean ejNulos[],
      int nClases,
      boolean distance,
      int vecinos[],
      int clase) {

    int i, j, l;
    boolean parar = false;
    int vecinosCercanos[];
    double minDistancias[];
    int votos[];
    double dist;
    int votada, votaciones;

    if (nvec > conj.length) nvec = conj.length;

    votos = new int[nClases];
    vecinosCercanos = new int[nvec];
    minDistancias = new double[nvec];
    for (i = 0; i < nvec; i++) {
      vecinosCercanos[i] = -1;
      minDistancias[i] = Double.POSITIVE_INFINITY;
    }

    for (i = 0; i < conj.length; i++) {
      dist =
          KNN.distancia(
              conj[i],
              real[i],
              nominal[i],
              nulos[i],
              ejemplo,
              ejReal,
              ejNominal,
              ejNulos,
              distance);
      if (dist > 0 && clases[i] == clase) {
        parar = false;
        for (j = 0; j < nvec && !parar; j++) {
          if (dist < minDistancias[j]) {
            parar = true;
            for (l = nvec - 1; l >= j + 1; l--) {
              minDistancias[l] = minDistancias[l - 1];
              vecinosCercanos[l] = vecinosCercanos[l - 1];
            }
            minDistancias[j] = dist;
            vecinosCercanos[j] = i;
          }
        }
      }
    }

    for (j = 0; j < nClases; j++) {
      votos[j] = 0;
    }

    for (j = 0; j < nvec; j++) {
      if (vecinosCercanos[j] >= 0) votos[clases[vecinosCercanos[j]]]++;
    }

    votada = 0;
    votaciones = votos[0];
    for (j = 1; j < nClases; j++) {
      if (votaciones < votos[j]) {
        votaciones = votos[j];
        votada = j;
      }
    }

    for (i = 0; i < vecinosCercanos.length; i++) vecinos[i] = vecinosCercanos[i];

    return votada;
  }