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;
}