Search in sources :

Example 6 with SquaredEuclideanDistanceFunction

use of de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction in project elki by elki-project.

the class LinearScanEuclideanDistanceRangeQuery method linearScan.

/**
 * Main loop for linear scan,
 *
 * @param relation Data relation
 * @param iter Iterator
 * @param obj Query object
 * @param range Query radius
 * @param result Output data structure
 */
private void linearScan(Relation<? extends O> relation, DBIDIter iter, O obj, double range, ModifiableDoubleDBIDList result) {
    final SquaredEuclideanDistanceFunction squared = SquaredEuclideanDistanceFunction.STATIC;
    // Avoid a loss in numerical precision when using the squared radius:
    final double upper = range * 1.0000001;
    // This should be more precise, but slower:
    // upper = MathUtil.floatToDoubleUpper((float)range);
    final double sqrange = upper * upper;
    while (iter.valid()) {
        final double sqdistance = squared.distance(obj, relation.get(iter));
        if (sqdistance <= sqrange) {
            final double dist = FastMath.sqrt(sqdistance);
            if (dist <= range) {
                // double check, as we increased the radius above
                result.add(dist, iter);
            }
        }
        iter.advance();
    }
}
Also used : SquaredEuclideanDistanceFunction(de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction)

Example 7 with SquaredEuclideanDistanceFunction

use of de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction in project elki by elki-project.

the class KMeansSort method assignToNearestCluster.

/**
 * Reassign objects, but only if their bounds indicate it is necessary to do
 * so.
 *
 * @param relation Data
 * @param means Current means
 * @param clusters Current clusters
 * @param assignment Cluster assignment
 * @param varsum Variance sum counter
 * @param cdist Centroid distances
 * @param cnum Centroid nearest neighbors
 * @param diststat Distance statistics
 * @return true when the object was reassigned
 */
private boolean assignToNearestCluster(Relation<V> relation, double[][] means, List<ModifiableDBIDs> clusters, WritableIntegerDataStore assignment, double[] varsum, double[][] cdist, int[][] cnum, LongStatistic diststat) {
    assert (k == means.length);
    long dists = 0;
    boolean changed = false;
    // Reset all clusters
    Arrays.fill(varsum, 0.);
    for (ModifiableDBIDs cluster : clusters) {
        cluster.clear();
    }
    double mult = (distanceFunction instanceof SquaredEuclideanDistanceFunction) ? 4 : 2;
    for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
        final int cur = assignment.intValue(iditer), ini = cur >= 0 ? cur : 0;
        // Distance to current mean:
        V fv = relation.get(iditer);
        double mindist = distanceFunction.distance(fv, DoubleVector.wrap(means[ini]));
        ++dists;
        final double threshold = mult * mindist;
        int minIndex = ini;
        for (int i : cnum[ini]) {
            if (cdist[minIndex][i] >= threshold) {
                // All following can only be worse.
                break;
            }
            double dist = distanceFunction.distance(fv, DoubleVector.wrap(means[i]));
            ++dists;
            if (dist < mindist) {
                minIndex = i;
                mindist = dist;
            }
        }
        varsum[minIndex] += mindist;
        clusters.get(minIndex).add(iditer);
        changed |= assignment.putInt(iditer, minIndex) != minIndex;
    }
    // Increment distance computations counter.
    if (diststat != null) {
        diststat.increment(dists);
    }
    return changed;
}
Also used : SquaredEuclideanDistanceFunction(de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 8 with SquaredEuclideanDistanceFunction

use of de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction in project elki by elki-project.

the class KMeansCompare method assignToNearestCluster.

/**
 * Reassign objects, but only if their bounds indicate it is necessary to do
 * so.
 *
 * @param relation Data
 * @param means Current means
 * @param clusters Current clusters
 * @param assignment Cluster assignment
 * @param varsum Variance sum counter
 * @param cdist Centroid distances
 * @param diststat Distance statistics
 * @return true when the object was reassigned
 */
private boolean assignToNearestCluster(Relation<V> relation, double[][] means, List<ModifiableDBIDs> clusters, WritableIntegerDataStore assignment, double[] varsum, double[][] cdist, LongStatistic diststat) {
    assert (k == means.length);
    long dists = 0;
    boolean changed = false;
    // Reset all clusters
    Arrays.fill(varsum, 0.);
    for (ModifiableDBIDs cluster : clusters) {
        cluster.clear();
    }
    double mult = (distanceFunction instanceof SquaredEuclideanDistanceFunction) ? 4 : 2;
    for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
        final int cur = assignment.intValue(iditer), ini = cur >= 0 ? cur : 0;
        // Distance to current mean:
        V fv = relation.get(iditer);
        double mindist = distanceFunction.distance(fv, DoubleVector.wrap(means[ini]));
        ++dists;
        final double thresh = mult * mindist;
        int minIndex = ini;
        for (int i = 0; i < k; i++) {
            if (i == ini || cdist[minIndex][i] >= thresh) {
                // Compare pruning
                continue;
            }
            double dist = distanceFunction.distance(fv, DoubleVector.wrap(means[i]));
            ++dists;
            if (dist < mindist) {
                minIndex = i;
                mindist = dist;
            }
        }
        varsum[minIndex] += mindist;
        clusters.get(minIndex).add(iditer);
        changed |= assignment.putInt(iditer, minIndex) != minIndex;
    }
    // Increment distance computations counter.
    if (diststat != null) {
        diststat.increment(dists);
    }
    return changed;
}
Also used : SquaredEuclideanDistanceFunction(de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Aggregations

SquaredEuclideanDistanceFunction (de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction)8 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)5 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)2 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)2 KMeansLloyd (de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans.KMeansLloyd)1 RandomlyGeneratedInitialMeans (de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans.initialization.RandomlyGeneratedInitialMeans)1 KMeansModel (de.lmu.ifi.dbs.elki.data.model.KMeansModel)1 Database (de.lmu.ifi.dbs.elki.database.Database)1 StaticArrayDatabase (de.lmu.ifi.dbs.elki.database.StaticArrayDatabase)1 DBIDRange (de.lmu.ifi.dbs.elki.database.ids.DBIDRange)1 KNNHeap (de.lmu.ifi.dbs.elki.database.ids.KNNHeap)1 ArrayAdapterDatabaseConnection (de.lmu.ifi.dbs.elki.datasource.ArrayAdapterDatabaseConnection)1 DatabaseConnection (de.lmu.ifi.dbs.elki.datasource.DatabaseConnection)1 DoubleStatistic (de.lmu.ifi.dbs.elki.logging.statistics.DoubleStatistic)1 LongStatistic (de.lmu.ifi.dbs.elki.logging.statistics.LongStatistic)1 StringStatistic (de.lmu.ifi.dbs.elki.logging.statistics.StringStatistic)1 Centroid (de.lmu.ifi.dbs.elki.math.linearalgebra.Centroid)1 EvaluationResult (de.lmu.ifi.dbs.elki.result.EvaluationResult)1 MeasurementGroup (de.lmu.ifi.dbs.elki.result.EvaluationResult.MeasurementGroup)1