Skip to content

Commit 0020bc5

Browse files
author
Gilles Sadowski
committed
Code examples are compilable again.
1 parent 0235de6 commit 0020bc5

12 files changed

Lines changed: 192 additions & 216 deletions

src/userguide/java/org/apache/commons/math4/userguide/ClusterAlgorithmComparison.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@
4141
import org.apache.commons.statistics.distribution.NormalDistribution;
4242
import org.apache.commons.geometry.euclidean.twod.Vector2D;
4343

44-
import org.apache.commons.math4.ml.clustering.Cluster;
45-
import org.apache.commons.math4.ml.clustering.Clusterable;
46-
import org.apache.commons.math4.ml.clustering.Clusterer;
47-
import org.apache.commons.math4.ml.clustering.DBSCANClusterer;
48-
import org.apache.commons.math4.ml.clustering.DoublePoint;
49-
import org.apache.commons.math4.ml.clustering.FuzzyKMeansClusterer;
50-
import org.apache.commons.math4.ml.clustering.KMeansPlusPlusClusterer;
51-
import org.apache.commons.math4.random.SobolSequenceGenerator;
52-
import org.apache.commons.math4.util.FastMath;
53-
import org.apache.commons.math4.util.Pair;
44+
import org.apache.commons.math4.legacy.ml.clustering.Cluster;
45+
import org.apache.commons.math4.legacy.ml.clustering.Clusterable;
46+
import org.apache.commons.math4.legacy.ml.clustering.Clusterer;
47+
import org.apache.commons.math4.legacy.ml.clustering.DBSCANClusterer;
48+
import org.apache.commons.math4.legacy.ml.clustering.DoublePoint;
49+
import org.apache.commons.math4.legacy.ml.clustering.FuzzyKMeansClusterer;
50+
import org.apache.commons.math4.legacy.ml.clustering.KMeansPlusPlusClusterer;
51+
import org.apache.commons.math4.legacy.random.SobolSequenceGenerator;
52+
import org.apache.commons.math4.core.jdkmath.JdkMath;
53+
import org.apache.commons.math4.legacy.core.Pair;
5454
import org.apache.commons.math4.userguide.ExampleUtils.ExampleFrame;
5555

