Exemple #1
0
  /**
   * Prepare a clustering pass on the indicated data.
   *
   * @param points The array of dataPoints to be clustered.
   * @param ids Array of cluster numbers which this call will fill in, defining which cluster each
   *     point belongs to. The caller must leave the data here intact between iterations.
   * @param means Array of x,y values in which to place centroids of the clusters.
   * @param region The region of the plane in which the points lie.
   */
  @Override
  public void prepare(Point[] points, int[] ids, double[][] means, Region region) {
    super.prepare(points, ids, means, region);

    // Save the data arrays.
    dataPoints = points;
    pointClusters = ids;
    clusterMeans = means;
    numPoints = points.length;
    numClusters = means.length;

    // Set up the strengths array.
    clusterStrengths = new double[numPoints][numClusters];

    // Set the initial cluster centroids to be random values
    // within the data region.
    double x = region.getX1();
    double y = region.getY1();
    double w = region.getWidth();
    double h = region.getHeight();
    for (int i = 0; i < numClusters; ++i) {
      means[i][0] = random.nextDouble() * w + x;
      means[i][1] = random.nextDouble() * h + y;
    }

    // Make an initial assignment of points to clusters, so on the first
    // iteration we have a basis for computing centroids.
    assignPoints(ids, means);
  }