Search in sources :

Example 1 with NumberVectorDistanceFunction

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

the class BestOfMultipleKMeans method run.

@Override
public Clustering<M> run(Database database, Relation<V> relation) {
    if (!(innerkMeans.getDistanceFunction() instanceof PrimitiveDistanceFunction)) {
        throw new AbortException("K-Means results can only be evaluated for primitive distance functions, got: " + innerkMeans.getDistanceFunction().getClass());
    }
    @SuppressWarnings("unchecked") final NumberVectorDistanceFunction<? super NumberVector> df = (NumberVectorDistanceFunction<? super NumberVector>) innerkMeans.getDistanceFunction();
    Clustering<M> bestResult = null;
    double bestCost = Double.NaN;
    FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("K-means iterations", trials, LOG) : null;
    for (int i = 0; i < trials; i++) {
        Clustering<M> currentCandidate = innerkMeans.run(database, relation);
        double currentCost = qualityMeasure.quality(currentCandidate, df, relation);
        if (LOG.isVerbose()) {
            LOG.verbose("Cost of candidate " + i + ": " + currentCost);
        }
        if (qualityMeasure.isBetter(currentCost, bestCost)) {
            bestResult = currentCandidate;
            bestCost = currentCost;
        }
        LOG.incrementProcessed(prog);
    }
    LOG.ensureCompleted(prog);
    return bestResult;
}
Also used : NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) NumberVectorDistanceFunction(de.lmu.ifi.dbs.elki.distance.distancefunction.NumberVectorDistanceFunction) FiniteProgress(de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress) PrimitiveDistanceFunction(de.lmu.ifi.dbs.elki.distance.distancefunction.PrimitiveDistanceFunction) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Example 2 with NumberVectorDistanceFunction

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

the class SampleKMeansInitialization method chooseInitialMeans.

@Override
public <T extends V> double[][] chooseInitialMeans(Database database, Relation<T> relation, int k, NumberVectorDistanceFunction<? super T> distanceFunction) {
    final DBIDs sample = DBIDUtil.randomSample(relation.getDBIDs(), rate, rnd);
    // Ugly cast, sorry
    @SuppressWarnings("unchecked") Relation<V> rel = (Relation<V>) relation;
    // FIXME: This does not necessarily hold. Check and fail!
    if (!distanceFunction.getInputTypeRestriction().isAssignableFromType(TypeUtil.NUMBER_VECTOR_FIELD)) {
        LoggingUtil.warning("Initializing k-means with k-means using specialized distance functions MAY fail, if the initialization method does require a distance defined on arbitrary number vectors.");
    }
    @SuppressWarnings("unchecked") NumberVectorDistanceFunction<? super V> pdf = (NumberVectorDistanceFunction<? super V>) distanceFunction;
    ProxyView<V> proxyv = new ProxyView<>(sample, rel);
    ProxyDatabase proxydb = new ProxyDatabase(sample, proxyv);
    innerkMeans.setK(k);
    innerkMeans.setDistanceFunction(pdf);
    Clustering<?> clusters = innerkMeans.run(proxydb, proxyv);
    double[][] means = new double[clusters.getAllClusters().size()][];
    int i = 0;
    for (Cluster<?> cluster : clusters.getAllClusters()) {
        means[i++] = ModelUtil.getPrototype(cluster.getModel(), relation).toArray();
    }
    return means;
}
Also used : ProxyView(de.lmu.ifi.dbs.elki.database.relation.ProxyView) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) ProxyDatabase(de.lmu.ifi.dbs.elki.database.ProxyDatabase) Relation(de.lmu.ifi.dbs.elki.database.relation.Relation) NumberVectorDistanceFunction(de.lmu.ifi.dbs.elki.distance.distancefunction.NumberVectorDistanceFunction)

Aggregations

NumberVectorDistanceFunction (de.lmu.ifi.dbs.elki.distance.distancefunction.NumberVectorDistanceFunction)2 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)1 ProxyDatabase (de.lmu.ifi.dbs.elki.database.ProxyDatabase)1 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)1 ProxyView (de.lmu.ifi.dbs.elki.database.relation.ProxyView)1 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)1 PrimitiveDistanceFunction (de.lmu.ifi.dbs.elki.distance.distancefunction.PrimitiveDistanceFunction)1 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)1 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)1