@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 void init(int pointDimension, long randomSeed) { this.N = pointDimension; kmeans.init(N,randomSeed); if( dx.length < N ) { dx = new double[N]; } }
@Override public void init(final int pointDimension, long randomSeed) { seedSelector.init(pointDimension,randomSeed); this.N = pointDimension; clusters = createQueue(pointDimension); workClusters = createQueue(pointDimension); bestClusters = createQueue(pointDimension); memberCount.resize(pointDimension); }
@Test public void matchPointsToClusters() { StandardKMeans_F64 alg = new StandardKMeans_F64(100,100,1,new InitializeStandard_F64()); alg.init(4, 123); alg.clusters.resize(3); alg.workClusters.resize(3); alg.memberCount.resize(3); alg.clusters.data[0] = new double[]{20,0,0,0}; alg.clusters.data[1] = new double[]{0,20,0,0}; alg.clusters.data[2] = new double[]{0,0,20,0}; List<double[]> points = new ArrayList<double[]>(); points.add( new double[]{20,5,0,0}); points.add( new double[]{25,-4,0,0}); points.add( new double[]{0,0,22,0}); alg.matchPointsToClusters(points); assertEquals(2,alg.memberCount.get(0)); assertEquals(0,alg.memberCount.get(1)); assertEquals(1,alg.memberCount.get(2)); assertEquals(45,alg.workClusters.data[0][0],1e-8); assertEquals(1 ,alg.workClusters.data[0][1],1e-8); assertEquals(0 ,alg.workClusters.data[0][2],1e-8); assertEquals(0 ,alg.workClusters.data[0][3],1e-8); assertEquals(0 ,alg.workClusters.data[1][1],1e-8); assertEquals(22,alg.workClusters.data[2][2],1e-8); }
@Test public void updateClusterCenters() { StandardKMeans_F64 alg = new StandardKMeans_F64(100,100,1,new InitializeStandard_F64()); alg.init(4,123); alg.clusters.resize(3); alg.workClusters.resize(3); alg.memberCount.resize(3); double orig[][] = new double[3][4]; orig[0] = new double[]{10,20,30,20}; orig[1] = new double[]{20,10,30,40}; orig[2] = new double[]{3,9,1,12}; alg.workClusters.data[0] = orig[0].clone(); alg.workClusters.data[1] = orig[1].clone(); alg.workClusters.data[2] = orig[2].clone(); alg.memberCount.data[0] = 10; alg.memberCount.data[1] = 1; alg.memberCount.data[2] = 3; // previous clusters will be near zero alg.updateClusterCenters(); for (int i = 0; i < 4; i++) { assertEquals(alg.clusters.data[0][i],orig[0][i]/10); assertEquals(alg.clusters.data[1][i],orig[1][i]/1); assertEquals(alg.clusters.data[2][i],orig[2][i]/3); } }
kmeans.process(points,seeds.size()); GrowQueue_I32 labels = kmeans.getPointLabels(); FastQueue<double[]> means = kmeans.getClusterMeans();
return new StandardKMeans_F64(maxIterations,maxConverge,convergeTol,seed);
/** * Finds the cluster which is the closest to each point. The point is the added to the sum for the cluster * and its member count incremented */ protected void matchPointsToClusters(List<double[]> points) { sumDistance = 0; for (int i = 0; i < points.size(); i++) { double[]p = points.get(i); // find the cluster which is closest to the point int bestCluster = findBestMatch(p); // sum up all the points which are members of this cluster double[] c = workClusters.get(bestCluster); for (int j = 0; j < c.length; j++) { c[j] += p[j]; } memberCount.data[bestCluster]++; labels.data[i] = bestCluster; sumDistance += bestDistance; } }
kmeans.process(points,seeds.size()); GrowQueue_I32 labels = kmeans.getPointLabels(); FastQueue<double[]> means = kmeans.getClusterMeans();
return new StandardKMeans_F64(maxIterations,maxConverge,convergeTol,seed);
/** * Finds the cluster which is the closest to each point. The point is the added to the sum for the cluster * and its member count incremented */ protected void matchPointsToClusters(List<double[]> points) { sumDistance = 0; for (int i = 0; i < points.size(); i++) { double[]p = points.get(i); // find the cluster which is closest to the point int bestCluster = findBestMatch(p); // sum up all the points which are members of this cluster double[] c = workClusters.get(bestCluster); for (int j = 0; j < c.length; j++) { c[j] += p[j]; } memberCount.data[bestCluster]++; labels.data[i] = bestCluster; sumDistance += bestDistance; } }
@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 ComputeClusters<double[]> createClustersAlg( boolean hint ) { if( hint ) { // assume the first 3 are in different groups for the seeds return new StandardKMeans_F64(1000,1000, 1e-8, new FixedSeeds()); } else { InitializeStandard_F64 seeds = new InitializeStandard_F64(); return new StandardKMeans_F64(1000,1000, 1e-8, seeds ); } }
@Override public void init(int pointDimension, long randomSeed) { this.N = pointDimension; kmeans.init(N,randomSeed); if( dx.length < N ) { dx = new double[N]; } }
@Override public void init(final int pointDimension, long randomSeed) { seedSelector.init(pointDimension,randomSeed); this.N = pointDimension; clusters = createQueue(pointDimension); workClusters = createQueue(pointDimension); bestClusters = createQueue(pointDimension); memberCount.resize(pointDimension); }
/** * 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; }
private StandardKMeans_F64 createKMeans() { return new StandardKMeans_F64(200,200,1e-6,new FixedSeeds()); }
/** * 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; }