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 2015/09/11 11:42:13 UTC
[01/10] [math] MATH-1267
Repository: commons-math
Updated Branches:
refs/heads/MATH_3_X 34b9fd042 -> 74515ba6a
MATH-1267
Helper for finding the grid coordinates of a "Neuron" in a "NeuronSquareMesh2D".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/a382b33a
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/a382b33a
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/a382b33a
Branch: refs/heads/MATH_3_X
Commit: a382b33abb45200ad67cb505e1023d1a55f2b314
Parents: 50c5eae
Author: Gilles <er...@apache.org>
Authored: Tue Sep 8 14:26:42 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Tue Sep 8 14:26:42 2015 +0200
----------------------------------------------------------------------
.../ml/neuralnet/twod/util/LocationFinder.java | 104 +++++++++++++++++++
.../neuralnet/twod/util/LocationFinderTest.java | 70 +++++++++++++
2 files changed, 174 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a382b33a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/LocationFinder.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/LocationFinder.java b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/LocationFinder.java
new file mode 100644
index 0000000..794af18
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/LocationFinder.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.ml.neuralnet.twod.util;
+
+import java.util.Map;
+import java.util.HashMap;
+import org.apache.commons.math3.ml.neuralnet.Neuron;
+import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
+import org.apache.commons.math3.exception.MathIllegalStateException;
+
+/**
+ * Helper class to find the grid coordinates of a neuron.
+ */
+public class LocationFinder {
+ /** Identifier to location mapping. */
+ private final Map<Long, Location> locations = new HashMap<Long, Location>();
+
+ /**
+ * Container holding a (row, column) pair.
+ */
+ public static class Location {
+ /** Row index. */
+ private final int row;
+ /** Column index. */
+ private final int column;
+
+ /**
+ * @param row Row index.
+ * @param column Column index.
+ */
+ public Location(int row,
+ int column) {
+ this.row = row;
+ this.column = column;
+ }
+
+ /**
+ * @return the row index.
+ */
+ public int getRow() {
+ return row;
+ }
+
+ /**
+ * @return the column index.
+ */
+ public int getColumn() {
+ return column;
+ }
+ }
+
+ /**
+ * Builds a finder to retrieve the locations of neurons that
+ * belong to the given {@code map}.
+ *
+ * @param map Map.
+ *
+ * @throws MathIllegalStateException if the network contains non-unique
+ * identifiers. This indicates an inconsistent state due to a bug in
+ * the construction code of the underlying
+ * {@link org.apache.commons.math3.ml.neuralnet.Network network}.
+ */
+ public LocationFinder(NeuronSquareMesh2D map) {
+ final int nR = map.getNumberOfRows();
+ final int nC = map.getNumberOfColumns();
+
+ for (int r = 0; r < nR; r++) {
+ for (int c = 0; c < nC; c++) {
+ final Long id = map.getNeuron(r, c).getIdentifier();
+ if (locations.get(id) != null) {
+ throw new MathIllegalStateException();
+ }
+ locations.put(id, new Location(r, c));
+ }
+ }
+ }
+
+ /**
+ * Retrieves a neuron's grid coordinates.
+ *
+ * @param n Neuron.
+ * @return the (row, column) coordinates of {@code n}, or {@code null}
+ * if no such neuron belongs to the {@link #LocationFinder(NeuronSquareMesh2D)
+ * map used to build this instance}.
+ */
+ public Location getLocation(Neuron n) {
+ return locations.get(n.getIdentifier());
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/a382b33a/src/test/java/org/apache/commons/math3/ml/neuralnet/twod/util/LocationFinderTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/ml/neuralnet/twod/util/LocationFinderTest.java b/src/test/java/org/apache/commons/math3/ml/neuralnet/twod/util/LocationFinderTest.java
new file mode 100644
index 0000000..dcadc47
--- /dev/null
+++ b/src/test/java/org/apache/commons/math3/ml/neuralnet/twod/util/LocationFinderTest.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.ml.neuralnet.twod.util;
+
+import org.apache.commons.math3.ml.neuralnet.Neuron;
+import org.apache.commons.math3.ml.neuralnet.Network;
+import org.apache.commons.math3.ml.neuralnet.FeatureInitializer;
+import org.apache.commons.math3.ml.neuralnet.FeatureInitializerFactory;
+import org.apache.commons.math3.ml.neuralnet.SquareNeighbourhood;
+import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test for {@link LocationFinder}.
+ */
+public class LocationFinderTest {
+ final FeatureInitializer init = FeatureInitializerFactory.uniform(0, 2);
+
+ /*
+ * Test assumes that the network is
+ *
+ * 0-----1
+ * | |
+ * | |
+ * 2-----3
+ */
+ @Test
+ public void test2x2Network() {
+ final FeatureInitializer[] initArray = { init };
+ final NeuronSquareMesh2D map = new NeuronSquareMesh2D(2, false,
+ 2, false,
+ SquareNeighbourhood.VON_NEUMANN,
+ initArray);
+ final LocationFinder finder = new LocationFinder(map);
+ final Network net = map.getNetwork();
+ LocationFinder.Location loc;
+
+ loc = finder.getLocation(net.getNeuron(0));
+ Assert.assertEquals(0, loc.getRow());
+ Assert.assertEquals(0, loc.getColumn());
+
+ loc = finder.getLocation(net.getNeuron(1));
+ Assert.assertEquals(0, loc.getRow());
+ Assert.assertEquals(1, loc.getColumn());
+
+ loc = finder.getLocation(net.getNeuron(2));
+ Assert.assertEquals(1, loc.getRow());
+ Assert.assertEquals(0, loc.getColumn());
+
+ loc = finder.getLocation(net.getNeuron(3));
+ Assert.assertEquals(1, loc.getRow());
+ Assert.assertEquals(1, loc.getColumn());
+ }
+}
[10/10] [math] Update of "changes.xml"
Posted by er...@apache.org.
Update of "changes.xml"
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/74515ba6
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/74515ba6
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/74515ba6
Branch: refs/heads/MATH_3_X
Commit: 74515ba6aa6a8c5a4b3c41214fced05d7320c654
Parents: 8025da1
Author: Gilles <er...@apache.org>
Authored: Fri Sep 11 11:36:48 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Sep 11 11:36:48 2015 +0200
----------------------------------------------------------------------
src/changes/changes.xml | 9 +++++++++
1 file changed, 9 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/74515ba6/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1698f0f..d2996b1 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,15 @@ If the output is not quite correct, check for invisible trailing spaces!
</properties>
<body>
<release version="3.6" date="XXXX-XX-XX" description="">
+ <action dev="erans" type="add" issue="MATH-1270">
+ Various SOFM visualizations (in package "o.a.c.m.ml.neuralnet.twod.util"):
+ Unified distance matrix, hit histogram, smoothed data histograms,
+ topographic error.
+ </action>
+ <action dev="erans" type="add" issue="MATH-1268">
+ New interfaces to be implemented by algorithms that visualizes properties
+ of a "NeuronSquareMesh2D" (package "o.a.c.m.ml.neuralnet.twod.util").
+ </action>
<action dev="luc" type="add" >
Reimplemented pow(double, double) in FastMath, for better accuracy in
integral power cases and trying to fix erroneous JIT optimization again.
[06/10] [math] MATH-1270
Posted by er...@apache.org.
MATH-1270
SOFM visualization: Topographic error.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/d7f6c8da
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/d7f6c8da
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/d7f6c8da
Branch: refs/heads/MATH_3_X
Commit: d7f6c8da9512af55c04a86984f56ab6f9e2da126
Parents: afac1f0
Author: Gilles <er...@apache.org>
Authored: Fri Sep 11 00:54:36 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Sep 11 00:54:36 2015 +0200
----------------------------------------------------------------------
.../twod/util/TopographicErrorHistogram.java | 90 ++++++++++++++++++++
1 file changed, 90 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/d7f6c8da/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/TopographicErrorHistogram.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/TopographicErrorHistogram.java b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/TopographicErrorHistogram.java
new file mode 100644
index 0000000..b337c0a
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/TopographicErrorHistogram.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.ml.neuralnet.twod.util;
+
+import org.apache.commons.math3.ml.neuralnet.MapUtils;
+import org.apache.commons.math3.ml.neuralnet.Neuron;
+import org.apache.commons.math3.ml.neuralnet.Network;
+import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
+import org.apache.commons.math3.ml.distance.DistanceMeasure;
+import org.apache.commons.math3.util.Pair;
+
+/**
+ * Computes the topographic error histogram.
+ * Each bin will contain the number of data for which the first and
+ * second best matching units are not adjacent in the map.
+ */
+public class TopographicErrorHistogram implements MapDataVisualization {
+ /** Distance. */
+ private final DistanceMeasure distance;
+ /** Whether to compute relative bin counts. */
+ private final boolean relativeCount;
+
+ /**
+ * @param relativeCount Whether to compute relative bin counts.
+ * If {@code true}, the data count in each bin will be divided by the total
+ * number of samples mapped to the neuron represented by that bin.
+ * @param distance Distance.
+ */
+ public TopographicErrorHistogram(boolean relativeCount,
+ DistanceMeasure distance) {
+ this.relativeCount = relativeCount;
+ this.distance = distance;
+ }
+
+ /** {@inheritDoc} */
+ public double[][] computeImage(NeuronSquareMesh2D map,
+ Iterable<double[]> data) {
+ final int nR = map.getNumberOfRows();
+ final int nC = map.getNumberOfColumns();
+
+ final Network net = map.getNetwork();
+ final LocationFinder finder = new LocationFinder(map);
+
+ // Hit bins.
+ final int[][] hit = new int[nR][nC];
+ // Error bins.
+ final double[][] error = new double[nR][nC];
+
+ for (double[] sample : data) {
+ final Pair<Neuron, Neuron> p = MapUtils.findBestAndSecondBest(sample, map, distance);
+ final Neuron best = p.getFirst();
+
+ final LocationFinder.Location loc = finder.getLocation(best);
+ final int row = loc.getRow();
+ final int col = loc.getColumn();
+ hit[row][col] += 1;
+
+ if (!net.getNeighbours(best).contains(p.getSecond())) {
+ // Increment count if first and second best matching units
+ // are not neighbours.
+ error[row][col] += 1;
+ }
+ }
+
+ if (relativeCount) {
+ for (int r = 0; r < nR; r++) {
+ for (int c = 0; c < nC; c++) {
+ error[r][c] /= hit[r][c];
+ }
+ }
+ }
+
+ return error;
+ }
+}
[09/10] [math] Merge branch 'MATH_3_X' of
https://git-wip-us.apache.org/repos/asf/commons-math into MATH_3_X
Posted by er...@apache.org.
Merge branch 'MATH_3_X' of https://git-wip-us.apache.org/repos/asf/commons-math into MATH_3_X
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/8025da17
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/8025da17
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/8025da17
Branch: refs/heads/MATH_3_X
Commit: 8025da1719dfc03094c8ce6fc3f0b4f14f7dc6af
Parents: d270d38 34b9fd0
Author: Gilles <er...@apache.org>
Authored: Fri Sep 11 11:26:32 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Sep 11 11:26:32 2015 +0200
----------------------------------------------------------------------
src/site/xdoc/userguide/analysis.xml | 44 ++++++++++++++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
[05/10] [math] MATH-1270
Posted by er...@apache.org.
MATH-1270
SOFM visualization: Hit histogram.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/afac1f05
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/afac1f05
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/afac1f05
Branch: refs/heads/MATH_3_X
Commit: afac1f05cd57907160ea60dddc6502770577f542
Parents: 5e28d11
Author: Gilles <er...@apache.org>
Authored: Fri Sep 11 00:53:50 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Sep 11 00:53:50 2015 +0200
----------------------------------------------------------------------
.../ml/neuralnet/twod/util/HitHistogram.java | 85 ++++++++++++++++++++
1 file changed, 85 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/afac1f05/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/HitHistogram.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/HitHistogram.java b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/HitHistogram.java
new file mode 100644
index 0000000..96b8c5d
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/HitHistogram.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.ml.neuralnet.twod.util;
+
+import org.apache.commons.math3.ml.neuralnet.MapUtils;
+import org.apache.commons.math3.ml.neuralnet.Neuron;
+import org.apache.commons.math3.ml.neuralnet.Network;
+import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
+import org.apache.commons.math3.ml.distance.DistanceMeasure;
+import org.apache.commons.math3.util.Pair;
+
+/**
+ * Computes the hit histogram.
+ * Each bin will contain the number of data for which the corresponding
+ * neuron is the best matching unit.
+ */
+public class HitHistogram implements MapDataVisualization {
+ /** Distance. */
+ private final DistanceMeasure distance;
+ /** Whether to compute relative bin counts. */
+ private final boolean normalizeCount;
+
+ /**
+ * @param relativeCount Whether to compute relative bin counts.
+ * If {@code true}, the data count in each bin will be divided by the total
+ * number of samples.
+ * @param distance Distance.
+ */
+ public HitHistogram(boolean normalizeCount,
+ DistanceMeasure distance) {
+ this.normalizeCount = normalizeCount;
+ this.distance = distance;
+ }
+
+ /** {@inheritDoc} */
+ public double[][] computeImage(NeuronSquareMesh2D map,
+ Iterable<double[]> data) {
+ final int nR = map.getNumberOfRows();
+ final int nC = map.getNumberOfColumns();
+
+ final Network net = map.getNetwork();
+ final LocationFinder finder = new LocationFinder(map);
+
+ // Totla number of samples.
+ int numSamples = 0;
+ // Hit bins.
+ final double[][] hit = new double[nR][nC];
+
+ for (double[] sample : data) {
+ final Neuron best = MapUtils.findBest(sample, map, distance);
+
+ final LocationFinder.Location loc = finder.getLocation(best);
+ final int row = loc.getRow();
+ final int col = loc.getColumn();
+ hit[row][col] += 1;
+
+ ++numSamples;
+ }
+
+ if (normalizeCount) {
+ for (int r = 0; r < nR; r++) {
+ for (int c = 0; c < nC; c++) {
+ hit[r][c] /= numSamples;
+ }
+ }
+ }
+
+ return hit;
+ }
+}
[03/10] [math] "package-info" file.
Posted by er...@apache.org.
"package-info" file.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/4ac5a9de
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/4ac5a9de
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/4ac5a9de
Branch: refs/heads/MATH_3_X
Commit: 4ac5a9dec01b83fa4873f2e86c0d482f6354cccf
Parents: cba459c
Author: Gilles <er...@apache.org>
Authored: Tue Sep 8 14:35:14 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Tue Sep 8 14:35:14 2015 +0200
----------------------------------------------------------------------
.../ml/neuralnet/twod/util/package-info.java | 22 ++++++++++++++++++++
1 file changed, 22 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/4ac5a9de/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/package-info.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/package-info.java b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/package-info.java
new file mode 100644
index 0000000..cd4aab0
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Utilities to visualize two-dimensional neural networks.
+ */
+
+package org.apache.commons.math3.ml.neuralnet.twod.util;
[02/10] [math] MATH-1268
Posted by er...@apache.org.
MATH-1268
API for algorithms that create some visualization of a 2D SOFM.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/cba459c0
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/cba459c0
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/cba459c0
Branch: refs/heads/MATH_3_X
Commit: cba459c0b51d6e33743a2039ffd4481ebc763cd7
Parents: a382b33
Author: Gilles <er...@apache.org>
Authored: Tue Sep 8 14:30:39 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Tue Sep 8 14:30:39 2015 +0200
----------------------------------------------------------------------
.../twod/util/MapDataVisualization.java | 37 ++++++++++++++++++++
.../neuralnet/twod/util/MapVisualization.java | 33 +++++++++++++++++
2 files changed, 70 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/cba459c0/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/MapDataVisualization.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/MapDataVisualization.java b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/MapDataVisualization.java
new file mode 100644
index 0000000..1570e8b
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/MapDataVisualization.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.ml.neuralnet.twod.util;
+
+import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
+
+/**
+ * Interface for algorithms that compute some metrics of the projection of
+ * data on a 2D-map.
+ */
+public interface MapDataVisualization {
+ /**
+ * Creates an image of the {@code data} metrics when represented by the
+ * {@code map}.
+ *
+ * @param map Map.
+ * @param data Data.
+ * @return a 2D-array (in row major order) representing the metrics.
+ */
+ double[][] computeImage(NeuronSquareMesh2D map,
+ Iterable<double[]> data);
+}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/cba459c0/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/MapVisualization.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/MapVisualization.java b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/MapVisualization.java
new file mode 100644
index 0000000..a4453f5
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/MapVisualization.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.ml.neuralnet.twod.util;
+
+import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
+
+/**
+ * Interface for algorithms that compute some property of a 2D-map.
+ */
+public interface MapVisualization {
+ /**
+ * Creates an image of the {@code map}.
+ *
+ * @param map Map.
+ * @return a 2D-array (in row major order) representing the property.
+ */
+ double[][] computeImage(NeuronSquareMesh2D map);
+}
[07/10] [math] MATH-1270
Posted by er...@apache.org.
MATH-1270
SOFM visualization: Smoothed data histogram.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/012c15ce
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/012c15ce
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/012c15ce
Branch: refs/heads/MATH_3_X
Commit: 012c15ce4d83817217430adb0b3750c92f07ab3e
Parents: d7f6c8d
Author: Gilles <er...@apache.org>
Authored: Fri Sep 11 00:55:00 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Sep 11 00:55:00 2015 +0200
----------------------------------------------------------------------
.../twod/util/SmoothedDataHistogram.java | 96 ++++++++++++++++++++
1 file changed, 96 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/012c15ce/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/SmoothedDataHistogram.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/SmoothedDataHistogram.java b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/SmoothedDataHistogram.java
new file mode 100644
index 0000000..c8a6d84
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/SmoothedDataHistogram.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.ml.neuralnet.twod.util;
+
+import org.apache.commons.math3.ml.neuralnet.MapUtils;
+import org.apache.commons.math3.ml.neuralnet.Neuron;
+import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
+import org.apache.commons.math3.ml.distance.DistanceMeasure;
+import org.apache.commons.math3.exception.NumberIsTooSmallException;
+
+/**
+ * Visualization of high-dimensional data projection on a 2D-map.
+ * The method is described in
+ * <quote>
+ * <em>Using Smoothed Data Histograms for Cluster Visualization in Self-Organizing Maps</em>
+ * <br>
+ * by Elias Pampalk, Andreas Rauber and Dieter Merkl.
+ * </quote>
+ */
+public class SmoothedDataHistogram implements MapDataVisualization {
+ /** Smoothing parameter. */
+ private final int smoothingBins;
+ /** Distance. */
+ private final DistanceMeasure distance;
+ /** Normalization factor. */
+ private final double membershipNormalization;
+
+ /**
+ * @param smoothingBins Number of bins.
+ * @param distance Distance.
+ */
+ public SmoothedDataHistogram(int smoothingBins,
+ DistanceMeasure distance) {
+ this.smoothingBins = smoothingBins;
+ this.distance = distance;
+
+ double sum = 0;
+ for (int i = 0; i < smoothingBins; i++) {
+ sum += smoothingBins - i;
+ }
+
+ this.membershipNormalization = 1d / sum;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws NumberIsTooSmallException if the size of the {@code map}
+ * is smaller than the number of {@link SmoothedDataHistogram(int,DistanceMeasure)
+ * smoothing bins}.
+ */
+ public double[][] computeImage(NeuronSquareMesh2D map,
+ Iterable<double[]> data) {
+ final int nR = map.getNumberOfRows();
+ final int nC = map.getNumberOfColumns();
+
+ final int mapSize = nR * nC;
+ if (mapSize < smoothingBins) {
+ throw new NumberIsTooSmallException(mapSize, smoothingBins, true);
+ }
+
+ final LocationFinder finder = new LocationFinder(map);
+
+ // Histogram bins.
+ final double[][] histo = new double[nR][nC];
+
+ for (double[] sample : data) {
+ final Neuron[] sorted = MapUtils.sort(sample,
+ map.getNetwork(),
+ distance);
+ for (int i = 0; i < smoothingBins; i++) {
+ final LocationFinder.Location loc = finder.getLocation(sorted[i]);
+ final int row = loc.getRow();
+ final int col = loc.getColumn();
+ histo[row][col] += (smoothingBins - i) * membershipNormalization;
+ }
+ }
+
+ return histo;
+ }
+}
[04/10] [math] MATH-1270
Posted by er...@apache.org.
MATH-1270
SOFM visualization: Unified distance matrix.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/5e28d11b
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/5e28d11b
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/5e28d11b
Branch: refs/heads/MATH_3_X
Commit: 5e28d11b17c3e1aae2d344cef70aee18ef05a43b
Parents: 4ac5a9d
Author: Gilles <er...@apache.org>
Authored: Fri Sep 11 00:49:55 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Sep 11 00:49:55 2015 +0200
----------------------------------------------------------------------
.../twod/util/UnifiedDistanceMatrix.java | 202 +++++++++++++++++++
1 file changed, 202 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/5e28d11b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/UnifiedDistanceMatrix.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/UnifiedDistanceMatrix.java b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/UnifiedDistanceMatrix.java
new file mode 100644
index 0000000..3f56260
--- /dev/null
+++ b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/util/UnifiedDistanceMatrix.java
@@ -0,0 +1,202 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.ml.neuralnet.twod.util;
+
+import java.util.Collection;
+import org.apache.commons.math3.ml.neuralnet.Neuron;
+import org.apache.commons.math3.ml.neuralnet.Network;
+import org.apache.commons.math3.ml.neuralnet.twod.NeuronSquareMesh2D;
+import org.apache.commons.math3.ml.distance.DistanceMeasure;
+
+/**
+ * <a href="http://en.wikipedia.org/wiki/U-Matrix">U-Matrix</a>
+ * visualization of high-dimensional data projection.
+ */
+public class UnifiedDistanceMatrix implements MapVisualization {
+ /** Whether to show distance between each pair of neighbouring units. */
+ private final boolean individualDistances;
+ /** Distance. */
+ private final DistanceMeasure distance;
+
+ /**
+ * @param boolean individualDistances. If {@code true}, the 8 individual
+ * inter-units distances will be {@link #computeImage(NeuronSquareMesh2D)
+ * computed}. They will be stored in additional pixels around each of
+ * the original units of the 2D-map. The value zero will be stored in the
+ * pixel corresponding to the location of a unit of the 2D-map.
+ * If {@code false}, only the average distance between a unit and all its
+ * neighbours will be computed (and stored in the pixel corresponding to
+ * that unit of the 2D-map). In that case, the number of neighbours taken
+ * into account depends on the network's
+ * {@link org.apache.commons.math3.ml.neuralnet.SquareNeighbourhood
+ * neighbourhood type}.
+ * @param distance Distance.
+ */
+ public UnifiedDistanceMatrix(boolean individualDistances,
+ DistanceMeasure distance) {
+ this.individualDistances = individualDistances;
+ this.distance = distance;
+ }
+
+ /** {@inheritDoc} */
+ public double[][] computeImage(NeuronSquareMesh2D map) {
+ if (individualDistances) {
+ return individualDistances(map);
+ } else {
+ return averageDistances(map);
+ }
+ }
+
+ /**
+ * Computes the distances between a unit of the map and its
+ * neighbours.
+ * The image will contain more pixels than the number of neurons
+ * in the given {@code map} because each neuron has 8 neighbours.
+ * The value zero will be stored in the pixels corresponding to
+ * the location of a map unit.
+ *
+ * @param map Map.
+ * @return an image representing the individual distances.
+ */
+ private double[][] individualDistances(NeuronSquareMesh2D map) {
+ final int numRows = map.getNumberOfRows();
+ final int numCols = map.getNumberOfColumns();
+
+ final double[][] uMatrix = new double[numRows * 2 + 1][numCols * 2 + 1];
+
+ for (int i = 0; i < numRows; i++) {
+ // Current unit's row index in result image.
+ final int iR = 2 * i + 1;
+
+ for (int j = 0; j < numCols; j++) {
+ // Current unit's column index in result image.
+ final int jR = 2 * j + 1;
+
+ final double[] current = map.getNeuron(i, j).getFeatures();
+ Neuron neighbour;
+
+ // Top-left neighbour.
+ neighbour = map.getNeuron(i, j,
+ NeuronSquareMesh2D.HorizontalDirection.LEFT,
+ NeuronSquareMesh2D.VerticalDirection.UP);
+ if (neighbour != null) {
+ uMatrix[iR - 1][jR - 1] = distance.compute(current,
+ neighbour.getFeatures());
+ }
+
+ // Top-center neighbour.
+ neighbour = map.getNeuron(i, j,
+ NeuronSquareMesh2D.HorizontalDirection.CENTER,
+ NeuronSquareMesh2D.VerticalDirection.UP);
+ if (neighbour != null) {
+ uMatrix[iR - 1][jR] = distance.compute(current,
+ neighbour.getFeatures());
+ }
+
+ // Top-right neighbour.
+ neighbour = map.getNeuron(i, j,
+ NeuronSquareMesh2D.HorizontalDirection.RIGHT,
+ NeuronSquareMesh2D.VerticalDirection.UP);
+ if (neighbour != null) {
+ uMatrix[iR - 1][jR + 1] = distance.compute(current,
+ neighbour.getFeatures());
+ }
+
+ // Left neighbour.
+ neighbour = map.getNeuron(i, j,
+ NeuronSquareMesh2D.HorizontalDirection.LEFT,
+ NeuronSquareMesh2D.VerticalDirection.CENTER);
+ if (neighbour != null) {
+ uMatrix[iR][jR - 1] = distance.compute(current,
+ neighbour.getFeatures());
+ }
+
+ // Right neighbour.
+ neighbour = map.getNeuron(i, j,
+ NeuronSquareMesh2D.HorizontalDirection.RIGHT,
+ NeuronSquareMesh2D.VerticalDirection.CENTER);
+ if (neighbour != null) {
+ uMatrix[iR][jR + 1] = distance.compute(current,
+ neighbour.getFeatures());
+ }
+
+ // Bottom-left neighbour.
+ neighbour = map.getNeuron(i, j,
+ NeuronSquareMesh2D.HorizontalDirection.LEFT,
+ NeuronSquareMesh2D.VerticalDirection.DOWN);
+ if (neighbour != null) {
+ uMatrix[iR + 1][jR - 1] = distance.compute(current,
+ neighbour.getFeatures());
+ }
+
+ // Bottom-center neighbour.
+ neighbour = map.getNeuron(i, j,
+ NeuronSquareMesh2D.HorizontalDirection.CENTER,
+ NeuronSquareMesh2D.VerticalDirection.DOWN);
+ if (neighbour != null) {
+ uMatrix[iR + 1][jR] = distance.compute(current,
+ neighbour.getFeatures());
+ }
+
+ // Bottom-right neighbour.
+ neighbour = map.getNeuron(i, j,
+ NeuronSquareMesh2D.HorizontalDirection.RIGHT,
+ NeuronSquareMesh2D.VerticalDirection.DOWN);
+ if (neighbour != null) {
+ uMatrix[iR + 1][jR + 1] = distance.compute(current,
+ neighbour.getFeatures());
+ }
+ }
+ }
+
+ return uMatrix;
+ }
+
+ /**
+ * Computes the distances between a unit of the map and its neighbours.
+ *
+ * @param map Map.
+ * @return an image representing the average distances.
+ */
+ private double[][] averageDistances(NeuronSquareMesh2D map) {
+ final int numRows = map.getNumberOfRows();
+ final int numCols = map.getNumberOfColumns();
+ final double[][] uMatrix = new double[numRows][numCols];
+
+ final Network net = map.getNetwork();
+
+ for (int i = 0; i < numRows; i++) {
+ for (int j = 0; j < numCols; j++) {
+ final Neuron neuron = map.getNeuron(i, j);
+ final Collection<Neuron> neighbours = net.getNeighbours(neuron);
+ final double[] features = neuron.getFeatures();
+
+ double d = 0;
+ int count = 0;
+ for (Neuron n : neighbours) {
+ ++count;
+ d += distance.compute(features, n.getFeatures());
+ }
+
+ uMatrix[i][j] = d / count;
+ }
+ }
+
+ return uMatrix;
+ }
+}
[08/10] [math] Merge branch 'MATH_3_X' of
https://git-wip-us.apache.org/repos/asf/commons-math into MATH_3_X
Posted by er...@apache.org.
Merge branch 'MATH_3_X' of https://git-wip-us.apache.org/repos/asf/commons-math into MATH_3_X
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/d270d387
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/d270d387
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/d270d387
Branch: refs/heads/MATH_3_X
Commit: d270d387ce6cebf6f4aae5322ce85810049a82dc
Parents: 012c15c bfc2d49
Author: Gilles <er...@apache.org>
Authored: Fri Sep 11 00:55:44 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Sep 11 00:55:44 2015 +0200
----------------------------------------------------------------------
src/changes/changes.xml | 7 +
.../commons/math3/ode/events/EventFilter.java | 26 +-
.../org/apache/commons/math3/util/FastMath.java | 423 +++++++++++--------
.../distribution/GammaDistributionTest.java | 2 +-
.../scalar/noderiv/BOBYQAOptimizerTest.java | 2 +-
.../direct/BOBYQAOptimizerTest.java | 2 +-
.../apache/commons/math3/util/FastMathTest.java | 59 ++-
7 files changed, 321 insertions(+), 200 deletions(-)
----------------------------------------------------------------------