use of de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter in project elki by elki-project.
the class KNNClassifier method classify.
@Override
public ClassLabel classify(O instance) {
Object2IntOpenHashMap<ClassLabel> count = new Object2IntOpenHashMap<>();
KNNList query = knnq.getKNNForObject(instance, k);
for (DoubleDBIDListIter neighbor = query.iter(); neighbor.valid(); neighbor.advance()) {
count.addTo(labelrep.get(neighbor), 1);
}
int bestoccur = Integer.MIN_VALUE;
ClassLabel bestl = null;
for (ObjectIterator<Entry<ClassLabel>> iter = count.object2IntEntrySet().fastIterator(); iter.hasNext(); ) {
Entry<ClassLabel> entry = iter.next();
if (entry.getIntValue() > bestoccur) {
bestoccur = entry.getIntValue();
bestl = entry.getKey();
}
}
return bestl;
}
use of de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter in project elki by elki-project.
the class AbstractCoverTree method collectByCover.
/**
* Collect all elements with respect to a new routing object.
*
* @param cur Routing object
* @param candidates Candidate list
* @param fmax Maximum distance
* @param collect Output list
*/
protected void collectByCover(DBIDRef cur, ModifiableDoubleDBIDList candidates, double fmax, ModifiableDoubleDBIDList collect) {
assert (collect.size() == 0) : "Not empty";
// Except first = cur!
DoubleDBIDListIter it = candidates.iter().advance();
while (it.valid()) {
assert (!DBIDUtil.equal(cur, it));
final double dist = distance(cur, it);
if (dist <= fmax) {
// Collect
collect.add(dist, it);
candidates.removeSwap(it.getOffset());
} else {
// Keep in candidates, outside cover radius.
it.advance();
}
}
}
use of de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter in project elki by elki-project.
the class AbstractCoverTree method excludeNotCovered.
/**
* Retain all elements within the current cover.
*
* @param candidates Candidates
* @param fmax Maximum distance
* @param collect Far neighbors
*/
protected void excludeNotCovered(ModifiableDoubleDBIDList candidates, double fmax, ModifiableDoubleDBIDList collect) {
for (DoubleDBIDListIter it = candidates.iter(); it.valid(); ) {
if (it.doubleValue() > fmax) {
collect.add(it.doubleValue(), it);
candidates.removeSwap(it.getOffset());
} else {
// Keep in candidates
it.advance();
}
}
}
use of de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter in project elki by elki-project.
the class AbstractCoverTree method maxDistance.
/**
* Find maximum in a list via scanning.
*
* @param elems Elements
* @return Maximum distance
*/
protected double maxDistance(DoubleDBIDList elems) {
double max = 0;
for (DoubleDBIDListIter it = elems.iter(); it.valid(); it.advance()) {
final double v = it.doubleValue();
max = max > v ? max : v;
}
return max;
}
use of de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter in project elki by elki-project.
the class InMemoryInvertedIndex method naiveQuerySparse.
/**
* Query the most similar objects, sparse version.
*
* @param obj Query object
* @param scores Score storage
* @param cands Non-zero objects set
* @return Result
*/
private double naiveQuerySparse(SparseNumberVector obj, WritableDoubleDataStore scores, HashSetModifiableDBIDs cands) {
// Length of query object, for final normalization
double len = 0.;
for (int iter = obj.iter(); obj.iterValid(iter); iter = obj.iterAdvance(iter)) {
final int dim = obj.iterDim(iter);
final double val = obj.iterDoubleValue(iter);
if (val == 0. || val != val) {
continue;
}
len += val * val;
// No matching documents in index:
if (dim >= index.size()) {
continue;
}
ModifiableDoubleDBIDList column = index.get(dim);
for (DoubleDBIDListIter n = column.iter(); n.valid(); n.advance()) {
scores.increment(n, n.doubleValue() * val);
cands.add(n);
}
}
return FastMath.sqrt(len);
}
Aggregations