@Test(expected = AIFHError.class) public void testTooManyClusters() { final KMeans kmeans = new KMeans(13); kmeans.initRandom(getDataSet()); }
public void initRandom(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); for (int i = 0; i < this.k; i++) { this.clusters.add(new Cluster(dimensions)); } for (final BasicData observation : theObservations) { final int clusterIndex = this.randomGeneration.nextInt(this.k); final Cluster cluster = this.clusters.get(clusterIndex); cluster.getObservations().add(observation); } for (final Cluster cluster : this.clusters) { if (cluster.getObservations().size() == 0) { boolean done = false; while (!done) { final int sourceIndex = this.randomGeneration.nextInt(this.k); final Cluster source = this.clusters.get(sourceIndex); if (source != cluster && source.getObservations().size() > 1) { final int sourceObservationIndex = this.randomGeneration.nextInt(source.getObservations().size()); final BasicData sourceObservation = source.getObservations().get(sourceObservationIndex); source.getObservations().remove(sourceObservationIndex); cluster.getObservations().add(sourceObservation); done = true; } } } } updateStep(); }
KMeans { public void initRandom(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); for (int i = 0; i < this.k; i++) { this.clusters.add(new Cluster(dimensions)); } for (final BasicData observation : theObservations) { final int clusterIndex = this.randomGeneration.nextInt(this.k); final Cluster cluster = this.clusters.get(clusterIndex); cluster.getObservations().add(observation); } for (final Cluster cluster : this.clusters) { if (cluster.getObservations().size() == 0) { boolean done = false; while (!done) { final int sourceIndex = this.randomGeneration.nextInt(this.k); final Cluster source = this.clusters.get(sourceIndex); if (source != cluster && source.getObservations().size() > 1) { final int sourceObservationIndex = this.randomGeneration.nextInt(source.getObservations().size()); final BasicData sourceObservation = source.getObservations().get(sourceObservationIndex); source.getObservations().remove(sourceObservationIndex); cluster.getObservations().add(sourceObservation); done = true; } } } } updateStep(); } }
KMeans { public void initRandom(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); for (int i = 0; i < this.k; i++) { this.clusters.add(new Cluster(dimensions)); } for (final BasicData observation : theObservations) { final int clusterIndex = this.randomGeneration.nextInt(this.k); final Cluster cluster = this.clusters.get(clusterIndex); cluster.getObservations().add(observation); } for (final Cluster cluster : this.clusters) { if (cluster.getObservations().size() == 0) { boolean done = false; while (!done) { final int sourceIndex = this.randomGeneration.nextInt(this.k); final Cluster source = this.clusters.get(sourceIndex); if (source != cluster && source.getObservations().size() > 1) { final int sourceObservationIndex = this.randomGeneration.nextInt(source.getObservations().size()); final BasicData sourceObservation = source.getObservations().get(sourceObservationIndex); source.getObservations().remove(sourceObservationIndex); cluster.getObservations().add(sourceObservation); done = true; } } } } updateStep(); } KMeans(final int theK); }
KMeans { public void initRandom(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); for (int i = 0; i < this.k; i++) { this.clusters.add(new Cluster(dimensions)); } for (final BasicData observation : theObservations) { final int clusterIndex = this.randomGeneration.nextInt(this.k); final Cluster cluster = this.clusters.get(clusterIndex); cluster.getObservations().add(observation); } for (final Cluster cluster : this.clusters) { if (cluster.getObservations().size() == 0) { boolean done = false; while (!done) { final int sourceIndex = this.randomGeneration.nextInt(this.k); final Cluster source = this.clusters.get(sourceIndex); if (source != cluster && source.getObservations().size() > 1) { final int sourceObservationIndex = this.randomGeneration.nextInt(source.getObservations().size()); final BasicData sourceObservation = source.getObservations().get(sourceObservationIndex); source.getObservations().remove(sourceObservationIndex); cluster.getObservations().add(sourceObservation); done = true; } } } } updateStep(); } KMeans(final int theK); void initRandom(final List<BasicData> theObservations); void initForgy(final List<BasicData> theObservations); Cluster findNearestCluster(final double[] observation); boolean iteration(); int iteration(final int maxIterations); GenerateRandom getRandomGeneration(); void setRandomGeneration(final GenerateRandom randomGeneration); CalculateDistance getDistanceMetric(); void setDistanceMetric(final CalculateDistance distanceMetric); int getK(); List<Cluster> getClusters(); }
KMeans { public void initRandom(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); for (int i = 0; i < this.k; i++) { this.clusters.add(new Cluster(dimensions)); } for (final BasicData observation : theObservations) { final int clusterIndex = this.randomGeneration.nextInt(this.k); final Cluster cluster = this.clusters.get(clusterIndex); cluster.getObservations().add(observation); } for (final Cluster cluster : this.clusters) { if (cluster.getObservations().size() == 0) { boolean done = false; while (!done) { final int sourceIndex = this.randomGeneration.nextInt(this.k); final Cluster source = this.clusters.get(sourceIndex); if (source != cluster && source.getObservations().size() > 1) { final int sourceObservationIndex = this.randomGeneration.nextInt(source.getObservations().size()); final BasicData sourceObservation = source.getObservations().get(sourceObservationIndex); source.getObservations().remove(sourceObservationIndex); cluster.getObservations().add(sourceObservation); done = true; } } } } updateStep(); } KMeans(final int theK); void initRandom(final List<BasicData> theObservations); void initForgy(final List<BasicData> theObservations); Cluster findNearestCluster(final double[] observation); boolean iteration(); int iteration(final int maxIterations); GenerateRandom getRandomGeneration(); void setRandomGeneration(final GenerateRandom randomGeneration); CalculateDistance getDistanceMetric(); void setDistanceMetric(final CalculateDistance distanceMetric); int getK(); List<Cluster> getClusters();  }