use of de.lmu.ifi.dbs.elki.algorithm.KNNDistancesSampler.KNNDistanceOrderResult in project elki by elki-project.
the class KNNDistancesSampler method run.
/**
* Provides an order of the kNN-distances for all objects within the specified
* database.
*
* @param database Database
* @param relation Relation
* @return Result
*/
public KNNDistanceOrderResult run(Database database, Relation<O> relation) {
final DistanceQuery<O> distanceQuery = database.getDistanceQuery(relation, getDistanceFunction());
final KNNQuery<O> knnQuery = database.getKNNQuery(distanceQuery, k + 1);
final int size = (int) ((sample <= 1.) ? Math.ceil(relation.size() * sample) : sample);
DBIDs sample = DBIDUtil.randomSample(relation.getDBIDs(), size, rnd);
FiniteProgress prog = LOG.isVerbose() ? new FiniteProgress("Sampling kNN distances", size, LOG) : null;
double[] knnDistances = new double[size];
int i = 0;
for (DBIDIter iditer = sample.iter(); iditer.valid(); iditer.advance(), i++) {
final KNNList neighbors = knnQuery.getKNNForDBID(iditer, k + 1);
knnDistances[i] = neighbors.getKNNDistance();
LOG.incrementProcessed(prog);
}
LOG.ensureCompleted(prog);
return new KNNDistanceOrderResult(knnDistances, k);
}
Aggregations