Search in sources :

Example 6 with ClassLabel

use of de.lmu.ifi.dbs.elki.data.ClassLabel in project elki by elki-project.

the class ByLabelOrAllInOneClustering method run.

@Override
public Clustering<Model> run(Database database) {
    // Prefer a true class label
    try {
        Relation<ClassLabel> relation = database.getRelation(TypeUtil.CLASSLABEL);
        return run(relation);
    } catch (NoSupportedDataTypeException e) {
    // Ignore.
    }
    try {
        Relation<ClassLabel> relation = database.getRelation(TypeUtil.GUESSED_LABEL);
        return run(relation);
    } catch (NoSupportedDataTypeException e) {
    // Ignore.
    }
    final DBIDs ids = database.getRelation(TypeUtil.ANY).getDBIDs();
    Clustering<Model> result = new Clustering<>("All-in-one trivial Clustering", "allinone-clustering");
    Cluster<Model> c = new Cluster<Model>(ids, ClusterModel.CLUSTER);
    result.addToplevelCluster(c);
    return result;
}
Also used : ClassLabel(de.lmu.ifi.dbs.elki.data.ClassLabel) NoSupportedDataTypeException(de.lmu.ifi.dbs.elki.data.type.NoSupportedDataTypeException) DBIDs(de.lmu.ifi.dbs.elki.database.ids.DBIDs) Model(de.lmu.ifi.dbs.elki.data.model.Model) ClusterModel(de.lmu.ifi.dbs.elki.data.model.ClusterModel) Cluster(de.lmu.ifi.dbs.elki.data.Cluster) Clustering(de.lmu.ifi.dbs.elki.data.Clustering)

Example 7 with ClassLabel

use of de.lmu.ifi.dbs.elki.data.ClassLabel in project elki by elki-project.

the class AbstractClassifier method alignLabels.

/**
 * Align the labels for a label query.
 *
 * @param l1 List of reference labels
 * @param d1 Probabilities for these labels
 * @param l2 List of requested labels
 * @return Probabilities in desired output order
 */
protected double[] alignLabels(List<ClassLabel> l1, double[] d1, Collection<ClassLabel> l2) {
    assert (l1.size() == d1.length);
    if (l1 == l2) {
        return d1.clone();
    }
    double[] d2 = new double[l2.size()];
    Iterator<ClassLabel> i2 = l2.iterator();
    for (int i = 0; i2.hasNext(); ) {
        ClassLabel l = i2.next();
        int idx = l1.indexOf(l);
        if (idx < 0 && getLogger().isDebuggingFiner()) {
            getLogger().debugFiner("Label not found: " + l);
        }
        // Default to 0 for unknown labels!
        d2[i] = (idx >= 0) ? d1[idx] : 0.;
    }
    return d2;
}
Also used : ClassLabel(de.lmu.ifi.dbs.elki.data.ClassLabel)

Example 8 with ClassLabel

use of de.lmu.ifi.dbs.elki.data.ClassLabel 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)

Example 9 with ClassLabel

use of de.lmu.ifi.dbs.elki.data.ClassLabel in project elki by elki-project.

the class StratifiedCrossValidation method initialize.

@Override
public void initialize(MultipleObjectsBundle bundle) {
    super.initialize(bundle);
    fold = 0;
    IntArrayList[] classBuckets = new IntArrayList[this.labels.size()];
    for (int i = 0; i < this.labels.size(); i++) {
        classBuckets[i] = new IntArrayList();
    }
    for (int i = 0, l = bundle.dataLength(); i < l; ++i) {
        ClassLabel label = (ClassLabel) bundle.data(i, labelcol);
        if (label == null) {
            throw new AbortException("Unlabeled instances currently not supported.");
        }
        int classIndex = Collections.binarySearch(labels, label);
        if (classIndex < 0) {
            throw new AbortException("Label not in label list: " + label);
        }
        classBuckets[classIndex].add(i);
    }
    // TODO: shuffle the class buckets?
    sizes = new int[nfold];
    assignment = new int[bundle.dataLength()];
    for (IntArrayList bucket : classBuckets) {
        for (int i = 0; i < bucket.size(); i++) {
            assignment[bucket.getInt(i)] = i % nfold;
        }
    }
}
Also used : ClassLabel(de.lmu.ifi.dbs.elki.data.ClassLabel) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Example 10 with ClassLabel

use of de.lmu.ifi.dbs.elki.data.ClassLabel in project elki by elki-project.

