use of org.eclipse.net4j.util.collection.Pair in project iobserve-analysis by research-iobserve.
the class ExpectationMaximizationClustering method clusterInstances.
/*
* (non-Javadoc)
*
* @see
* org.iobserve.analysis.cdoruserbehavior.clustering.IClustering#clusterInstances(weka.core.
* Instances)
*/
@Override
public Map<Integer, List<Pair<Instance, Double>>> clusterInstances(final Instances instances) {
final EM emClustering = new EM();
if (ExpectationMaximizationClustering.LOGGER.isInfoEnabled()) {
ExpectationMaximizationClustering.LOGGER.info("Computing the EM-Clustering with following options: " + emClustering.getOptions());
}
// NOPMD
final Map<Integer, List<Pair<Instance, Double>>> resultMap = new HashMap<>();
try {
emClustering.buildClusterer(instances);
/**
* iterate through all instances and bucket sort them with their probabilities to their
* assigned cluster.
*/
for (int i = 0; i < instances.numInstances(); i++) {
final Instance currentInstance = instances.instance(i);
final int cluster = emClustering.clusterInstance(currentInstance);
final double probability = emClustering.distributionForInstance(currentInstance)[cluster];
if (resultMap.get(cluster) == null) {
resultMap.put(cluster, new LinkedList<Pair<Instance, Double>>());
}
resultMap.get(cluster).add(new Pair<>(currentInstance, probability));
}
} catch (final Exception e) {
// NOPMD NOCS api induced
ExpectationMaximizationClustering.LOGGER.error("Clustering failed.", e);
}
return resultMap;
}
use of org.eclipse.net4j.util.collection.Pair in project iobserve-analysis by research-iobserve.
the class ClusterMerger method execute.
/*
* (non-Javadoc)
*
* @see teetime.framework.AbstractConsumerStage#execute(java.lang.Object)
*/
@Override
protected void execute(final Map<Integer, List<Pair<Instance, Double>>> clustering) throws Exception {
/**
* simply pick the first instance of every cluster lookup attributes to build a new
* instances Object
*/
Instance instance = clustering.entrySet().iterator().next().getValue().get(0).getElement1();
final FastVector attributes = new FastVector();
for (int j = 0; j < instance.numAttributes(); j++) {
attributes.addElement(instance.attribute(j));
}
final Instances result = new Instances("Clustering Result", attributes, clustering.size());
for (final List<Pair<Instance, Double>> entry : clustering.values()) {
if (!entry.isEmpty()) {
instance = entry.get(0).getElement1();
result.add(instance);
}
}
this.printInstances(result);
this.outputPort.send(result);
}
Aggregations