Search in sources :

Example 1 with Entry

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;
}
Also used : DoubleDBIDListIter(de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter) Entry(it.unimi.dsi.fastutil.objects.Object2IntMap.Entry) ClassLabel(de.lmu.ifi.dbs.elki.data.ClassLabel) KNNList(de.lmu.ifi.dbs.elki.database.ids.KNNList) Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)

Example 2 with Entry

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();
        }
    }
}
Also used : Entry(it.unimi.dsi.fastutil.objects.Object2IntMap.Entry) ClassLabel(de.lmu.ifi.dbs.elki.data.ClassLabel) Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Aggregations

ClassLabel (de.lmu.ifi.dbs.elki.data.ClassLabel)2 Entry (it.unimi.dsi.fastutil.objects.Object2IntMap.Entry)2 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)2 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)1 DoubleDBIDListIter (de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter)1 KNNList (de.lmu.ifi.dbs.elki.database.ids.KNNList)1