@Override public int assign(double[] point) { int best = -1; double bestScore = Double.MAX_VALUE; for (int i = 0; i < clusters.size(); i++) { double score = StandardKMeans_F64.distanceSq(point,clusters.get(i)); if( score < bestScore ) { bestScore = score; best = i; } } return best; }
@Override public int assign(double[] point) { int best = -1; double bestScore = Double.MAX_VALUE; for (int i = 0; i < clusters.size(); i++) { double score = StandardKMeans_F64.distanceSq(point,clusters.get(i)); if( score < bestScore ) { bestScore = score; best = i; } } return best; }
/** * Searches for this cluster which is the closest to p */ protected int findBestMatch(double[] p) { int bestCluster = -1; bestDistance = Double.MAX_VALUE; for (int j = 0; j < clusters.size; j++) { double d = distanceSq(p,clusters.get(j)); if( d < bestDistance ) { bestDistance = d; bestCluster = j; } } return bestCluster; }
/** * Searches for this cluster which is the closest to p */ protected int findBestMatch(double[] p) { int bestCluster = -1; bestDistance = Double.MAX_VALUE; for (int j = 0; j < clusters.size; j++) { double d = distanceSq(p,clusters.get(j)); if( d < bestDistance ) { bestDistance = d; bestCluster = j; } } return bestCluster; }
double d = StandardKMeans_F64.distanceSq(point,clusters.get(i)); fit[i] = d; if( d > max ) {
double d = StandardKMeans_F64.distanceSq(point,clusters.get(i)); fit[i] = d; if( d > max ) {
/** * Updates the list of distances from a point to the closest cluster. Update list of total distances */ protected final void updateDistances( List<double[]> points , double []clusterNew ) { totalDistance = 0; for (int i = 0; i < distance.size(); i++) { double dOld = distance.get(i); double dNew = StandardKMeans_F64.distanceSq(points.get(i),clusterNew); if( dNew < dOld ) { distance.data[i] = dNew; totalDistance += dNew; } else { totalDistance += dOld; } } }
/** * Updates the list of distances from a point to the closest cluster. Update list of total distances */ protected final void updateDistances( List<double[]> points , double []clusterNew ) { totalDistance = 0; for (int i = 0; i < distance.size(); i++) { double dOld = distance.get(i); double dNew = StandardKMeans_F64.distanceSq(points.get(i),clusterNew); if( dNew < dOld ) { distance.data[i] = dNew; totalDistance += dNew; } else { totalDistance += dOld; } } }
@Override public void selectSeeds(List<double[]> points, List<double[]> seeds) { if( seeds.size() > points.size() ) throw new IllegalArgumentException("More seeds requested than points!"); distance.resize(points.size()); // the first seed is randomly selected from the list of points double[] seed = points.get( rand.nextInt(points.size()) ); copyInto(seed,seeds.get(0)); // compute the distance each points is from the seed totalDistance = 0; for (int i = 0; i < points.size(); i++) { double[] p = points.get(i); double d = StandardKMeans_F64.distanceSq(p,seed); distance.data[i] = d; totalDistance += d; } // iteratively select the next seed and update the list of point distances for (int i = 1; i < seeds.size(); i++) { if( totalDistance == 0 ) { // if the total distance is zero that means there are duplicate points and that // all the unique points have already been added as seeds. just select a point // and copy it into rest of the seeds copyInto(seed, seeds.get(i)); } else { double target = rand.nextDouble(); copyInto(selectNextSeed(points, target), seeds.get(i)); updateDistances(points, seeds.get(i)); } } }
@Override public void selectSeeds(List<double[]> points, List<double[]> seeds) { if( seeds.size() > points.size() ) throw new IllegalArgumentException("More seeds requested than points!"); distance.resize(points.size()); // the first seed is randomly selected from the list of points double[] seed = points.get( rand.nextInt(points.size()) ); copyInto(seed,seeds.get(0)); // compute the distance each points is from the seed totalDistance = 0; for (int i = 0; i < points.size(); i++) { double[] p = points.get(i); double d = StandardKMeans_F64.distanceSq(p,seed); distance.data[i] = d; totalDistance += d; } // iteratively select the next seed and update the list of point distances for (int i = 1; i < seeds.size(); i++) { if( totalDistance == 0 ) { // if the total distance is zero that means there are duplicate points and that // all the unique points have already been added as seeds. just select a point // and copy it into rest of the seeds copyInto(seed, seeds.get(i)); } else { double target = rand.nextDouble(); copyInto(selectNextSeed(points, target), seeds.get(i)); updateDistances(points, seeds.get(i)); } } }
@Test public void distanceSq() { double a[] = new double[]{1,2,3,4,5}; double b[] = new double[]{4,6,3,1,-1}; double found = StandardKMeans_F64.distanceSq(a,b); assertEquals(70.0,found,1e-8); }