List of usage examples for weka.clusterers ClusterEvaluation ClusterEvaluation
public ClusterEvaluation()
From source file:myclusterer.WekaCode.java
public void evaluateModel(Instances dataTest) throws Exception { eval = new ClusterEvaluation(); eval.setClusterer(clusterer);// www.ja v a 2 s. co m eval.evaluateClusterer(dataTest); }
From source file:net.sf.markov4jmeter.behaviormodelextractor.extraction.transformation.clustering.XMeansClusteringStrategy.java
License:Apache License
/** * {@inheritDoc}//from www.j av a 2 s . co m * * <p> * This method is specialized for <b>xmeans</b> clustering. */ @Override public BehaviorMix apply(final BehaviorModelAbsolute[] behaviorModelsAbsolute, final UseCaseRepository useCaseRepository) { final ABMToRBMTransformer abmToRbmTransformer = new ABMToRBMTransformer(); // Behavior Mix to be returned; final BehaviorMix behaviorMix = this.createBehaviorMix(); try { // Returns a valid instances set, generated based on the absolut // behavior models Instances instances = getInstances(behaviorModelsAbsolute); // XMeans --> Weka XMeans xmeans = new XMeans(); if (CommandLineArgumentsHandler.getSeedValue() != null) { xmeans.setSeed(Integer.parseInt(CommandLineArgumentsHandler.getSeedValue())); } // distance function DistanceFunction euclideanDistance = new EuclideanDistance(); // String[] options = new String[1]; // options[0] = "-D"; // euclideanDistance.setOptions(options); euclideanDistance.setInstances(instances); xmeans.setDistanceF(euclideanDistance); // DistanceFunction manhattanDistance = new ManhattanDistance(); // String[] options = new String[1]; // options[0] = "-D"; // manhattanDistance.setOptions(options); // manhattanDistance.setInstances(instances); // xmeans.setDistanceF(manhattanDistance); int[] clustersize = null; // create new assignments int[] assignments = new int[instances.numInstances()]; // get number of clusters to be generated. int numberOfClustersMin = Integer.parseInt(CommandLineArgumentsHandler.getNumberOfClustersMin()); int numberOfClustersMax = 0; if (CommandLineArgumentsHandler.getNumberOfClustersMax() != "") { numberOfClustersMax = Integer.parseInt(CommandLineArgumentsHandler.getNumberOfClustersMax()); } else { numberOfClustersMax = numberOfClustersMin; } // clustering xmeans.setMinNumClusters(numberOfClustersMin); xmeans.setMaxNumClusters(numberOfClustersMax); // build cluster xmeans.buildClusterer(instances); ClusterEvaluation clusterEvaluation = new ClusterEvaluation(); clusterEvaluation.setClusterer(xmeans); clusterEvaluation.evaluateClusterer(instances); // clusterSize clustersize = new int[xmeans.getClusterCenters().numInstances()]; // set assignments and clustersize for (int s = 0; s < instances.numInstances(); s++) { assignments[s] = xmeans.clusterInstance(instances.instance(s)); clustersize[xmeans.clusterInstance(instances.instance(s))]++; } ClusteringMetrics clusteringMetrics = new ClusteringMetrics(); clusteringMetrics.calculateInterClusteringSimilarity(xmeans.getClusterCenters()); clusteringMetrics.calculateIntraClusteringSimilarity(xmeans.getClusterCenters(), instances, assignments); clusteringMetrics.calculateBetas(); clusteringMetrics.printErrorMetricsHeader(); clusteringMetrics.printErrorMetrics(xmeans.getClusterCenters().numInstances()); clusteringMetrics.printClusteringMetrics(clustersize, assignments, instances); // clusteringMetrics.printClusterAssignmentsToSession(assignments, // xmeans.getClusterCenters().numInstances()); Instances resultingCentroids = xmeans.getClusterCenters(); // for each centroid instance, create new behaviorModelRelative for (int i = 0; i < resultingCentroids.numInstances(); i++) { Instance centroid = resultingCentroids.instance(i); // create a Behavior Model, which includes all vertices only; // the vertices are associated with the use cases, and a // dedicated // vertex that represents the final state will be added; final BehaviorModelAbsolute behaviorModelAbsoluteCentroid = this .createBehaviorModelAbsoluteWithoutTransitions(useCaseRepository.getUseCases()); // install the transitions in between vertices; this.installTransitions(behaviorModelsAbsolute, behaviorModelAbsoluteCentroid, centroid, assignments, i); // convert absolute to relative behaviorModel final BehaviorModelRelative behaviorModelRelative = abmToRbmTransformer .transform(behaviorModelAbsoluteCentroid); // relative Frequency of cluster i double relativeFrequency = (double) clustersize[i] / (double) instances.numInstances(); // create the (unique) Behavior Mix entry to be returned; final BehaviorMixEntry behaviorMixEntry = this.createBehaviorMixEntry( AbstractClusteringStrategy.GENERIC_BEHAVIOR_MODEL_NAME, relativeFrequency, // relative frequency; behaviorModelRelative); // add to resulting behaviorMix behaviorMix.getEntries().add(behaviorMixEntry); } return behaviorMix; } catch (ExtractionException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } // if any error occurs, an ExtractionExeption should be thrown, // indicating the error that occurred; // the classes "NoClusteringStrategy" and "SimpleClusteringStrategy" // should give an idea for handling the Behavior Models and how to // use the helping methods of the (abstract) parent class. return behaviorMix; }
From source file:nl.uva.sne.commons.ClusterUtils.java
public static Map<String, String> bulidClusters(Clusterer clusterer, Instances data, String inDir) throws Exception { FilteredClusterer fc = new FilteredClusterer(); String[] options = new String[2]; options[0] = "-R"; // "range" options[1] = "1"; // we want to ignore the attribute that is in the position '1' Remove remove = new Remove(); // new instance of filter remove.setOptions(options); // set options fc.setFilter(remove); //add filter to remove attributes fc.setClusterer(clusterer); //bind FilteredClusterer to original clusterer fc.buildClusterer(data);/*from www.ja v a2 s . co m*/ Map<String, String> clusters = new HashMap<>(); for (int i = 0; i < data.numInstances(); i++) { Instance inst = data.instance(i); int theClass = fc.clusterInstance(inst); String s = data.attribute(0).value(i); clusters.put(inDir + File.separator + s, String.valueOf(theClass)); System.err.println(s + " is in cluster " + theClass); } ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(fc); // the cluster to evaluate eval.evaluateClusterer(data); // data to evaluate the clusterer on // double ll = eval.getLogLikelihood(); // Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "LogLikelihood :{0}", ll); // // if (clusterer instanceof SimpleKMeans) { // double sqrErr = ((SimpleKMeans) clusterer).getSquaredError(); // Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "Squared Error:{0}", sqrErr); // } Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "# of clusters: {0}", eval.getNumClusters()); Logger.getLogger(ClusterUtils.class.getName()).log(Level.INFO, "clusterResults: {0}", eval.clusterResultsToString()); return clusters; }
From source file:qoala.arff.java
public void SimpleKmeans(int numberOfCLuster) throws Exception { Instances train = new Instances(dataSet); SimpleKMeans skm = new SimpleKMeans(); skm.setPreserveInstancesOrder(true); skm.setNumClusters(numberOfCLuster); skm.buildClusterer(train);/* w w w .j a va 2s . c o m*/ skm.setSeed(10); int[] ClusterSize = skm.getClusterSizes(); ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(skm); eval.evaluateClusterer(train); System.out.println("Cluster Evaluation:" + eval.clusterResultsToString()); int[] assignments = skm.getAssignments(); System.out.println("# - cluster - distribution"); for (int j = 0; j < skm.getNumClusters(); j++) { int i = 0; for (int clusterNum : assignments) { if (clusterNum == j) System.out.println("Instance " + i + " -> Cluster number: " + clusterNum); i++; } } }
From source file:qoala.arff.java
public void EMClustering(int NumberOfCluster) throws Exception { Instances train = new Instances(dataSet); String[] options = new String[2]; options[0] = "-I"; options[1] = "100"; EM em = new EM(); em.setOptions(options);/*from w ww .j a va 2 s . c o m*/ em.setNumClusters(NumberOfCluster); em.buildClusterer(train); ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(em); eval.evaluateClusterer(train); eval.getNumClusters(); System.out.println("Cluster Evaluation:" + eval.clusterResultsToString()); System.out.println("# - cluster - distribution"); for (int j = 0; j < eval.getNumClusters(); j++) { for (int i = 0; i < train.numInstances(); i++) { int cluster = em.clusterInstance(train.instance(i)); if (cluster == j) System.out.println("Instance " + i + " -> Cluster number: " + cluster); } } }
From source file:qoala.arff.java
public void XMenas() throws Exception { Instances train = new Instances(dataSet); XMeans xm = new XMeans(); xm.setMaxNumClusters(100);/*from www. j a v a 2 s . c o m*/ xm.setMinNumClusters(2); xm.buildClusterer(train); ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(xm); eval.evaluateClusterer(train); eval.getNumClusters(); System.out.println("Cluster Evaluation:" + eval.clusterResultsToString()); System.out.println("# - cluster - distribution"); for (int j = 0; j < eval.getNumClusters(); j++) { for (int i = 0; i < train.numInstances(); i++) { int cluster = xm.clusterInstance(train.instance(i)); if (cluster == j) System.out.println("Instance " + i + " -> Cluster number: " + cluster); } } }
From source file:rdfsystem.data.DataMining.java
public static String cluster(RdfManager manager) throws Exception { Instances ins = transformData(manager, false); SimpleKMeans cls = new SimpleKMeans(); String[] options = "-N 5".split(" "); cls.setOptions(options);//from w w w. jav a 2 s .c o m cls.buildClusterer(ins); ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(cls); eval.evaluateClusterer(ins); return eval.clusterResultsToString(); }
From source file:sirius.clustering.main.TrainClustererPane.java
License:Open Source License
private void start() { if (this.fileTextField.getText().length() == 0) { JOptionPane.showMessageDialog(parent, "Please choose training file!", "Error", JOptionPane.ERROR_MESSAGE); return;//w ww. j av a2s. co m } if (m_ClustererEditor.getValue() == null) { JOptionPane.showMessageDialog(parent, "Please choose clustering method!", "Error", JOptionPane.ERROR_MESSAGE); return; } if (clusterThread != null) { JOptionPane.showMessageDialog(parent, "Cannot start training of Clusterer as another is running!", "Error", JOptionPane.ERROR_MESSAGE); return; } this.startButton.setEnabled(false); this.stopButton.setEnabled(true); this.numberOfClusterTextField.setText(""); clusterThread = (new Thread() { public void run() { try { Instances inst = new Instances(new BufferedReader(new FileReader(fileTextField.getText()))); inst.setClassIndex(m_ClassCombo.getSelectedIndex()); if (inst.classAttribute().isNumeric()) { JOptionPane.showMessageDialog(parent, "Class must be nominal!", "Error", JOptionPane.ERROR_MESSAGE); } else { outputTextArea.setText(""); clusterer = (Clusterer) m_ClustererEditor.getValue(); statusLabel.setText(" Training Clusterer.."); clusterer.buildClusterer(removeClass(inst)); ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(clusterer); eval.evaluateClusterer(inst); outputTextArea.append(eval.clusterResultsToString()); outputTextArea.append("\n"); if (clusterer != null) { numberOfClusterTextField.setText("" + clusterer.numberOfClusters()); statusLabel.setText(" Clusterer Trained.."); } } startButton.setEnabled(true); stopButton.setEnabled(false); clusterThread = null; } catch (Exception e) { e.printStackTrace(); } } }); clusterThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority clusterThread.start(); }
From source file:soccer.core.ASimplePractice.java
public void evaluate() throws IOException, Exception { Instances data = loader.getInstances(); SimpleKMeans cluster = new SimpleKMeans(); cluster.setNumClusters(4);// w w w . j a va 2s . c o m cluster.buildClusterer(data); ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(cluster); eval.evaluateClusterer(data); System.out.println(eval.clusterResultsToString()); }
From source file:soccer.core.classifiers.BookKeeperConsistencyClassifier.java
public static void main(String[] args) throws Exception { BookKeeperConsistency bkc = new BookKeeperConsistency(); Instances data = bkc.getInstances(); RemoveWithValues rwv = new RemoveWithValues(); rwv.setOptions(new String[] { "-C", "4", "-S", "6", "-V" }); rwv.setInputFormat(data);/*w w w . java2s . c om*/ data = Filter.useFilter(data, rwv); RemoveWithValues rwv1 = new RemoveWithValues(); rwv1.setOptions(new String[] { "-C", "6", "-S", "6", "-V" }); rwv1.setInputFormat(data); data = Filter.useFilter(data, rwv1); // Normalize nm = new Normalize(); // nm.setOptions(new String[]{ // "-S", "100" // }); // nm.setInputFormat(data); // data = Filter.useFilter(data, nm); Remove rm = new Remove(); rm.setOptions(new String[] { "-R", "2-last" }); rm.setInputFormat(data); Instances newData = Filter.useFilter(data, rm); SimpleKMeans cluster = new SimpleKMeans(); cluster.setOptions(new String[] { "-N", "2", "-A", "weka.core.ManhattanDistance" }); cluster.buildClusterer(newData); ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(cluster); eval.evaluateClusterer(newData); System.out.println(eval.clusterResultsToString()); // for (int i = 0; i < newData.size(); i++) { // Instance instance = newData.get(i); // if (cluster.clusterInstance(instance) == 0) { // System.out.println(data.get(i).toString()); // } // } }