Java Code Examples for org.apache.commons.math3.ml.distance.DistanceMeasure#compute()
The following examples show how to use
org.apache.commons.math3.ml.distance.DistanceMeasure#compute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapUtils.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Finds the neuron that best matches the given features. * * @param features Data. * @param neurons List of neurons to scan. If the list is empty * {@code null} will be returned. * @param distance Distance function. The neuron's features are * passed as the first argument to {@link DistanceMeasure#compute(double[],double[])}. * @return the neuron whose features are closest to the given data. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the size of the input is not compatible with the neurons features * size. */ public static Neuron findBest(double[] features, Iterable<Neuron> neurons, DistanceMeasure distance) { Neuron best = null; double min = Double.POSITIVE_INFINITY; for (final Neuron n : neurons) { final double d = distance.compute(n.getFeatures(), features); if (d < min) { min = d; best = n; } } return best; }
Example 2
Source File: MapUtils.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Finds the two neurons that best match the given features. * * @param features Data. * @param neurons List of neurons to scan. If the list is empty * {@code null} will be returned. * @param distance Distance function. The neuron's features are * passed as the first argument to {@link DistanceMeasure#compute(double[],double[])}. * @return the two neurons whose features are closest to the given data. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the size of the input is not compatible with the neurons features * size. */ public static Pair<Neuron, Neuron> findBestAndSecondBest(double[] features, Iterable<Neuron> neurons, DistanceMeasure distance) { Neuron[] best = { null, null }; double[] min = { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }; for (final Neuron n : neurons) { final double d = distance.compute(n.getFeatures(), features); if (d < min[0]) { // Replace second best with old best. min[1] = min[0]; best[1] = best[0]; // Store current as new best. min[0] = d; best[0] = n; } else if (d < min[1]) { // Replace old second best with current. min[1] = d; best[1] = n; } } return new Pair<Neuron, Neuron>(best[0], best[1]); }
Example 3
Source File: MapUtils.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Computes the quantization error. * The quantization error is the average distance between a feature vector * and its "best matching unit" (closest neuron). * * @param data Feature vectors. * @param neurons List of neurons to scan. * @param distance Distance function. * @return the error. * @throws NoDataException if {@code data} is empty. */ public static double computeQuantizationError(Iterable<double[]> data, Iterable<Neuron> neurons, DistanceMeasure distance) { double d = 0; int count = 0; for (double[] f : data) { ++count; d += distance.compute(f, findBest(f, neurons, distance).getFeatures()); } if (count == 0) { throw new NoDataException(); } return d / count; }
Example 4
Source File: MapUtils.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Finds the neuron that best matches the given features. * * @param features Data. * @param neurons List of neurons to scan. If the list is empty * {@code null} will be returned. * @param distance Distance function. The neuron's features are * passed as the first argument to {@link DistanceMeasure#compute(double[],double[])}. * @return the neuron whose features are closest to the given data. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the size of the input is not compatible with the neurons features * size. */ public static Neuron findBest(double[] features, Iterable<Neuron> neurons, DistanceMeasure distance) { Neuron best = null; double min = Double.POSITIVE_INFINITY; for (final Neuron n : neurons) { final double d = distance.compute(n.getFeatures(), features); if (d < min) { min = d; best = n; } } return best; }
Example 5
Source File: MapUtils.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Finds the two neurons that best match the given features. * * @param features Data. * @param neurons List of neurons to scan. If the list is empty * {@code null} will be returned. * @param distance Distance function. The neuron's features are * passed as the first argument to {@link DistanceMeasure#compute(double[],double[])}. * @return the two neurons whose features are closest to the given data. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the size of the input is not compatible with the neurons features * size. */ public static Pair<Neuron, Neuron> findBestAndSecondBest(double[] features, Iterable<Neuron> neurons, DistanceMeasure distance) { Neuron[] best = { null, null }; double[] min = { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }; for (final Neuron n : neurons) { final double d = distance.compute(n.getFeatures(), features); if (d < min[0]) { // Replace second best with old best. min[1] = min[0]; best[1] = best[0]; // Store current as new best. min[0] = d; best[0] = n; } else if (d < min[1]) { // Replace old second best with current. min[1] = d; best[1] = n; } } return new Pair<Neuron, Neuron>(best[0], best[1]); }
Example 6
Source File: MapUtils.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Computes the quantization error. * The quantization error is the average distance between a feature vector * and its "best matching unit" (closest neuron). * * @param data Feature vectors. * @param neurons List of neurons to scan. * @param distance Distance function. * @return the error. * @throws NoDataException if {@code data} is empty. */ public static double computeQuantizationError(Iterable<double[]> data, Iterable<Neuron> neurons, DistanceMeasure distance) { double d = 0; int count = 0; for (double[] f : data) { ++count; d += distance.compute(f, findBest(f, neurons, distance).getFeatures()); } if (count == 0) { throw new NoDataException(); } return d / count; }
Example 7
Source File: MapUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** * 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; }
Example 8
Source File: MapUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** * 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; }