use of de.lmu.ifi.dbs.elki.data.Clustering in project elki by elki-project.
the class ExternalClustering method attachToRelation.
/**
* Build a clustering from the file result.
*
* @param database Database
* @param r Result to attach to
* @param assignment Cluster assignment
* @param name Name
*/
private void attachToRelation(Database database, Relation<?> r, IntArrayList assignment, ArrayList<String> name) {
DBIDs ids = r.getDBIDs();
if (!(ids instanceof ArrayDBIDs)) {
throw new AbortException("External clusterings can only be used with static DBIDs.");
}
Int2IntOpenHashMap sizes = new Int2IntOpenHashMap();
for (IntListIterator it = assignment.iterator(); it.hasNext(); ) {
sizes.addTo(it.nextInt(), 1);
}
Int2ObjectOpenHashMap<ArrayModifiableDBIDs> cids = new Int2ObjectOpenHashMap<>(sizes.size());
for (ObjectIterator<Int2IntMap.Entry> it = sizes.int2IntEntrySet().fastIterator(); it.hasNext(); ) {
Int2IntMap.Entry entry = it.next();
cids.put(entry.getIntKey(), DBIDUtil.newArray(entry.getIntValue()));
}
{
DBIDArrayIter it = ((ArrayDBIDs) ids).iter();
for (int i = 0; i < assignment.size(); i++) {
cids.get(assignment.getInt(i)).add(it.seek(i));
}
}
String nam = FormatUtil.format(name, " ");
String snam = nam.toLowerCase().replace(' ', '-');
Clustering<ClusterModel> result = new Clustering<>(nam, snam);
for (ObjectIterator<Int2ObjectMap.Entry<ArrayModifiableDBIDs>> it = cids.int2ObjectEntrySet().fastIterator(); it.hasNext(); ) {
Int2ObjectMap.Entry<ArrayModifiableDBIDs> entry = it.next();
boolean noise = entry.getIntKey() < 0;
result.addToplevelCluster(new Cluster<>(entry.getValue(), noise, ClusterModel.CLUSTER));
}
database.getHierarchy().add(r, result);
}
use of de.lmu.ifi.dbs.elki.data.Clustering in project elki by elki-project.
the class ByLabelHierarchicalClustering method run.
/**
* Run the actual clustering algorithm.
*
* @param relation The data input to use
*/
public Clustering<Model> run(Relation<?> relation) {
HashMap<String, DBIDs> labelmap = new HashMap<>();
ModifiableDBIDs noiseids = DBIDUtil.newArray();
Clustering<Model> clustering = new Clustering<>("By Label Hierarchical Clustering", "bylabel-clustering");
for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
final Object val = relation.get(iditer);
if (val == null) {
noiseids.add(iditer);
continue;
}
String label = val.toString();
assign(labelmap, label, iditer);
}
ArrayList<Cluster<Model>> clusters = new ArrayList<>(labelmap.size());
for (Entry<String, DBIDs> entry : labelmap.entrySet()) {
DBIDs ids = entry.getValue();
if (ids instanceof DBID) {
noiseids.add((DBID) ids);
continue;
}
Cluster<Model> clus = new Cluster<Model>(entry.getKey(), ids, ClusterModel.CLUSTER);
clusters.add(clus);
}
for (Cluster<Model> cur : clusters) {
boolean isrootcluster = true;
for (Cluster<Model> oth : clusters) {
if (oth != cur && oth.getName().startsWith(cur.getName())) {
clustering.addChildCluster(oth, cur);
if (LOG.isDebuggingFiner()) {
LOG.debugFiner(oth.getName() + " is a child of " + cur.getName());
}
isrootcluster = false;
}
}
if (isrootcluster) {
clustering.addToplevelCluster(cur);
}
}
// Collected noise IDs.
if (noiseids.size() > 0) {
Cluster<Model> c = new Cluster<Model>("Noise", noiseids, ClusterModel.CLUSTER);
c.setNoise(true);
clustering.addToplevelCluster(c);
}
return clustering;
}
use of de.lmu.ifi.dbs.elki.data.Clustering in project elki by elki-project.
the class EvaluateDaviesBouldin method processNewResult.
@Override
public void processNewResult(ResultHierarchy hier, Result result) {
List<Clustering<?>> crs = Clustering.getClusteringResults(result);
if (crs.isEmpty()) {
return;
}
Database db = ResultUtil.findDatabase(hier);
Relation<? extends NumberVector> rel = db.getRelation(this.distanceFunction.getInputTypeRestriction());
for (Clustering<?> c : crs) {
evaluateClustering(db, (Relation<? extends NumberVector>) rel, c);
}
}
use of de.lmu.ifi.dbs.elki.data.Clustering in project elki by elki-project.
the class EvaluateSilhouette method processNewResult.
@Override
public void processNewResult(ResultHierarchy hier, Result result) {
List<Clustering<?>> crs = Clustering.getClusteringResults(result);
if (crs.isEmpty()) {
return;
}
Database db = ResultUtil.findDatabase(hier);
Relation<O> rel = db.getRelation(distance.getInputTypeRestriction());
DistanceQuery<O> dq = db.getDistanceQuery(rel, distance);
for (Clustering<?> c : crs) {
evaluateClustering(db, rel, dq, c);
}
}
use of de.lmu.ifi.dbs.elki.data.Clustering in project elki by elki-project.
the class EvaluateSimplifiedSilhouette method processNewResult.
@Override
public void processNewResult(ResultHierarchy hier, Result result) {
List<Clustering<?>> crs = Clustering.getClusteringResults(result);
if (crs.isEmpty()) {
return;
}
Database db = ResultUtil.findDatabase(hier);
Relation<? extends NumberVector> rel = db.getRelation(this.distance.getInputTypeRestriction());
for (Clustering<?> c : crs) {
evaluateClustering(db, rel, c);
}
}
Aggregations