Search in sources :

Example 1 with Pair

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;
}
Also used : Instance(weka.core.Instance) HashMap(java.util.HashMap) EM(weka.clusterers.EM) List(java.util.List) LinkedList(java.util.LinkedList) Pair(org.eclipse.net4j.util.collection.Pair)

Example 2 with Pair

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);
}
Also used : Instances(weka.core.Instances) FastVector(weka.core.FastVector) Instance(weka.core.Instance) Pair(org.eclipse.net4j.util.collection.Pair)

Aggregations

Pair (org.eclipse.net4j.util.collection.Pair)2 Instance (weka.core.Instance)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 EM (weka.clusterers.EM)1 FastVector (weka.core.FastVector)1 Instances (weka.core.Instances)1