MATH-1548: Remove methods redundant with functionality defined in "NeuronSquareMesh2D".
This commit is contained in:
parent
28e5b802fe
commit
ed4817c730
|
@ -17,13 +17,9 @@
|
||||||
|
|
||||||
package org.apache.commons.math4.ml.neuralnet;
|
package org.apache.commons.math4.ml.neuralnet;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.math4.exception.NoDataException;
|
import org.apache.commons.math4.exception.NoDataException;
|
||||||
import org.apache.commons.math4.ml.distance.DistanceMeasure;
|
import org.apache.commons.math4.ml.distance.DistanceMeasure;
|
||||||
import org.apache.commons.math4.ml.neuralnet.twod.NeuronSquareMesh2D;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utilities for network maps.
|
* Utilities for network maps.
|
||||||
|
@ -36,87 +32,6 @@ public class MapUtils {
|
||||||
*/
|
*/
|
||||||
private MapUtils() {}
|
private MapUtils() {}
|
||||||
|
|
||||||
/**
|
|
||||||
* Computes the <a href="http://en.wikipedia.org/wiki/U-Matrix">
|
|
||||||
* U-matrix</a> of a two-dimensional map.
|
|
||||||
*
|
|
||||||
* @param map Network.
|
|
||||||
* @param distance Function to use for computing the average
|
|
||||||
* distance from a neuron to its neighbours.
|
|
||||||
* @return the matrix of average distances.
|
|
||||||
*/
|
|
||||||
public static double[][] computeU(NeuronSquareMesh2D map,
|
|
||||||
DistanceMeasure distance) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Computes the "hit" histogram of a two-dimensional map.
|
|
||||||
*
|
|
||||||
* @param data Feature vectors.
|
|
||||||
* @param map Network.
|
|
||||||
* @param distance Function to use for determining the best matching unit.
|
|
||||||
* @return the number of hits for each neuron in the map.
|
|
||||||
*/
|
|
||||||
public static int[][] computeHitHistogram(Iterable<double[]> data,
|
|
||||||
NeuronSquareMesh2D map,
|
|
||||||
DistanceMeasure distance) {
|
|
||||||
final HashMap<Neuron, Integer> hit = new HashMap<>();
|
|
||||||
final MapRanking rank = new MapRanking(map.getNetwork(), distance);
|
|
||||||
|
|
||||||
for (double[] f : data) {
|
|
||||||
final Neuron best = rank.rank(f, 1).get(0);
|
|
||||||
final Integer count = hit.get(best);
|
|
||||||
if (count == null) {
|
|
||||||
hit.put(best, 1);
|
|
||||||
} else {
|
|
||||||
hit.put(best, count + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy the histogram data into a 2D map.
|
|
||||||
final int numRows = map.getNumberOfRows();
|
|
||||||
final int numCols = map.getNumberOfColumns();
|
|
||||||
final int[][] histo = new int[numRows][numCols];
|
|
||||||
|
|
||||||
for (int i = 0; i < numRows; i++) {
|
|
||||||
for (int j = 0; j < numCols; j++) {
|
|
||||||
final Neuron neuron = map.getNeuron(i, j);
|
|
||||||
final Integer count = hit.get(neuron);
|
|
||||||
if (count == null) {
|
|
||||||
histo[i][j] = 0;
|
|
||||||
} else {
|
|
||||||
histo[i][j] = count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return histo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the quantization error.
|
* Computes the quantization error.
|
||||||
* The quantization error is the average distance between a feature vector
|
* The quantization error is the average distance between a feature vector
|
||||||
|
|
Loading…
Reference in New Issue