org.apache.commons.math3.ml.distance.DistanceMeasure Java Examples

The following examples show how to use org.apache.commons.math3.ml.distance.DistanceMeasure. 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: FuzzyKMeansClusterer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance of a FuzzyKMeansClusterer.
 *
 * @param k the number of clusters to split the data into
 * @param fuzziness the fuzziness factor, must be > 1.0
 * @param maxIterations the maximum number of iterations to run the algorithm for.
 *   If negative, no maximum will be used.
 * @param measure the distance measure to use
 * @param epsilon the convergence criteria (default is 1e-3)
 * @param random random generator to use for choosing initial centers
 * @throws NumberIsTooSmallException if {@code fuzziness <= 1.0}
 */
public FuzzyKMeansClusterer(final int k, final double fuzziness,
                            final int maxIterations, final DistanceMeasure measure,
                            final double epsilon, final RandomGenerator random)
        throws NumberIsTooSmallException {

    super(measure);

    if (fuzziness <= 1.0d) {
        throw new NumberIsTooSmallException(fuzziness, 1.0, false);
    }
    this.k = k;
    this.fuzziness = fuzziness;
    this.maxIterations = maxIterations;
    this.epsilon = epsilon;
    this.random = random;

    this.membershipMatrix = null;
    this.points = null;
    this.clusters = null;
}
 
Example #2
Source File: FuzzyKMeansClusterer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance of a FuzzyKMeansClusterer.
 *
 * @param k the number of clusters to split the data into
 * @param fuzziness the fuzziness factor, must be &gt; 1.0
 * @param maxIterations the maximum number of iterations to run the algorithm for.
 *   If negative, no maximum will be used.
 * @param measure the distance measure to use
 * @param epsilon the convergence criteria (default is 1e-3)
 * @param random random generator to use for choosing initial centers
 * @throws NumberIsTooSmallException if {@code fuzziness <= 1.0}
 */
public FuzzyKMeansClusterer(final int k, final double fuzziness,
                            final int maxIterations, final DistanceMeasure measure,
                            final double epsilon, final RandomGenerator random)
        throws NumberIsTooSmallException {

    super(measure);

    if (fuzziness <= 1.0d) {
        throw new NumberIsTooSmallException(fuzziness, 1.0, false);
    }
    this.k = k;
    this.fuzziness = fuzziness;
    this.maxIterations = maxIterations;
    this.epsilon = epsilon;
    this.random = random;

    this.membershipMatrix = null;
    this.points = null;
    this.clusters = null;
}
 
Example #3
Source File: MapUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #4
Source File: FuzzyKMeansClusterer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance of a FuzzyKMeansClusterer.
 *
 * @param k the number of clusters to split the data into
 * @param fuzziness the fuzziness factor, must be &gt; 1.0
 * @param maxIterations the maximum number of iterations to run the algorithm for.
 *   If negative, no maximum will be used.
 * @param measure the distance measure to use
 * @param epsilon the convergence criteria (default is 1e-3)
 * @param random random generator to use for choosing initial centers
 * @throws NumberIsTooSmallException if {@code fuzziness <= 1.0}
 */
public FuzzyKMeansClusterer(final int k, final double fuzziness,
                            final int maxIterations, final DistanceMeasure measure,
                            final double epsilon, final RandomGenerator random)
        throws NumberIsTooSmallException {

    super(measure);

    if (fuzziness <= 1.0d) {
        throw new NumberIsTooSmallException(fuzziness, 1.0, false);
    }
    this.k = k;
    this.fuzziness = fuzziness;
    this.maxIterations = maxIterations;
    this.epsilon = epsilon;
    this.random = random;

    this.membershipMatrix = null;
    this.points = null;
    this.clusters = null;
}
 
Example #5
Source File: MapUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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: FuzzyKMeansClusterer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance of a FuzzyKMeansClusterer.
 *
 * @param k the number of clusters to split the data into
 * @param fuzziness the fuzziness factor, must be &gt; 1.0
 * @param maxIterations the maximum number of iterations to run the algorithm for.
 *   If negative, no maximum will be used.
 * @param measure the distance measure to use
 * @param epsilon the convergence criteria (default is 1e-3)
 * @param random random generator to use for choosing initial centers
 * @throws NumberIsTooSmallException if {@code fuzziness <= 1.0}
 */
