List of usage examples for weka.clusterers HierarchicalClusterer distributionForInstance
@Override public double[] distributionForInstance(Instance instance) throws Exception
From source file:eu.cassandra.server.mongo.csn.MongoCluster.java
License:Apache License
public DBObject clusterHierarchical(String message, String graph_id, String run_id, String clusterBasedOn, int numberOfClusters, String name, String clusterbasedon) { try {/*from ww w.j a v a 2 s. c o m*/ Instances instances = getInstances(clusterBasedOn, graph_id); if (instances.numInstances() < 2) { return new JSONtoReturn().createJSONError(message, new Exception("Number of CSN Nodes is < 2")); } HierarchicalClusterer h = new HierarchicalClusterer(); h.setOptions(new String[] { "-L", "AVERAGE" }); h.setDistanceFunction(new EuclideanDistance()); if (numberOfClusters > 0) h.setNumClusters(numberOfClusters); h.buildClusterer(instances); HashMap<Integer, Vector<String>> clusters = new HashMap<Integer, Vector<String>>(); double[] arr; for (int i = 0; i < instances.numInstances(); i++) { String nodeId = nodeIDs.get(i); arr = h.distributionForInstance(instances.instance(i)); for (int j = 0; j < arr.length; j++) { if (arr[j] == 1.0) { if (!clusters.containsKey(j)) { Vector<String> nodes = new Vector<String>(); nodes.add(nodeId); clusters.put(j, nodes); } else { Vector<String> nodes = clusters.get(j); nodes.add(nodeId); clusters.put(j, nodes); } } } } return saveClusters(graph_id, run_id, "hierarchical", clusters, null, name, clusterbasedon); } catch (Exception e) { e.printStackTrace(); return new JSONtoReturn().createJSONError(message, e); } }
From source file:org.iobserve.analysis.behavior.clustering.hierarchical.ClusteringResultsBuilder.java
License:Apache License
/** * Build clustering result list of clustered instances and their assigned clusters. * * @param instances//from w w w .j av a 2s . c o m * Input data that is clustered. * * @param hierarchicalClusterer * Weka-clusterer that performs the hierarchical clustering. * * @return clusteringResults */ public static Map<Integer, List<Pair<Instance, Double>>> buildClusteringResults(final Instances instances, final HierarchicalClusterer hierarchicalClusterer) { /* * An entry of the map looks like this: <clusterID, <instance, 1.0>>. The 1,0 is the * probability that the instance is in this cluster. It is needed for unity of clustering * output data. */ final Map<Integer, List<Pair<Instance, Double>>> clusteringResults = new HashMap<>(); // NOPMD; for (int i = 0; i < instances.numInstances(); i++) { final Instance currentInstance = instances.instance(i); try { final int cluster = hierarchicalClusterer.clusterInstance(currentInstance); final double probability = hierarchicalClusterer.distributionForInstance(currentInstance)[cluster]; if (clusteringResults.get(cluster) == null) { clusteringResults.put(cluster, new LinkedList<Pair<Instance, Double>>()); } clusteringResults.get(cluster).add(new Pair<>(currentInstance, probability)); } catch (final Exception e) { // NOPMD NOCS api dependency ClusteringResultsBuilder.LOGGER.error("Failed building hierarchical clustering.", e); } } return clusteringResults; }