List of usage examples for weka.clusterers SimpleKMeans setSeed
@Override public void setSeed(int value)
From source file:kmeansapps.Kmeans.java
public void startCluster(String path, int numOfCluster, JTable tableResult, JFrame apps) { try {/*from w w w . jav a 2 s .c o m*/ // TODO code application logic here SimpleKMeans kmeans = new SimpleKMeans(); String[] columnNames = new String[numOfCluster]; kmeans.setSeed(10); kmeans.setPreserveInstancesOrder(true); kmeans.setNumClusters(numOfCluster); BufferedReader datafile = readDataFile(path); Instances data = new Instances(datafile); kmeans.buildClusterer(data); double SSE = kmeans.getSquaredError(); // This array returns the cluster number (starting with 0) for each instance // The array has as many elements as the number of instances int[] assignments = kmeans.getAssignments(); // //setting columNames // for (int i = 0; i < numOfCluster; i++) { // columnNames[i] = "Cluster "+i+""; // } // bikin arraylist 2 dimensi untuk menampung instance masuk ke cluster berapa. ArrayList<ArrayList<String>> listOfCluster = new ArrayList<ArrayList<String>>(); ArrayList<String> listMemberOfCluster; //tambahkan list cluster for (int i = 0; i < numOfCluster; i++) { listMemberOfCluster = new ArrayList<>(); listOfCluster.add(listMemberOfCluster); } //tambahkan anggota list ke cluster int j = 0; for (int clusterNum : assignments) { listOfCluster.get(clusterNum).add(j + ""); j++; } for (int i = 0; i < listOfCluster.size(); i++) { System.out.print("Cluster - " + i + " -> "); for (String listMemberOfCluster1 : listOfCluster.get(i)) { System.out.print(listMemberOfCluster1 + " "); } System.out.println(""); } // int i=0; // for(int clusterNum : assignments) { // System.out.printf("Instance %d -> Cluster %d \n", i, clusterNum); // i++; // System.out.println(SSE); // } // //output to table // tableResult.setModel(new DefaultTableModel( // new Object[][]{ // }, // columnNames)); // apps.setVisible(true); // // int j=0; // DefaultTableModel model = (DefaultTableModel) tableResult.getModel(); // for(int clusterNum : assignments) { // if (clusterNum==0){ // model.addRow(new Object[]{j, "", "", "", "", ""}); // } // else if (clusterNum==1){ // model.addRow(new Object[]{"", j, "", "", "", ""}); // } // else if (clusterNum==2){ // model.addRow(new Object[]{"", "", j, "", "", ""}); // } // else if (clusterNum==3){ // model.addRow(new Object[]{"", "", "", j, "", ""}); // } // else if (clusterNum==4){ // model.addRow(new Object[]{"", "", "", "", j, ""}); // } // else if (clusterNum==5){ // model.addRow(new Object[]{"", "", "", "", "", j}); // } // // j++; // } } catch (Exception ex) { Logger.getLogger(Kmeans.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:kmeansapps.Kmeans.java
public void startCluster(String path, int numOfCluster, JTextArea textarea) { try {//w w w . ja v a 2 s . c om // TODO code application logic here SimpleKMeans kmeans = new SimpleKMeans(); String[] columnNames = new String[numOfCluster]; kmeans.setSeed(10); kmeans.setPreserveInstancesOrder(true); kmeans.setNumClusters(numOfCluster); BufferedReader datafile = readDataFile(path); Instances data = new Instances(datafile); kmeans.buildClusterer(data); double SSE = kmeans.getSquaredError(); // This array returns the cluster number (starting with 0) for each instance // The array has as many elements as the number of instances int[] assignments = kmeans.getAssignments(); // bikin arraylist 2 dimensi untuk menampung instance masuk ke cluster berapa. ArrayList<ArrayList<String>> listOfCluster = new ArrayList<ArrayList<String>>(); ArrayList<String> listMemberOfCluster; //tambahkan list cluster for (int i = 0; i < numOfCluster; i++) { listMemberOfCluster = new ArrayList<>(); listOfCluster.add(listMemberOfCluster); } //tambahkan anggota list ke cluster int j = 0; for (int clusterNum : assignments) { listOfCluster.get(clusterNum).add(j + ""); j++; } textarea.setText(""); String result = ""; for (int i = 0; i < listOfCluster.size(); i++) { result = result + ("Cluster - " + i + " ==> "); for (String listMemberOfCluster1 : listOfCluster.get(i)) { result = result + (listMemberOfCluster1 + " "); } result = result + ("\n"); } result = result + ("\nSSE : ") + kmeans.getSquaredError(); textarea.setText(result); } catch (Exception ex) { Logger.getLogger(Kmeans.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:nl.uva.sne.classifiers.Kmeans.java
@Override public Map<String, String> cluster(String inDir) throws IOException, ParseException { try {//from w w w . ja v a 2s. c o m Instances data = ClusterUtils.terms2Instances(inDir, false); DistanceFunction df; // SimpleKMeans currently only supports the Euclidean and Manhattan distances. switch (distanceFunction) { case "Euclidean": df = new EuclideanDistance(data); break; case "Manhattan": df = new ManhattanDistance(data); break; default: df = new EuclideanDistance(data); break; } SimpleKMeans clusterer = new SimpleKMeans(); Random rand = new Random(System.currentTimeMillis()); int seed = rand.nextInt((Integer.MAX_VALUE - 1000000) + 1) + 1000000; clusterer.setSeed(seed); clusterer.setMaxIterations(1000000000); Logger.getLogger(Kmeans.class.getName()).log(Level.INFO, "Start clusteing"); clusterer.setPreserveInstancesOrder(true); clusterer.setNumClusters(numOfClusters); clusterer.setDistanceFunction(df); return ClusterUtils.bulidClusters(clusterer, data, inDir); } catch (Exception ex) { Logger.getLogger(Kmeans.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:org.montp2.m1decol.ter.clustering.KMeansClustering.java
License:Open Source License
public Clusterer computeClustering(String inPath, String outPath, Properties propertiesCluster) throws Exception { Instances inputInstances = WekaUtils.loadARFF(inPath); EuclideanDistance euclideanDistance = new EuclideanDistance(); euclideanDistance.setAttributeIndices("first-last"); euclideanDistance.setDontNormalize(false); euclideanDistance.setInvertSelection(false); SimpleKMeans kmeans = new SimpleKMeans(); kmeans.setPreserveInstancesOrder(/* ww w . j ava 2s . c o m*/ Boolean.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.PERSERVE_INSTANCE))); kmeans.setDontReplaceMissingValues(Boolean .valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.DONT_REPLACE_MISSING_VALUES))); kmeans.setDisplayStdDevs( Boolean.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.DISPLAY_STD_DEVS))); kmeans.setMaxIterations( Integer.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.MAX_ITERATIONS))); kmeans.setNumClusters( Integer.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.NUM_CLUSTERS))); kmeans.setSeed(10); //kmeans.setSeed( // Integer.valueOf(propertiesCluster.getProperty(ClusterProperties.Kmeans.SEED))); kmeans.setDistanceFunction(euclideanDistance); kmeans.buildClusterer(inputInstances); WekaUtils.saveModel(kmeans, outPath); /* * * Pour obtenir les pourcentages de les clusters * ClusterEvaluation eval = new ClusterEvaluation(); * eval.setClusterer(kmeans); * eval.evaluateClusterer(inputInstances); * System.out.println(eval.clusterResultsToString()); * * */ return kmeans; }
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);//from ww w . ja v a 2 s. co 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++; } } }