public FuzzyKMeansClusterer(final int k, final double fuzziness,
                            final int maxIterations, final DistanceMeasure measure,
                            final double epsilon, final RandomGenerator random)
        throws NumberIsTooSmallException {

    super(measure);

    if (fuzziness <= 1.0d) {
        throw new NumberIsTooSmallException(fuzziness, 1.0, false);
    }
    this.k = k;
    this.fuzziness = fuzziness;
    this.maxIterations = maxIterations;
    this.epsilon = epsilon;
    this.random = random;

    this.membershipMatrix = null;
    this.points = null;
    this.clusters = null;
}
 
Example #7
Source File: MapUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #8
Source File: KnnRegressionEvaluator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public KnnRegressionTuple(Matrix observations,
                          double[] outcomes,
                          int k,
                          DistanceMeasure distanceMeasure,
                          Map<?,?> map,
                          boolean scale,
                          boolean robust,
                          boolean bivariate) {
  super(map);
  this.observations = observations;
  this.outcomes = outcomes;
  this.k = k;
  this.distanceMeasure = distanceMeasure;
  this.scale = scale;
  this.robust = robust;
  this.bivariate = bivariate;
}
 
Example #9
Source File: MapUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the topographic error.
 * The topographic error is the proportion of data for which first and
 * second best matching units are not adjacent in the map.
 *
 * @param data Feature vectors.
 * @param net Network.
 * @param distance Distance function.
 * @return the error.
 * @throws NoDataException if {@code data} is empty.
 */
public static double computeTopographicError(Iterable<double[]> data,
                                             Network net,
                                             DistanceMeasure distance) {
    int notAdjacentCount = 0;
    int count = 0;
    for (double[] f : data) {
        ++count;
        final Pair<Neuron, Neuron> p = findBestAndSecondBest(f, net, distance);
        if (!net.getNeighbours(p.getFirst()).contains(p.getSecond())) {
            // Increment count if first and second best matching units
            // are not neighbours.
            ++notAdjacentCount;
        }
    }

    if (count == 0) {
        throw new NoDataException();
    }

    return ((double) notAdjacentCount) / count;
}
 
Example #10
Source File: MapUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #11
Source File: MapUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #12
Source File: MapUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #13
Source File: MapUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the topographic error.
 * The topographic error is the proportion of data for which first and
 * second best matching units are not adjacent in the map.
 *
 * @param data Feature vectors.
 * @param net Network.
 * @param distance Distance function.
 * @return the error.
 * @throws NoDataException if {@code data} is empty.
 */
public static double computeTopographicError(Iterable<double[]> data,
                                             Network net,
                                             DistanceMeasure distance) {
    int notAdjacentCount = 0;
    int count = 0;
    for (double[] f : data) {
        ++count;
        final Pair<Neuron, Neuron> p = findBestAndSecondBest(f, net, distance);
        if (!net.getNeighbours(p.getFirst()).contains(p.getSecond())) {
            // Increment count if first and second best matching units
            // are not neighbours.
            ++notAdjacentCount;
        }
    }

    if (count == 0) {
        throw new NoDataException();
    }

    return ((double) notAdjacentCount) / count;
}
 
Example #14
Source File: KohonenUpdateAction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param distance Distance function.
 * @param learningFactor Learning factor update function.
 * @param neighbourhoodSize Neighbourhood size update function.
 */
public KohonenUpdateAction(DistanceMeasure distance,
                           LearningFactorFunction learningFactor,
                           NeighbourhoodSizeFunction neighbourhoodSize) {
    this.distance = distance;
    this.learningFactor = learningFactor;
    this.neighbourhoodSize = neighbourhoodSize;
}
 
Example #15
Source File: DBSCANClusterer.java    From egads with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new instance of a DBSCANClusterer.
 *
 * @param eps maximum radius of the neighborhood to be considered
 * @param minPts minimum number of points needed for a cluster
 * @param measure the distance measure to use
 * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0}
 */
public DBSCANClusterer(final double eps, final int minPts, final DistanceMeasure measure)
    throws NotPositiveException {
    super(measure);
 
    if (eps < 0.0d) {
        throw new NotPositiveException(eps);
    }
    if (minPts < 0) {
        throw new NotPositiveException(minPts);
    }
    this.eps = eps;
    this.minPts = minPts;
}
 
Example #16
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build a clusterer.
 *
 * @param k the number of clusters to split the data into
 * @param maxIterations the maximum number of iterations to run the algorithm for.
 *   If negative, no maximum will be used.
 * @param measure the distance measure to use
 * @param random random generator to use for choosing initial centers
 * @param emptyStrategy strategy to use for handling empty clusters that
 * may appear during algorithm iterations
 */
