Search in sources :

Example 11 with EvaluationResult

use of de.lmu.ifi.dbs.elki.result.EvaluationResult in project elki by elki-project.

the class EvaluateSquaredErrors method evaluateClustering.

/**
 * Evaluate a single clustering.
 *
 * @param db Database
 * @param rel Data relation
 * @param c Clustering
 * @return ssq
 */
public double evaluateClustering(Database db, Relation<? extends NumberVector> rel, Clustering<?> c) {
    boolean square = !distance.isSquared();
    int ignorednoise = 0;
    List<? extends Cluster<?>> clusters = c.getAllClusters();
    double ssq = 0, sum = 0;
    for (Cluster<?> cluster : clusters) {
        if (cluster.size() <= 1 || cluster.isNoise()) {
            switch(noiseOption) {
                case IGNORE_NOISE:
                    ignorednoise += cluster.size();
                    continue;
                case TREAT_NOISE_AS_SINGLETONS:
                    continue;
                case MERGE_NOISE:
                    // Treat as cluster below:
                    break;
            }
        }
        NumberVector center = ModelUtil.getPrototypeOrCentroid(cluster.getModel(), rel, cluster.getIDs());
        for (DBIDIter it1 = cluster.getIDs().iter(); it1.valid(); it1.advance()) {
            final double d = distance.distance(center, rel.get(it1));
            sum += d;
            ssq += square ? d * d : d;
        }
    }
    final int div = Math.max(1, rel.size() - ignorednoise);
    if (LOG.isStatistics()) {
        LOG.statistics(new DoubleStatistic(key + ".mean", sum / div));
        LOG.statistics(new DoubleStatistic(key + ".ssq", ssq));
        LOG.statistics(new DoubleStatistic(key + ".rmsd", FastMath.sqrt(ssq / div)));
    }
    EvaluationResult ev = EvaluationResult.findOrCreate(db.getHierarchy(), c, "Internal Clustering Evaluation", "internal evaluation");
    MeasurementGroup g = ev.findOrCreateGroup("Distance-based Evaluation");
    g.addMeasure("Mean distance", sum / div, 0., Double.POSITIVE_INFINITY, true);
    g.addMeasure("Sum of Squares", ssq, 0., Double.POSITIVE_INFINITY, true);
    g.addMeasure("RMSD", FastMath.sqrt(ssq / div), 0., Double.POSITIVE_INFINITY, true);
    db.getHierarchy().add(c, ev);
    return ssq;
}
Also used : DoubleStatistic(de.lmu.ifi.dbs.elki.logging.statistics.DoubleStatistic) NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) MeasurementGroup(de.lmu.ifi.dbs.elki.result.EvaluationResult.MeasurementGroup) EvaluationResult(de.lmu.ifi.dbs.elki.result.EvaluationResult) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Aggregations

EvaluationResult (de.lmu.ifi.dbs.elki.result.EvaluationResult)11 MeasurementGroup (de.lmu.ifi.dbs.elki.result.EvaluationResult.MeasurementGroup)9 DoubleStatistic (de.lmu.ifi.dbs.elki.logging.statistics.DoubleStatistic)8 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)7 LongStatistic (de.lmu.ifi.dbs.elki.logging.statistics.LongStatistic)7 StringStatistic (de.lmu.ifi.dbs.elki.logging.statistics.StringStatistic)7 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)6 ArrayDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs)2 DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)2 MeanVariance (de.lmu.ifi.dbs.elki.math.MeanVariance)2 Centroid (de.lmu.ifi.dbs.elki.math.linearalgebra.Centroid)2 SpatialComparable (de.lmu.ifi.dbs.elki.data.spatial.SpatialComparable)1 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)1 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)1 SquaredEuclideanDistanceFunction (de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction)1 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)1 Mean (de.lmu.ifi.dbs.elki.math.Mean)1 DoubleHeap (de.lmu.ifi.dbs.elki.utilities.datastructures.heap.DoubleHeap)1 DoubleMaxHeap (de.lmu.ifi.dbs.elki.utilities.datastructures.heap.DoubleMaxHeap)1 DoubleMinHeap (de.lmu.ifi.dbs.elki.utilities.datastructures.heap.DoubleMinHeap)1