List of usage examples for weka.clusterers Clusterer distributionForInstance
public double[] distributionForInstance(Instance instance) throws Exception;
From source file:de.unidue.langtech.grading.tc.ClusteringTask.java
License:Open Source License
/** * Returns a mapping from cluster IDs to instance offsets * @return//www . j a v a 2s . com */ private Map<Integer, Set<Integer>> getClusterMap(Instances data, Clusterer clusterer) throws Exception { Map<Integer, Set<Integer>> clusterMap = new HashMap<Integer, Set<Integer>>(); @SuppressWarnings("rawtypes") Enumeration instanceEnumeration = data.enumerateInstances(); int instanceOffset = 0; while (instanceEnumeration.hasMoreElements()) { Instance instance = (Instance) instanceEnumeration.nextElement(); double[] distribution = clusterer.distributionForInstance(instance); int clusterId = 0; for (double value : distribution) { if (new Double(value).intValue() == 1) { Set<Integer> clusterInstances; if (!clusterMap.containsKey(clusterId)) { clusterInstances = new HashSet<Integer>(); clusterMap.put(clusterId, clusterInstances); } clusterInstances = clusterMap.get(clusterId); clusterInstances.add(instanceOffset); clusterMap.put(clusterId, clusterInstances); } clusterId++; } instanceOffset++; } return clusterMap; }
From source file:org.mcennis.graphrat.algorithm.clustering.WekaProbablisticClusterer.java
License:Open Source License
@Override public void execute(Graph g) { ActorByMode mode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode"); mode.buildQuery((String) parameter.get("GroundMode").get(), ".*", false); try {/* w ww . ja v a 2 s.co m*/ Clusterer clusterer = (Clusterer) ((Class) parameter.get("Clusterer").get()).newInstance(); String[] options = ((String) parameter.get("Options").get()).split("\\s+"); ((OptionHandler) clusterer).setOptions(options); Iterator<Actor> actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null); Instances dataSet = null; while (actor.hasNext()) { Actor a = actor.next(); Property property = a.getProperty( AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get())); if (!property.getValue().isEmpty()) { Instance value = (Instance) property.getValue().get(0); if ((dataSet == null) && (value.dataset() != null)) { FastVector attributes = new FastVector(); for (int i = 0; i < value.dataset().numAttributes(); ++i) { attributes.addElement(value.dataset().attribute(i)); } dataSet = new Instances("Clustering", attributes, 1000); } else if ((dataSet == null)) { FastVector attributes = new FastVector(); for (int i = 0; i < value.numAttributes(); ++i) { Attribute element = new Attribute(Integer.toString(i)); attributes.addElement(element); } dataSet = new Instances("Clustering", attributes, 1000); } dataSet.add(value); } } clusterer.buildClusterer(dataSet); actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null); HashMap<Integer, Graph> clusters = new HashMap<Integer, Graph>(); while (actor.hasNext()) { Actor a = actor.next(); Property property = a.getProperty( AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get())); if (!property.getValue().isEmpty()) { Instance instance = (Instance) property.getValue().get(0); double[] cluster = new double[] {}; try { cluster = clusterer.distributionForInstance(instance); } catch (Exception ex) { Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, "ClusterInstance on clusterer failed", ex); } Property clusterProperty = PropertyFactory.newInstance().create("BasicProperty", AlgorithmMacros.getDestID(parameter, g, (String) parameter.get("DestinationProperty").get()), (new double[] {}).getClass()); clusterProperty.add(cluster); a.add(clusterProperty); } } } catch (InstantiationException ex) { Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex); } }