the class AbstractSupervisedProjectionVectorFilter method filter.

@Override
public MultipleObjectsBundle filter(MultipleObjectsBundle objects) {
    final int dataLength = objects.dataLength();
    if (dataLength == 0) {
        return objects;
    }
    List<? extends ClassLabel> classcolumn = null;
    // First of all, identify a class label column.
    for (int r = 0; r < objects.metaLength(); r++) {
        SimpleTypeInformation<?> type = objects.meta(r);
        List<?> column = objects.getColumn(r);
        if (TypeUtil.CLASSLABEL.isAssignableFromType(type)) {
            @SuppressWarnings("unchecked") final List<? extends ClassLabel> castcolumn = (List<? extends ClassLabel>) column;
            classcolumn = castcolumn;
            break;
        }
    }
    if (classcolumn == null) {
        getLogger().warning("No class label column found (try " + ClassLabelFilter.class.getSimpleName() + ") -- cannot run " + this.getClass().getSimpleName());
        return objects;
    }
    boolean somesuccess = false;
    MultipleObjectsBundle bundle = new MultipleObjectsBundle();
    // Secondly, look for columns to train the projection on.
    for (int r = 0; r < objects.metaLength(); r++) {
        SimpleTypeInformation<?> type = objects.meta(r);
        List<?> column = objects.getColumn(r);
        if (!TypeUtil.NUMBER_VECTOR_FIELD.isAssignableFromType(type)) {
            bundle.appendColumn(type, column);
            continue;
        }
        @SuppressWarnings("unchecked") List<V> vectorcolumn = (List<V>) column;
        final VectorFieldTypeInformation<?> vtype = (VectorFieldTypeInformation<?>) type;
        @SuppressWarnings("unchecked") NumberVector.Factory<V> factory = (NumberVector.Factory<V>) vtype.getFactory();
        int dim = vtype.getDimensionality();
        if (tdim > dim) {
            if (getLogger().isVerbose()) {
                getLogger().verbose("Setting projection dimension to original dimension: projection dimension: " + tdim + " larger than original dimension: " + dim);
            }
            tdim = dim;
        }
        try {
            double[][] proj = computeProjectionMatrix(vectorcolumn, classcolumn, dim);
            for (int i = 0; i < dataLength; i++) {
                double[] pv = times(proj, vectorcolumn.get(i).toArray());
                vectorcolumn.set(i, factory.newNumberVector(pv));
            }
            bundle.appendColumn(convertedType(type, factory), column);
            somesuccess = true;
        } catch (Exception e) {
            getLogger().error("Projection failed -- continuing with unprojected data!", e);
            bundle.appendColumn(type, column);
            continue;
        }
    }
    if (!somesuccess) {
        getLogger().warning("No vector field of fixed dimensionality found.");
        return objects;
    }
    return bundle;
}
Also used : MultipleObjectsBundle(de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle) ClassLabel(de.lmu.ifi.dbs.elki.data.ClassLabel) VectorFieldTypeInformation(de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation) NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) List(java.util.List) IntList(it.unimi.dsi.fastutil.ints.IntList) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList)

Aggregations

ClassLabel (de.lmu.ifi.dbs.elki.data.ClassLabel)12 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)5 SimpleClassLabel (de.lmu.ifi.dbs.elki.data.SimpleClassLabel)4 MultipleObjectsBundle (de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle)4 ExternalID (de.lmu.ifi.dbs.elki.data.ExternalID)2 Model (de.lmu.ifi.dbs.elki.data.model.Model)2 VectorFieldTypeInformation (de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation)2 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)2 IntList (it.unimi.dsi.fastutil.ints.IntList)2 Entry (it.unimi.dsi.fastutil.objects.Object2IntMap.Entry)2 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)2 ArrayList (java.util.ArrayList)2 Cluster (de.lmu.ifi.dbs.elki.data.Cluster)1 Clustering (de.lmu.ifi.dbs.elki.data.Clustering)1 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)1 LabelList (de.lmu.ifi.dbs.elki.data.LabelList)1 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)1 SparseDoubleVector (de.lmu.ifi.dbs.elki.data.SparseDoubleVector)1 ClusterModel (de.lmu.ifi.dbs.elki.data.model.ClusterModel)1 NoSupportedDataTypeException (de.lmu.ifi.dbs.elki.data.type.NoSupportedDataTypeException)1