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