use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class RankingQualityHistogram method run.
/**
* Process a database
*
* @param database Database to process
* @param relation Relation to process
* @return Histogram of ranking qualities
*/
public HistogramResult run(Database database, Relation<O> relation) {
final DistanceQuery<O> distanceQuery = database.getDistanceQuery(relation, getDistanceFunction());
final KNNQuery<O> knnQuery = database.getKNNQuery(distanceQuery, relation.size());
if (LOG.isVerbose()) {
LOG.verbose("Preprocessing clusters...");
}
// Cluster by labels
Collection<Cluster<Model>> split = (new ByLabelOrAllInOneClustering()).run(database).getAllClusters();
DoubleStaticHistogram hist = new DoubleStaticHistogram(numbins, 0.0, 1.0);
if (LOG.isVerbose()) {
LOG.verbose("Processing points...");
}
FiniteProgress progress = LOG.isVerbose() ? new FiniteProgress("Computing ROC AUC values", relation.size(), LOG) : null;
ROCEvaluation roc = new ROCEvaluation();
MeanVariance mv = new MeanVariance();
// sort neighbors
for (Cluster<?> clus : split) {
for (DBIDIter iter = clus.getIDs().iter(); iter.valid(); iter.advance()) {
KNNList knn = knnQuery.getKNNForDBID(iter, relation.size());
double result = EvaluateClustering.evaluateRanking(roc, clus, knn);
mv.put(result);
hist.increment(result, 1. / relation.size());
LOG.incrementProcessed(progress);
}
}
LOG.ensureCompleted(progress);
// Transform Histogram into a Double Vector array.
Collection<double[]> res = new ArrayList<>(relation.size());
for (DoubleStaticHistogram.Iter iter = hist.iter(); iter.valid(); iter.advance()) {
res.add(new double[] { iter.getCenter(), iter.getValue() });
}
HistogramResult result = new HistogramResult("Ranking Quality Histogram", "ranking-histogram", res);
result.addHeader("Mean: " + mv.getMean() + " Variance: " + mv.getSampleVariance());
return result;
}
use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class ALIDEstimator method estimate.
@Override
public double estimate(KNNQuery<?> knnq, DBIDRef cur, int k) {
int a = 0;
double sum = 0;
final KNNList kl = knnq.getKNNForDBID(cur, k);
final double w = kl.getKNNDistance();
final double halfw = 0.5 * w;
for (DoubleDBIDListIter it = kl.iter(); it.valid(); it.advance()) {
if (it.doubleValue() <= 0. || DBIDUtil.equal(cur, it)) {
continue;
}
final double v = it.doubleValue();
sum += v < halfw ? FastMath.log(v / w) : FastMath.log1p((v - w) / w);
++a;
final double nw = w - v;
final double halfnw = 0.5 * nw;
for (DoubleDBIDListIter it2 = knnq.getKNNForDBID(it, k).iter(); it2.valid() && it2.doubleValue() <= nw; it2.advance()) {
if (it2.doubleValue() <= 0. || DBIDUtil.equal(it, it2)) {
continue;
}
final double v2 = it2.doubleValue();
sum += v2 < halfnw ? FastMath.log(v2 / nw) : FastMath.log1p((v2 - nw) / nw);
++a;
}
}
return -a / sum;
}
use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class LinearScanRKNNQuery method getRKNNForBulkDBIDs.
@Override
public List<? extends DoubleDBIDList> getRKNNForBulkDBIDs(ArrayDBIDs ids, int k) {
List<ModifiableDoubleDBIDList> rNNList = new ArrayList<>(ids.size());
for (int i = 0; i < ids.size(); i++) {
rNNList.add(DBIDUtil.newDistanceDBIDList());
}
ArrayDBIDs allIDs = DBIDUtil.ensureArray(relation.getDBIDs());
List<? extends KNNList> kNNList = knnQuery.getKNNForBulkDBIDs(allIDs, k);
int i = 0;
for (DBIDIter iter = allIDs.iter(); iter.valid(); iter.advance()) {
KNNList knn = kNNList.get(i);
for (DoubleDBIDListIter n = knn.iter(); n.valid(); n.advance()) {
int j = 0;
for (DBIDIter iter2 = ids.iter(); iter2.valid(); iter2.advance()) {
if (DBIDUtil.equal(n, iter2)) {
ModifiableDoubleDBIDList rNN = rNNList.get(j);
rNN.add(n.doubleValue(), iter);
}
j++;
}
}
i++;
}
for (int j = 0; j < ids.size(); j++) {
rNNList.get(j).sort();
}
return rNNList;
}
use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class LinearScanDistanceKNNQuery method getKNNForBulkDBIDs.
@Override
public List<KNNList> getKNNForBulkDBIDs(ArrayDBIDs ids, int k) {
final int size = ids.size();
final List<KNNHeap> heaps = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
heaps.add(DBIDUtil.newHeap(k));
}
linearScanBatchKNN(ids, heaps);
// Serialize heaps
List<KNNList> result = new ArrayList<>(size);
for (KNNHeap heap : heaps) {
result.add(heap.toKNNList());
}
return result;
}
use of de.lmu.ifi.dbs.elki.database.ids.KNNList in project elki by elki-project.
the class LinearScanEuclideanDistanceKNNQuery method getKNNForBulkDBIDs.
@Override
public List<KNNList> getKNNForBulkDBIDs(ArrayDBIDs ids, int k) {
final Relation<? extends O> relation = getRelation();
final int size = ids.size();
final List<KNNHeap> heaps = new ArrayList<>(size);
List<O> objs = new ArrayList<>(size);
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
heaps.add(DBIDUtil.newHeap(k));
objs.add(relation.get(iter));
}
linearScanBatchKNN(objs, heaps);
List<KNNList> result = new ArrayList<>(heaps.size());
for (KNNHeap heap : heaps) {
result.add(heap.toKNNListSqrt());
}
return result;
}
Aggregations