List of usage examples for weka.clusterers FilteredClusterer buildClusterer
@Override public void buildClusterer(Instances data) throws Exception
From source file:model.clustering.Clustering.java
public String filledFile(Instances data, int numOfClusters, String remove) throws Exception { String mainData = data.toString(); int index = mainData.indexOf("@data"); String clusterData = mainData.substring(0, index + 6); Remove removeFilter = new Remove(); removeFilter.setAttributeIndices(remove); kMeansCLusterer = new SimpleKMeans(); kMeansCLusterer.setNumClusters(numOfClusters); FilteredClusterer filteredClusterer = new FilteredClusterer(); filteredClusterer.setClusterer(kMeansCLusterer); filteredClusterer.setFilter(removeFilter); filteredClusterer.buildClusterer(data); Enumeration<Instance> newData = data.enumerateInstances(); eval = new ClusterEvaluation(); eval.setClusterer(filteredClusterer); eval.evaluateClusterer(data);//from www. j av a 2 s . c o m while (newData.hasMoreElements()) { Instance i = (Instance) newData.nextElement(); int kluster = filteredClusterer.clusterInstance(i); String instanceString = i.toString() + "," + kluster; clusterData = clusterData + instanceString + "\n"; } return clusterData; }
From source file:nl.uva.sne.classifiers.Hierarchical.java
@Override public Map<String, String> cluster(String inDir) throws IOException, ParseException { try {/*from w w w. ja v a2s . c o m*/ Instances data = ClusterUtils.terms2Instances(inDir, false); // ArffSaver s = new ArffSaver(); // s.setInstances(data); // s.setFile(new File(inDir+"/dataset.arff")); // s.writeBatch(); DistanceFunction df; // SimpleKMeans currently only supports the Euclidean and Manhattan distances. switch (distanceFunction) { case "Minkowski": df = new MinkowskiDistance(data); break; case "Euclidean": df = new EuclideanDistance(data); break; case "Chebyshev": df = new ChebyshevDistance(data); break; case "Manhattan": df = new ManhattanDistance(data); break; default: df = new EuclideanDistance(data); break; } Logger.getLogger(Hierarchical.class.getName()).log(Level.INFO, "Start clusteing"); weka.clusterers.HierarchicalClusterer clusterer = new HierarchicalClusterer(); clusterer.setOptions(new String[] { "-L", "COMPLETE" }); clusterer.setDebug(true); clusterer.setNumClusters(numOfClusters); clusterer.setDistanceFunction(df); clusterer.setDistanceIsBranchLength(true); clusterer.setPrintNewick(false); weka.clusterers.FilteredClusterer fc = new weka.clusterers.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); // // Print normal // clusterer.setPrintNewick(false); // System.out.println(clusterer.graph()); // // Print Newick // clusterer.setPrintNewick(true); // System.out.println(clusterer.graph()); // // // Let's try to show this clustered data! // JFrame mainFrame = new JFrame("Weka Test"); // mainFrame.setSize(600, 400); // mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Container content = mainFrame.getContentPane(); // content.setLayout(new GridLayout(1, 1)); // // HierarchyVisualizer visualizer = new HierarchyVisualizer(clusterer.graph()); // content.add(visualizer); // // mainFrame.setVisible(true); return ClusterUtils.bulidClusters(clusterer, data, inDir); } catch (Exception ex) { Logger.getLogger(Hierarchical.class.getName()).log(Level.SEVERE, null, ex); } return null; }
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); 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); }//www . ja v a2 s . co m 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:view.centerPanels.ClusteringPredictPnlCenter.java
private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed Instances test = new Instances(Data.getInstance().getInstances()); test.delete();// w w w. j av a 2 s.c om //proverava da li su dobro unete vrednosti //ako nesto nije doro uneseno nekaa iskoci JoptionPane //sta je lose uneseno, naziv aributa recimo for (int i = 0; i < fields.size(); i++) { String text = fields.get(i).getText().trim(); //prekace prazna pollja jer za klasterizaciju znaci da se ona ignorisu //to za klasifikaciju nije slucaj if (!text.equals("")) { if (test.attribute(i).isNominal()) { boolean correct = false; for (int j = 0; j < test.attribute(i).numValues(); j++) { if (text.equals(test.attribute(i).value(j))) { correct = true; } } if (!correct) { JOptionPane.showMessageDialog(this, "Incorrect format for attribute " + test.attribute(i).name()); break; } } if (test.attribute(i).isNumeric()) { try { double value = Double.parseDouble(text); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Incorrect format for attribute " + test.attribute(i).name()); break; } } } } int numAttributes = test.numAttributes(); Instance instance = new Instance(numAttributes); //ovaj remove je potreban samo zaklasterizaciju String remove = ""; boolean hasRemove = false; for (int i = 0; i < fields.size(); i++) { String text = fields.get(i).getText().trim(); //vama ne sme da se pojavi prazan string if (text.equals("")) { remove = remove + (i + 1) + ","; hasRemove = true; } else { try { double value = Double.parseDouble(text); instance.setValue(i, value); } catch (Exception e) { instance.setValue(i, text); } } } if (hasRemove) { remove = remove.substring(0, remove.length() - 1); } //meni se InstanceS zove test a vama instances, ovako se dodaje ta jedna instanca test.add(instance); //sad radite vasu evaluaciju ovo je klaserizacija ostalo Remove removeFilter = new Remove(); removeFilter.setAttributeIndices(remove); FilteredClusterer filteredClusterer = new FilteredClusterer(); try { filteredClusterer.setClusterer(kMeans); filteredClusterer.setFilter(removeFilter); filteredClusterer.buildClusterer(Data.getInstance().getInstances()); } catch (Exception e) { } ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(filteredClusterer); try { eval.evaluateClusterer(test); } catch (Exception ex) { Logger.getLogger(ClusteringPredictPnlCenter.class.getName()).log(Level.SEVERE, null, ex); } String[] results = eval.clusterResultsToString().split("\n"); String cluster = results[results.length - 1].split(" ")[0]; textAreaResult.setText("This instance belongs to \ncluster number: " + cluster + ".\n\n" + "Take a look on visualization \nfor better feeleing about \nthis instance"); test.delete(); }