5656
/**
@@ -69,13 +69,13 @@ public static List<Vector2D> makeCircles(int samples,
6969
throw new IllegalArgumentException();
7070
}
7171

72-
ContinuousDistribution.Sampler dist = new NormalDistribution(0.0, noise).createSampler(rng);
72+
ContinuousDistribution.Sampler dist = NormalDistribution.of(0.0, noise).createSampler(rng);
7373

7474
List<Vector2D> points = new ArrayList<>();
75-
double range = 2.0 * FastMath.PI;
75+
double range = 2.0 * JdkMath.PI;
7676
double step = range / (samples / 2.0 + 1);
7777
for (double angle = 0; angle < range; angle += step) {
78-
Vector2D outerCircle = Vector2D.of(FastMath.cos(angle), FastMath.sin(angle));
78+
Vector2D outerCircle = Vector2D.of(JdkMath.cos(angle), JdkMath.sin(angle));
7979
Vector2D innerCircle = outerCircle.multiply(factor);
8080

8181
points.add(outerCircle.add(generateNoiseVector(dist)));
@@ -93,22 +93,22 @@ public static List<Vector2D> makeMoons(int samples,
9393
boolean shuffle,
9494
double noise,
9595
UniformRandomProvider rng) {
96-
ContinuousDistribution.Sampler dist = new NormalDistribution(0.0, noise).createSampler(rng);
96+
ContinuousDistribution.Sampler dist = NormalDistribution.of(0.0, noise).createSampler(rng);
9797

9898
int nSamplesOut = samples / 2;
9999
int nSamplesIn = samples - nSamplesOut;
100100

101101
List<Vector2D> points = new ArrayList<>();
102-
double range = FastMath.PI;
102+
double range = JdkMath.PI;
103103
double step = range / (nSamplesOut / 2.0);
104104
for (double angle = 0; angle < range; angle += step) {
105-
Vector2D outerCircle = Vector2D.of(FastMath.cos(angle), FastMath.sin(angle));
105+
Vector2D outerCircle = Vector2D.of(JdkMath.cos(angle), JdkMath.sin(angle));
106106
points.add(outerCircle.add(generateNoiseVector(dist)));
107107
}
108108

109109
step = range / (nSamplesIn / 2.0);
110110
for (double angle = 0; angle < range; angle += step) {
111-
Vector2D innerCircle = Vector2D.of(1 - FastMath.cos(angle), 1 - FastMath.sin(angle) - 0.5);
111+
Vector2D innerCircle = Vector2D.of(1 - JdkMath.cos(angle), 1 - JdkMath.sin(angle) - 0.5);
112112
points.add(innerCircle.add(generateNoiseVector(dist)));
113113
}
114114

@@ -126,8 +126,8 @@ public static List<Vector2D> makeBlobs(int samples,
126126
double max,
127127
boolean shuffle,
128128
UniformRandomProvider rng) {
129-
ContinuousDistribution.Sampler uniform = new UniformContinuousDistribution(min, max).createSampler(rng);
130-
ContinuousDistribution.Sampler gauss = new NormalDistribution(0.0, clusterStd).createSampler(rng);
129+
ContinuousDistribution.Sampler uniform = UniformContinuousDistribution.of(min, max).createSampler(rng);
130+
ContinuousDistribution.Sampler gauss = NormalDistribution.of(0.0, clusterStd).createSampler(rng);
131131

132132
Vector2D[] centerPoints = new Vector2D[centers];
133133
for (int i = 0; i < centers; i++) {
@@ -161,7 +161,7 @@ public static List<Vector2D> makeRandom(int samples) {
161161
generator.skipTo(999999);
162162
List<Vector2D> points = new ArrayList<>();
163163
for (double i = 0; i < samples; i++) {
164-
double[] vector = generator.nextVector();
164+
double[] vector = generator.get();
165165
vector[0] = vector[0] * 2 - 1;
166166
vector[1] = vector[1] * 2 - 1;
167167
Vector2D point = Vector2D.of(vector);
@@ -205,33 +205,28 @@ public Display() {
205205

206206
final long seed = RandomSource.createLong(); // Random seed.
207207
UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, seed);
208-
List<List<DoublePoint>> datasets = new ArrayList<List<DoublePoint>>();
208+
List<List<DoublePoint>> datasets = new ArrayList<>();
209209

210210
datasets.add(normalize(makeCircles(nSamples, true, 0.04, 0.5, rng), -1, 1, -1, 1));
211211
datasets.add(normalize(makeMoons(nSamples, true, 0.04, rng), -1, 2, -1, 1));
212212
datasets.add(normalize(makeBlobs(nSamples, 3, 1.0, -10, 10, true, rng), -12, 12, -12, 12));
213213
datasets.add(normalize(makeRandom(nSamples), -1, 1, -1, 1));
214214

215-
List<Pair<String, Clusterer<DoublePoint>>> algorithms = new ArrayList<>();
215+
List<Pair<String, ? extends Clusterer<DoublePoint>>> algorithms = new ArrayList<>();
216216

217-
algorithms.add(new Pair<String, Clusterer<DoublePoint>>("KMeans\n(k=2)",
218-
new KMeansPlusPlusClusterer<>(2)));
219-
algorithms.add(new Pair<String, Clusterer<DoublePoint>>("KMeans\n(k=3)",
220-
new KMeansPlusPlusClusterer<>(3)));
221-
algorithms.add(new Pair<String, Clusterer<DoublePoint>>("FuzzyKMeans\n(k=3, fuzzy=2)",
222-
new FuzzyKMeansClusterer<>(3, 2)));
223-
algorithms.add(new Pair<String, Clusterer<DoublePoint>>("FuzzyKMeans\n(k=3, fuzzy=10)",
224-
new FuzzyKMeansClusterer<>(3, 10)));
225-
algorithms.add(new Pair<String, Clusterer<DoublePoint>>("DBSCAN\n(eps=.1, min=3)",
226-
new DBSCANClusterer<>(0.1, 3)));
217+
algorithms.add(new Pair<>("KMeans\n(k=2)", new KMeansPlusPlusClusterer<DoublePoint>(2)));
218+
algorithms.add(new Pair<>("KMeans\n(k=3)", new KMeansPlusPlusClusterer<DoublePoint>(3)));
219+
algorithms.add(new Pair<>("FuzzyKMeans\n(k=3, fuzzy=2)", new FuzzyKMeansClusterer<DoublePoint>(3, 2)));
220+
algorithms.add(new Pair<>("FuzzyKMeans\n(k=3, fuzzy=10)", new FuzzyKMeansClusterer<DoublePoint>(3, 10)));
221+
algorithms.add(new Pair<>("DBSCAN\n(eps=.1, min=3)", new DBSCANClusterer<DoublePoint>(0.1, 3)));
227222

228223
GridBagConstraints c = new GridBagConstraints();
229224
c.fill = GridBagConstraints.VERTICAL;
230225
c.gridx = 0;
231226
c.gridy = 0;
232227
c.insets = new Insets(2, 2, 2, 2);
233228

234-
for (Pair<String, Clusterer<DoublePoint>> pair : algorithms) {
229+
for (Pair<String, ? extends Clusterer<DoublePoint>> pair : algorithms) {
235230
JLabel text = new JLabel("<html><body>" + pair.getFirst().replace("\n", "<br>"));
236231
add(text, c);
237232
c.gridx++;
@@ -240,7 +235,7 @@ public Display() {
240235

241236
for (List<DoublePoint> dataset : datasets) {
242237
c.gridx = 0;
243-
for (Pair<String, Clusterer<DoublePoint>> pair : algorithms) {
238+
for (Pair<String, ? extends Clusterer<DoublePoint>> pair : algorithms) {
244239
long start = System.currentTimeMillis();
245240
List<? extends Cluster<DoublePoint>> clusters = pair.getSecond().cluster(dataset);
246241
long end = System.currentTimeMillis();

0 commit comments

Comments
 (0)