@Test(expected = AIFHError.class) public void testNoObservations() { final List<BasicData> list = new ArrayList<BasicData>(); final KMeans kmeans = new KMeans(3); kmeans.initForgy(list); }
public void initForgy(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); this.clusters.clear(); final Set<Integer> usedObservations = new HashSet<Integer>(); for (int i = 0; i < this.k; i++) { final Cluster cluster = new Cluster(dimensions); this.clusters.add(cluster); int observationIndex = -1; while (observationIndex == -1) { observationIndex = this.randomGeneration.nextInt(theObservations.size()); if (usedObservations.contains(observationIndex)) { observationIndex = -1; } } final double[] observation = theObservations.get(observationIndex).getInput(); System.arraycopy(observation, 0, cluster.getCenter(), 0, dimensions); usedObservations.add(observationIndex); } for (final BasicData observation : theObservations) { final Cluster cluster = findNearestCluster(observation.getInput()); cluster.getObservations().add(observation); } updateStep(); }
KMeans { public void initForgy(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); this.clusters.clear(); final Set<Integer> usedObservations = new HashSet<Integer>(); for (int i = 0; i < this.k; i++) { final Cluster cluster = new Cluster(dimensions); this.clusters.add(cluster); int observationIndex = -1; while (observationIndex == -1) { observationIndex = this.randomGeneration.nextInt(theObservations.size()); if (usedObservations.contains(observationIndex)) { observationIndex = -1; } } final double[] observation = theObservations.get(observationIndex).getInput(); System.arraycopy(observation, 0, cluster.getCenter(), 0, dimensions); usedObservations.add(observationIndex); } for (final BasicData observation : theObservations) { final Cluster cluster = findNearestCluster(observation.getInput()); cluster.getObservations().add(observation); } updateStep(); } }
KMeans { public void initForgy(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); this.clusters.clear(); final Set<Integer> usedObservations = new HashSet<Integer>(); for (int i = 0; i < this.k; i++) { final Cluster cluster = new Cluster(dimensions); this.clusters.add(cluster); int observationIndex = -1; while (observationIndex == -1) { observationIndex = this.randomGeneration.nextInt(theObservations.size()); if (usedObservations.contains(observationIndex)) { observationIndex = -1; } } final double[] observation = theObservations.get(observationIndex).getInput(); System.arraycopy(observation, 0, cluster.getCenter(), 0, dimensions); usedObservations.add(observationIndex); } for (final BasicData observation : theObservations) { final Cluster cluster = findNearestCluster(observation.getInput()); cluster.getObservations().add(observation); } updateStep(); } KMeans(final int theK); }
KMeans { public void initForgy(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); this.clusters.clear(); final Set<Integer> usedObservations = new HashSet<Integer>(); for (int i = 0; i < this.k; i++) { final Cluster cluster = new Cluster(dimensions); this.clusters.add(cluster); int observationIndex = -1; while (observationIndex == -1) { observationIndex = this.randomGeneration.nextInt(theObservations.size()); if (usedObservations.contains(observationIndex)) { observationIndex = -1; } } final double[] observation = theObservations.get(observationIndex).getInput(); System.arraycopy(observation, 0, cluster.getCenter(), 0, dimensions); usedObservations.add(observationIndex); } for (final BasicData observation : theObservations) { final Cluster cluster = findNearestCluster(observation.getInput()); cluster.getObservations().add(observation); } 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 initForgy(final List<BasicData> theObservations) { final int dimensions = findDimensions(theObservations); this.clusters.clear(); final Set<Integer> usedObservations = new HashSet<Integer>(); for (int i = 0; i < this.k; i++) { final Cluster cluster = new Cluster(dimensions); this.clusters.add(cluster); int observationIndex = -1; while (observationIndex == -1) { observationIndex = this.randomGeneration.nextInt(theObservations.size()); if (usedObservations.contains(observationIndex)) { observationIndex = -1; } } final double[] observation = theObservations.get(observationIndex).getInput(); System.arraycopy(observation, 0, cluster.getCenter(), 0, dimensions); usedObservations.add(observationIndex); } for (final BasicData observation : theObservations) { final Cluster cluster = findNearestCluster(observation.getInput()); cluster.getObservations().add(observation); } 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();  }