Search in sources :

Example 1 with ProxyView

use of de.lmu.ifi.dbs.elki.database.relation.ProxyView in project elki by elki-project.

the class CTLuGLSBackwardSearchAlgorithm method run.

/**
 * Run the algorithm
 *
 * @param database Database to process
 * @param relationx Spatial relation
 * @param relationy Attribute relation
 * @return Algorithm result
 */
public OutlierResult run(Database database, Relation<V> relationx, Relation<? extends NumberVector> relationy) {
    WritableDoubleDataStore scores = DataStoreUtil.makeDoubleStorage(relationx.getDBIDs(), DataStoreFactory.HINT_STATIC);
    DoubleMinMax mm = new DoubleMinMax(0.0, 0.0);
    // Outlier detection loop
    {
        ModifiableDBIDs idview = DBIDUtil.newHashSet(relationx.getDBIDs());
        ProxyView<V> proxy = new ProxyView<>(idview, relationx);
        double phialpha = NormalDistribution.standardNormalQuantile(1.0 - alpha * .5);
        // Detect outliers while significant.
        while (true) {
            Pair<DBIDVar, Double> candidate = singleIteration(proxy, relationy);
            if (candidate.second < phialpha) {
                break;
            }
            scores.putDouble(candidate.first, candidate.second);
            if (!Double.isNaN(candidate.second)) {
                mm.put(candidate.second);
            }
            idview.remove(candidate.first);
        }
        // Remaining objects are inliers
        for (DBIDIter iter = idview.iter(); iter.valid(); iter.advance()) {
            scores.putDouble(iter, 0.0);
        }
    }
    DoubleRelation scoreResult = new MaterializedDoubleRelation("GLSSODBackward", "GLSSODbackward-outlier", scores, relationx.getDBIDs());
    OutlierScoreMeta scoreMeta = new BasicOutlierScoreMeta(mm.getMin(), mm.getMax(), 0, Double.POSITIVE_INFINITY, 0);
    return new OutlierResult(scoreMeta, scoreResult);
}
Also used : ProxyView(de.lmu.ifi.dbs.elki.database.relation.ProxyView) DoubleMinMax(de.lmu.ifi.dbs.elki.math.DoubleMinMax) WritableDoubleDataStore(de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore) OutlierResult(de.lmu.ifi.dbs.elki.result.outlier.OutlierResult) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) DoubleRelation(de.lmu.ifi.dbs.elki.database.relation.DoubleRelation) MaterializedDoubleRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation) MaterializedDoubleRelation(de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation) BasicOutlierScoreMeta(de.lmu.ifi.dbs.elki.result.outlier.BasicOutlierScoreMeta) OutlierScoreMeta(de.lmu.ifi.dbs.elki.result.outlier.OutlierScoreMeta) BasicOutlierScoreMeta(de.lmu.ifi.dbs.elki.result.outlier.BasicOutlierScoreMeta) Pair(de.lmu.ifi.dbs.elki.utilities.pairs.Pair) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 2 with ProxyView

use of de.lmu.ifi.dbs.elki.database.relation.ProxyView 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

ProxyView (de.lmu.ifi.dbs.elki.database.relation.ProxyView)2 ProxyDatabase (de.lmu.ifi.dbs.elki.database.ProxyDatabase)1 WritableDoubleDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore)1 ArrayModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs)1 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)1 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)1 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)1 DoubleRelation (de.lmu.ifi.dbs.elki.database.relation.DoubleRelation)1 MaterializedDoubleRelation (de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation)1 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)1 NumberVectorDistanceFunction (de.lmu.ifi.dbs.elki.distance.distancefunction.NumberVectorDistanceFunction)1 DoubleMinMax (de.lmu.ifi.dbs.elki.math.DoubleMinMax)1 BasicOutlierScoreMeta (de.lmu.ifi.dbs.elki.result.outlier.BasicOutlierScoreMeta)1 OutlierResult (de.lmu.ifi.dbs.elki.result.outlier.OutlierResult)1 OutlierScoreMeta (de.lmu.ifi.dbs.elki.result.outlier.OutlierScoreMeta)1 Pair (de.lmu.ifi.dbs.elki.utilities.pairs.Pair)1