use of it.unimi.dsi.fastutil.objects.Object2IntMap.Entry 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 it.unimi.dsi.fastutil.objects.Object2IntMap.Entry in project elki by elki-project.
the class PriorProbabilityClassifier method buildClassifier.
/**
* Learns the prior probability for all classes.
*/
@Override
public void buildClassifier(Database database, Relation<? extends ClassLabel> labelrep) {
Object2IntOpenHashMap<ClassLabel> count = new Object2IntOpenHashMap<>();
for (DBIDIter iter = labelrep.iterDBIDs(); iter.valid(); iter.advance()) {
count.addTo(labelrep.get(iter), 1);
}
int max = Integer.MIN_VALUE;
double size = labelrep.size();
distribution = new double[count.size()];
labels = new ArrayList<>(count.size());
ObjectIterator<Entry<ClassLabel>> iter = count.object2IntEntrySet().fastIterator();
for (int i = 0; iter.hasNext(); ++i) {
Entry<ClassLabel> entry = iter.next();
distribution[i] = entry.getIntValue() / size;
labels.add(entry.getKey());
if (entry.getIntValue() > max) {
max = entry.getIntValue();
prediction = entry.getKey();
}
}
}
Aggregations