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;
}
Aggregations