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;
}
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;
}
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();
}
}
}
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;
}
}
}
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;
}
Aggregations