public KMeansPlusPlusClusterer(final int k, final int maxIterations,
                               final DistanceMeasure measure,
                               final RandomGenerator random,
                               final EmptyClusterStrategy emptyStrategy) {
    super(measure);
    this.k             = k;
    this.maxIterations = maxIterations;
    this.random        = random;
    this.emptyStrategy = emptyStrategy;
}
 
Example #17
Source File: FuzzyKMeansClustererTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetters() {
    final DistanceMeasure measure = new CanberraDistance();
    final RandomGenerator random = new JDKRandomGenerator();
    final FuzzyKMeansClusterer<DoublePoint> clusterer =
            new FuzzyKMeansClusterer<DoublePoint>(3, 2.0, 100, measure, 1e-6, random);

    Assert.assertEquals(3, clusterer.getK());
    Assert.assertEquals(2.0, clusterer.getFuzziness(), 1e-6);
    Assert.assertEquals(100, clusterer.getMaxIterations());
    Assert.assertEquals(1e-6, clusterer.getEpsilon(), 1e-12);
    Assert.assertThat(clusterer.getDistanceMeasure(), CoreMatchers.is(measure));
    Assert.assertThat(clusterer.getRandomGenerator(), CoreMatchers.is(random));
}
 
Example #18
Source File: DBSCANClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of a DBSCANClusterer.
 *
 * @param eps maximum radius of the neighborhood to be considered
 * @param minPts minimum number of points needed for a cluster
 * @param measure the distance measure to use
 * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0}
 */
public DBSCANClusterer(final double eps, final int minPts, final DistanceMeasure measure)
    throws NotPositiveException {
    super(measure);

    if (eps < 0.0d) {
        throw new NotPositiveException(eps);
    }
    if (minPts < 0) {
        throw new NotPositiveException(minPts);
    }
    this.eps = eps;
    this.minPts = minPts;
}
 
Example #19
Source File: FuzzyKMeansClustererTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetters() {
    final DistanceMeasure measure = new CanberraDistance();
    final RandomGenerator random = new JDKRandomGenerator();
    final FuzzyKMeansClusterer<DoublePoint> clusterer =
            new FuzzyKMeansClusterer<DoublePoint>(3, 2.0, 100, measure, 1e-6, random);

    Assert.assertEquals(3, clusterer.getK());
    Assert.assertEquals(2.0, clusterer.getFuzziness(), 1e-6);
    Assert.assertEquals(100, clusterer.getMaxIterations());
    Assert.assertEquals(1e-6, clusterer.getEpsilon(), 1e-12);
    Assert.assertThat(clusterer.getDistanceMeasure(), CoreMatchers.is(measure));
    Assert.assertThat(clusterer.getRandomGenerator(), CoreMatchers.is(random));
}
 
Example #20
Source File: DBSCANClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of a DBSCANClusterer.
 *
 * @param eps maximum radius of the neighborhood to be considered
 * @param minPts minimum number of points needed for a cluster
 * @param measure the distance measure to use
 * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0}
 */
public DBSCANClusterer(final double eps, final int minPts, final DistanceMeasure measure)
    throws NotPositiveException {
    super(measure);

    if (eps < 0.0d) {
        throw new NotPositiveException(eps);
    }
    if (minPts < 0) {
        throw new NotPositiveException(minPts);
    }
    this.eps = eps;
    this.minPts = minPts;
}
 
Example #21
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build a clusterer.
 *
 * @param k the number of clusters to split the data into
 * @param maxIterations the maximum number of iterations to run the algorithm for.
 *   If negative, no maximum will be used.
 * @param measure the distance measure to use
 * @param random random generator to use for choosing initial centers
 * @param emptyStrategy strategy to use for handling empty clusters that
 * may appear during algorithm iterations
 */
public KMeansPlusPlusClusterer(final int k, final int maxIterations,
                               final DistanceMeasure measure,
                               final RandomGenerator random,
                               final EmptyClusterStrategy emptyStrategy) {
    super(measure);
    this.k             = k;
    this.maxIterations = maxIterations;
    this.random        = random;
    this.emptyStrategy = emptyStrategy;
}
 
Example #22
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build a clusterer.
 *
 * @param k the number of clusters to split the data into
 * @param maxIterations the maximum number of iterations to run the algorithm for.
 *   If negative, no maximum will be used.
 * @param measure the distance measure to use
 * @param random random generator to use for choosing initial centers
 * @param emptyStrategy strategy to use for handling empty clusters that
 * may appear during algorithm iterations
 */
