You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2022/01/25 00:38:02 UTC
[commons-math] 10/11: Add "ElkanKMeansPlusPlusClusterer" to example application.
This is an automated email from the ASF dual-hosted git repository.
erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
commit 9ce9f49d4a8d81a633882a8ea126de6c33207384
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Tue Jan 25 01:27:17 2022 +0100
Add "ElkanKMeansPlusPlusClusterer" to example application.
---
.../math4/examples/kmeans/image/StandAlone.java | 51 +++++++++++++++++++---
1 file changed, 44 insertions(+), 7 deletions(-)
diff --git a/commons-math-examples/examples-kmeans/image/src/main/java/org/apache/commons/math4/examples/kmeans/image/StandAlone.java b/commons-math-examples/examples-kmeans/image/src/main/java/org/apache/commons/math4/examples/kmeans/image/StandAlone.java
index 9f84a4b..f0081e7 100644
--- a/commons-math-examples/examples-kmeans/image/src/main/java/org/apache/commons/math4/examples/kmeans/image/StandAlone.java
+++ b/commons-math-examples/examples-kmeans/image/src/main/java/org/apache/commons/math4/examples/kmeans/image/StandAlone.java
@@ -18,6 +18,8 @@
package org.apache.commons.math4.examples.kmeans.image;
import java.util.concurrent.Callable;
+import java.time.Instant;
+import java.time.Duration;
import picocli.CommandLine;
import picocli.CommandLine.Option;
@@ -29,6 +31,7 @@ import org.apache.commons.math4.legacy.ml.distance.DistanceMeasure;
import org.apache.commons.math4.legacy.ml.distance.EuclideanDistance;
import org.apache.commons.math4.legacy.ml.clustering.Clusterer;
import org.apache.commons.math4.legacy.ml.clustering.KMeansPlusPlusClusterer;
+import org.apache.commons.math4.legacy.ml.clustering.ElkanKMeansPlusPlusClusterer;
/**
* Application class.
@@ -67,14 +70,48 @@ public final class StandAlone implements Callable<Void> {
final ImageData image = ImageData.load(inputFile);
final UniformRandomProvider rng = RandomSource.MWC_256.create();
final DistanceMeasure distance = new EuclideanDistance();
- final Clusterer<ImageData.PixelClusterable> algo =
- new KMeansPlusPlusClusterer<>(numClusters,
- maxIter,
- distance,
- rng);
- image.write(algo.cluster(image.getPixels()),
- outputPrefix + ".k_" + numClusters + ".kmeans.");
+
+ cluster(image,
+ new ElkanKMeansPlusPlusClusterer<ImageData.PixelClusterable>(numClusters,
+ maxIter,
+ distance,
+ rng),
+ "elkan");
+ cluster(image,
+ new KMeansPlusPlusClusterer<ImageData.PixelClusterable>(numClusters,
+ maxIter,
+ distance,
+ rng),
+ "kmeans");
return null;
}
+
+ /**
+ * Perform clustering and write results.
+ *
+ * @param image Input.
+ * @param algo Algorithm to do the clustering.
+ * @param id Identifier for output file name.
+ */
+ private void cluster(ImageData image,
+ Clusterer<ImageData.PixelClusterable> algo,
+ String id) {
+ final String dot = ".";
+ final String out = new StringBuilder()
+ .append(outputPrefix)
+ .append(dot)
+ .append("k_")
+ .append(numClusters)
+ .append(dot)
+ .append(id)
+ .append(dot)
+ .toString();
+
+ final Instant start = Instant.now();
+ image.write(algo.cluster(image.getPixels()), out);
+ //CHECKSTYLE: stop all
+ System.out.println("time=" + Duration.between(start, Instant.now()).toMillis());
+ //CHECKSTYLE: resume all
+ }
}