List of usage examples for weka.clusterers HierarchicalClusterer HierarchicalClusterer
HierarchicalClusterer
From source file:lu.lippmann.cdb.lab.beta.util.WekaUtil2.java
License:Open Source License
/** * //w ww . jav a 2 s. com * @param newInstances * @param K * @return * @throws Exception */ public static List<IndexedInstance> doHAC(final Instances instances, final int K) throws Exception { final HierarchicalClusterer clusterer = new HierarchicalClusterer(); clusterer.setOptions(Utils.splitOptions("-N " + K + " -L MEAN -P -A weka.core.EuclideanDistance")); return computeClusters(clusterer, instances); }
From source file:myclusterer.WekaCode.java
public static Clusterer buildClusterer(Instances dataSet, int clusterType) throws Exception { Clusterer clusterer = null;//from ww w . jav a2s .co m if (clusterType == SimpleKMeans) { SimpleKMeans kmeans = new SimpleKMeans(); Scanner scan = new Scanner(System.in); System.out.print("Masukkan jumlah cluster: "); int K = scan.nextInt(); kmeans.setNumClusters(K); clusterer = kmeans; clusterer.buildClusterer(dataSet); } else if (clusterType == HierarchicalClusterer) { HierarchicalClusterer hierarchical = new HierarchicalClusterer(); Scanner scan = new Scanner(System.in); System.out.print("Masukkan jumlah cluster: "); int K = scan.nextInt(); hierarchical.setNumClusters(K); clusterer = hierarchical; clusterer.buildClusterer(dataSet); } else if (clusterType == MyKMeans) { MyKMeans kmeans = new MyKMeans(); Scanner scan = new Scanner(System.in); System.out.print("Masukkan jumlah cluster: "); int K = scan.nextInt(); kmeans.setNumClusters(K); clusterer = kmeans; clusterer.buildClusterer(dataSet); } else if (clusterType == MyAgnes) { MyAgnes agnes = new MyAgnes(); Scanner scan = new Scanner(System.in); System.out.print("Masukkan jumlah cluster: "); int K = scan.nextInt(); agnes.setNumClusters(K); clusterer = agnes; clusterer.buildClusterer(dataSet); } return clusterer; }
From source file:net.sf.mzmine.modules.peaklistmethods.dataanalysis.clustering.hierarchical.HierarClusterer.java
License:Open Source License
@Override public ClusteringResult performClustering(Instances dataset, ParameterSet parameters) { HierarchicalClusterer clusterer = new HierarchicalClusterer(); String[] options = new String[5]; LinkType link = parameters.getParameter(HierarClustererParameters.linkType).getValue(); DistanceType distanceType = parameters.getParameter(HierarClustererParameters.distanceType).getValue(); options[0] = "-L"; options[1] = link.name();// w w w .j a v a 2 s . com options[2] = "-A"; switch (distanceType) { case EUCLIDIAN: options[3] = "weka.core.EuclideanDistance"; break; case CHEBYSHEV: options[3] = "weka.core.ChebyshevDistance"; break; case MANHATTAN: options[3] = "weka.core.ManhattanDistance"; break; case MINKOWSKI: options[3] = "weka.core.MinkowskiDistance"; break; } options[4] = "-P"; try { clusterer.setOptions(options); clusterer.setPrintNewick(true); clusterer.buildClusterer(dataset); // clusterer.graph() gives only the first cluster and in the case // there // are more than one cluster the variables in the second cluster are // missing. // I'm using clusterer.toString() which contains all the clusters in // Newick format. ClusteringResult result = new ClusteringResult(null, clusterer.toString(), clusterer.getNumClusters(), null); return result; } catch (Exception ex) { logger.log(Level.SEVERE, null, ex); return null; } }
From source file:nl.uva.sne.classifiers.Hierarchical.java
@Override public Map<String, String> cluster(String inDir) throws IOException, ParseException { try {//w w w . j a v a2 s . 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:org.iobserve.analysis.behavior.clustering.hierarchical.HierarchicalClustering.java
License:Apache License
/** * Manage the finding and creation of a "good" clustering of a given input data set. * * @param instances//from w w w . ja v a2 s .c o m * Input data set. */ @Override public Map<Integer, List<Pair<Instance, Double>>> clusterInstances(final Instances instances) { Map<Integer, List<Pair<Instance, Double>>> clusteringResults = new HashMap<>(); // NOPMD // Create hierarchical clusterer and set its options. HierarchicalClustering.hierarchicalClusterer = new HierarchicalClusterer(); HierarchicalClustering.hierarchicalClusterer.setDistanceFunction(this.distanceFunction); HierarchicalClustering.hierarchicalClusterer.setDistanceIsBranchLength(false); HierarchicalClustering.hierarchicalClusterer.setLinkType(this.linkage); // Find a "good" number of clusters by applying the clusterSelectionMethod. final ClustersSelector clusterSelection = new ClustersSelector(this.clusterSelectionMethod, HierarchicalClustering.hierarchicalClusterer, instances); clusteringResults = clusterSelection.findGoodClustering(); // Write clusteringResults to a CSV file. final CSVSinkFilter csvFilter = new CSVSinkFilter(); final Map<Double, List<Instance>> clusteringKVs = csvFilter .convertClusteringResultsToKVPair(clusteringResults); try { csvFilter.createCSVFromClusteringResult(this.outputPath, clusteringKVs); } catch (final IOException e) { HierarchicalClustering.LOGGER.error("Writing hierarchical clustering results to csv failed.", e); } HierarchicalClustering.LOGGER.info("Hierarchical clustering done."); return clusteringResults; }
From source file:swm.project.mappings.UserToUserCluster.java
private void clusterWithDBSCAN() throws FileNotFoundException, IOException, Exception { Reader reader;/* w w w . j a va 2 s. co m*/ reader = new FileReader(MappingConstants.USER_MOVIE_CLUSTERS); Instances instanceValues = new Instances(reader); HierarchicalClusterer h = new HierarchicalClusterer(); h.setNumClusters(20); h.setPrintNewick(true); h.buildClusterer(instanceValues); }
From source file:wekimini.InputGenerator.java
public void selectHiClusters() throws Exception { buildDataset();// w w w. j a va 2s . com HierarchicalClusterer clusterer = new HierarchicalClusterer(); clusterer.setLinkType(new SelectedTag(1, HierarchicalClusterer.TAGS_LINK_TYPE)); clusterer.buildClusterer(dataset); logger.log(Level.SEVERE, "EM options: ", clusterer.getOptions()); logger.log(Level.SEVERE, "EM numCluster tip text: ", clusterer.numClustersTipText()); logger.log(Level.SEVERE, "EM debug tip text: ", clusterer.debugTipText()); logger.log(Level.SEVERE, "EM info: ", clusterer.globalInfo()); int num = clusterer.numberOfClusters(); String txt = Integer.toString(num); logger.log(Level.SEVERE, "EM numberOfClusters: ", txt); String text = Integer.toString(numClusters); logger.log(Level.SEVERE, "EM numClusters: ", text); }