public KMeansPlusPlusClusterer(final int k, final int maxIterations,
                               final DistanceMeasure measure,
                               final RandomGenerator random,
                               final EmptyClusterStrategy emptyStrategy) {
    super(measure);
    this.k             = k;
    this.maxIterations = maxIterations;
    this.random        = random;
    this.emptyStrategy = emptyStrategy;
}
 
Example #23
Source File: MapUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #24
Source File: FuzzyKMeansClustererTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetters() {
    final DistanceMeasure measure = new CanberraDistance();
    final RandomGenerator random = new JDKRandomGenerator();
    final FuzzyKMeansClusterer<DoublePoint> clusterer =
            new FuzzyKMeansClusterer<DoublePoint>(3, 2.0, 100, measure, 1e-6, random);

    Assert.assertEquals(3, clusterer.getK());
    Assert.assertEquals(2.0, clusterer.getFuzziness(), 1e-6);
    Assert.assertEquals(100, clusterer.getMaxIterations());
    Assert.assertEquals(1e-6, clusterer.getEpsilon(), 1e-12);
    Assert.assertThat(clusterer.getDistanceMeasure(), CoreMatchers.is(measure));
    Assert.assertThat(clusterer.getRandomGenerator(), CoreMatchers.is(random));
}
 
Example #25
Source File: DBSCANClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of a DBSCANClusterer.
 *
 * @param eps maximum radius of the neighborhood to be considered
 * @param minPts minimum number of points needed for a cluster
 * @param measure the distance measure to use
 * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0}
 */
public DBSCANClusterer(final double eps, final int minPts, final DistanceMeasure measure)
    throws NotPositiveException {
    super(measure);

    if (eps < 0.0d) {
        throw new NotPositiveException(eps);
    }
    if (minPts < 0) {
        throw new NotPositiveException(minPts);
    }
    this.eps = eps;
    this.minPts = minPts;
}
 
Example #26
Source File: KohonenUpdateAction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param distance Distance function.
 * @param learningFactor Learning factor update function.
 * @param neighbourhoodSize Neighbourhood size update function.
 */
public KohonenUpdateAction(DistanceMeasure distance,
                           LearningFactorFunction learningFactor,
                           NeighbourhoodSizeFunction neighbourhoodSize) {
    this.distance = distance;
    this.learningFactor = learningFactor;
    this.neighbourhoodSize = neighbourhoodSize;
}
 
Example #27
Source File: MapUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #28
Source File: FuzzyKMeansClustererTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetters() {
    final DistanceMeasure measure = new CanberraDistance();
    final RandomGenerator random = new JDKRandomGenerator();
    final FuzzyKMeansClusterer<DoublePoint> clusterer =
            new FuzzyKMeansClusterer<DoublePoint>(3, 2.0, 100, measure, 1e-6, random);

    Assert.assertEquals(3, clusterer.getK());
    Assert.assertEquals(2.0, clusterer.getFuzziness(), 1e-6);
    Assert.assertEquals(100, clusterer.getMaxIterations());
    Assert.assertEquals(1e-6, clusterer.getEpsilon(), 1e-12);
    Assert.assertThat(clusterer.getDistanceMeasure(), CoreMatchers.is(measure));
    Assert.assertThat(clusterer.getRandomGenerator(), CoreMatchers.is(random));
}
 
Example #29
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build a clusterer.
 *
 * @param k the number of clusters to split the data into
 * @param maxIterations the maximum number of iterations to run the algorithm for.
 *   If negative, no maximum will be used.
 * @param measure the distance measure to use
 * @param random random generator to use for choosing initial centers
 * @param emptyStrategy strategy to use for handling empty clusters that
 * may appear during algorithm iterations
 */
public KMeansPlusPlusClusterer(final int k, final int maxIterations,
                               final DistanceMeasure measure,
                               final RandomGenerator random,
                               final EmptyClusterStrategy emptyStrategy) {
    super(measure);
    this.k             = k;
    this.maxIterations = maxIterations;
    this.random        = random;
    this.emptyStrategy = emptyStrategy;
}
 
Example #30
Source File: DBSCANClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of a DBSCANClusterer.
 *
 * @param eps maximum radius of the neighborhood to be considered
 * @param minPts minimum number of points needed for a cluster
 * @param measure the distance measure to use
 * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0}
 */
public DBSCANClusterer(final double eps, final int minPts, final DistanceMeasure measure)
    throws NotPositiveException {
    super(measure);

    if (eps < 0.0d) {
        throw new NotPositiveException(eps);
    }
    if (minPts < 0) {
        throw new NotPositiveException(minPts);
    }
    this.eps = eps;
    this.minPts = minPts;
}