/** * 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; }
/** * Calculates the distance between two {@link Clusterable} instances * with the configured {@link DistanceMeasure}. * * @param p1 the first clusterable * @param p2 the second clusterable * @return the distance between the two clusterables */ protected double distance(final Clusterable p1, final Clusterable p2) { return measure.compute(p1.getPoint(), p2.getPoint()); }
/** * Calculates the distance between two {@link Clusterable} instances * with the configured {@link DistanceMeasure}. * * @param p1 the first clusterable * @param p2 the second clusterable * @return the distance between the two clusterables */ protected double distance(final Clusterable p1, final Clusterable p2) { return measure.compute(p1.getPoint(), p2.getPoint()); }
final double d = distance.compute(n.getFeatures(), features); list.add(new PairNeuronDouble(n, d));
Double.POSITIVE_INFINITY }; for (final Neuron n : neurons) { final double d = distance.compute(n.getFeatures(), features); if (d < min[0]) {
/** * 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; }
NeuronSquareMesh2D.VerticalDirection.CENTER); if (neighbour != null) { uMatrix[iR][jR + 1] = distance.compute(current, neighbour.getFeatures()); NeuronSquareMesh2D.VerticalDirection.DOWN); if (neighbour != null) { uMatrix[iR + 1][jR] = distance.compute(current, neighbour.getFeatures()); distance.compute(current.getFeatures(), bottomRight.getFeatures()); final double right2Bottom = (right == null || bottom == null) ? 0 : distance.compute(right.getFeatures(), bottom.getFeatures());
for (Neuron n : neighbours) { ++count; d += distance.compute(features, n.getFeatures());
for (Neuron n : neighbours) { ++count; d += distance.compute(features, n.getFeatures());
final int col = loc.getColumn(); hit[row][col] += 1; error[row][col] += distance.compute(sample, best.getFeatures());
/** * Calculates the distance between two {@link Clusterable} instances * with the configured {@link DistanceMeasure}. * * @param p1 the first clusterable * @param p2 the second clusterable * @return the distance between the two clusterables */ protected double distance(final Clusterable p1, final Clusterable p2) { return measure.compute(p1.getPoint(), p2.getPoint()); }
/** * 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; }
/** * Calculates the distance between two {@link Clusterable} instances * with the configured {@link DistanceMeasure}. * * @param p1 the first clusterable * @param p2 the second clusterable * @return the distance between the two clusterables */ protected double distance(final Clusterable p1, final Clusterable p2) { return measure.compute(p1.getPoint(), p2.getPoint()); }
/** * Calculates the distance between two {@link Clusterable} instances * with the configured {@link DistanceMeasure}. * * @param p1 the first clusterable * @param p2 the second clusterable * @return the distance between the two clusterables */ protected double distance(final Clusterable p1, final Clusterable p2) { return measure.compute(p1.getPoint(), p2.getPoint()); }
/** * Calculates the distance between two {@link Clusterable} instances * with the configured {@link DistanceMeasure}. * * @param p1 the first clusterable * @param p2 the second clusterable * @return the distance between the two clusterables */ protected double distance(final Clusterable p1, final Clusterable p2) { return measure.compute(p1.getPoint(), p2.getPoint()); }
/** * 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; }
private Matrix distance(DistanceMeasure distanceMeasure, Matrix matrix) { double[][] data = matrix.getData(); Array2DRowRealMatrix realMatrix = new Array2DRowRealMatrix(data, false); realMatrix = (Array2DRowRealMatrix)realMatrix.transpose(); data = realMatrix.getDataRef(); double[][] distanceMatrix = new double[data.length][data.length]; for(int i=0; i<data.length; i++) { double[] row = data[i]; for(int j=0; j<data.length; j++) { double[] row2 = data[j]; double dist = distanceMeasure.compute(row, row2); distanceMatrix[i][j] = dist; } } return new Matrix(distanceMatrix); } }
for (Neuron n : neighbours) { ++count; d += distance.compute(features, n.getFeatures());
final int col = loc.getColumn(); hit[row][col] += 1; error[row][col] += distance.compute(sample, best.getFeatures());
for (Neuron n : neighbours) { ++count; d += distance.compute(features, n.getFeatures());