Search in sources :

Example 1 with EM

use of weka.clusterers.EM 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)

Aggregations

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