Search in sources :

Example 11 with ProxyDatabase

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

ProxyDatabase (de.lmu.ifi.dbs.elki.database.ProxyDatabase)11 MaterializedRelation (de.lmu.ifi.dbs.elki.database.relation.MaterializedRelation)6 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)5 VectorFieldTypeInformation (de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation)4 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)4 ArrayList (java.util.ArrayList)4 Cluster (de.lmu.ifi.dbs.elki.data.Cluster)3 Clustering (de.lmu.ifi.dbs.elki.data.Clustering)3 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)3 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)3 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)2 DBSCAN (de.lmu.ifi.dbs.elki.algorithm.clustering.DBSCAN)1 ParameterizationFunction (de.lmu.ifi.dbs.elki.algorithm.clustering.correlation.cash.ParameterizationFunction)1 Model (de.lmu.ifi.dbs.elki.data.model.Model)1 SubspaceModel (de.lmu.ifi.dbs.elki.data.model.SubspaceModel)1 NumericalFeatureSelection (de.lmu.ifi.dbs.elki.data.projection.NumericalFeatureSelection)1 SimpleTypeInformation (de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation)1 WritableDoubleDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore)1 ArrayDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs)1 ArrayModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs)1