@Override public void setVerbose(boolean verbose) { kmeans.setVerbose(verbose); } }
@Override public AssignCluster<double[]> copy() { return new AssignKMeans_F64(this); }
@Override public void init(int pointDimension, long randomSeed) { this.N = pointDimension; kmeans.init(N,randomSeed); if( dx.length < N ) { dx = new double[N]; } }
/** * Request more seeds than there are points. This is impossible to do and ensure the seeds are * unique. */ @Test public void impossible() { try { int DOF = 20; InitializeKMeans_F64 alg = createAlg(); alg.init(DOF,0xBEEF); // 4 points and 4 seeds. Each point must be a seed List<double[]> points = TestStandardKMeans_F64.createPoints(DOF,3,true); List<double[]> seeds = TestStandardKMeans_F64.createPoints(DOF,4,false); alg.selectSeeds(points,seeds); fail("Should have thrown an exception!"); } catch( Exception e ) { } }
@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(final int pointDimension, long randomSeed) { seedSelector.init(pointDimension,randomSeed); this.N = pointDimension; clusters = createQueue(pointDimension); workClusters = createQueue(pointDimension); bestClusters = createQueue(pointDimension); memberCount.resize(pointDimension); }
@Override public ComputeClusters<double[]> createClustersAlg( boolean hint ) { if( hint ) { return new ExpectationMaximizationGmm_F64(1000, 1e-8, seeds); } else { InitializeStandard_F64 kseeds = new InitializeStandard_F64(); StandardKMeans_F64 kmeans = new StandardKMeans_F64(1000,1000,1e-8,kseeds); SeedFromKMeans_F64 seeds = new SeedFromKMeans_F64(kmeans); return new ExpectationMaximizationGmm_F64(1000, 1e-8, seeds); } } }
@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; }
@Test public void serialize() { List<double[]> clusters = new ArrayList<double[]>(); clusters.add( new double[]{10,0,0}); clusters.add( new double[]{0,0,10}); AssignKMeans_F64 alg = new AssignKMeans_F64(clusters); byte[] encoded = save(alg); // AssignKMeans_F64 found = load( encoded ); }
@Override public InitializeKMeans_F64 createAlg() { return new InitializeStandard_F64(); } }
@Override public InitializeKMeans_F64 createAlg() { return new InitializePlusPlus(); } }
private StandardKMeans_F64 createKMeans() { return new StandardKMeans_F64(200,200,1e-6,new FixedSeeds()); }
/** * Makes sure the seeds that it selects are unique */ @Test public void uniqueSeeds() { // extremely unlikely that it will select each one uniquely if random uniqueSeeds(4,4); // The standard algorithm switches technique when the ratio of points to seeds become // more extreme. This might not have a collision uniqueSeeds(10,4); }
@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); }
@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 AssignCluster<double[]> copy() { return new AssignKMeans_F64(this); }
@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 setVerbose(boolean verbose) { kmeans.setVerbose(verbose); } }
/** * 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; }