Exemple #1
0
  /** Generate a random neighbor which differs in only one medoid with current clusters. */
  private double getRandomNeighbor(T[] data, T[] medoids, int[] y, double[] d) {
    int n = data.length;

    int index = Math.randomInt(k);
    T medoid = null;
    boolean dup;
    do {
      dup = false;
      medoid = data[Math.randomInt(n)];
      for (int i = 0; i < k; i++) {
        if (medoid == medoids[i]) {
          dup = true;
          break;
        }
      }
    } while (dup);

    medoids[index] = medoid;

    for (int i = 0; i < n; i++) {
      double dist = distance.d(data[i], medoid);
      if (d[i] > dist) {
        y[i] = index;
        d[i] = dist;
      } else if (y[i] == index) {
        d[i] = dist;
        y[i] = index;
        for (int j = 0; j < k; j++) {
          if (j != index) {
            dist = distance.d(data[i], medoids[j]);
            if (d[i] > dist) {
              y[i] = j;
              d[i] = dist;
            }
          }
        }
      }
    }

    return Math.sum(d);
  }
Exemple #2
0
  /**
   * Cluster a new instance.
   *
   * @param x a new instance.
   * @return the cluster label, which is the index of nearest medoid.
   */
  @Override
  public int predict(T x) {
    double minDist = Double.MAX_VALUE;
    int bestCluster = 0;

    for (int i = 0; i < k; i++) {
      double dist = distance.d(x, medoids[i]);
      if (dist < minDist) {
        minDist = dist;
        bestCluster = i;
      }
    }

    return bestCluster;
  }