Java Code Examples for weka.clusterers.SimpleKMeans#setPreserveInstancesOrder()
The following examples show how to use
weka.clusterers.SimpleKMeans#setPreserveInstancesOrder() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WekaClusterTest.java From Java-Data-Science-Cookbook with MIT License | 6 votes |
public void clusterData(){ kmeans = new SimpleKMeans(); kmeans.setSeed(10); try { kmeans.setPreserveInstancesOrder(true); kmeans.setNumClusters(10); kmeans.buildClusterer(cpu); int[] assignments = kmeans.getAssignments(); int i = 0; for(int clusterNum : assignments) { System.out.printf("Instance %d -> Cluster %d\n", i, clusterNum); i++; } } catch (Exception e1) { } }
Example 2
Source File: Clustering.java From java-ml-projects with Apache License 2.0 | 5 votes |
private List<Series<Number, Number>> buildClusteredSeries() throws Exception { List<XYChart.Series<Number, Number>> clusteredSeries = new ArrayList<>(); // to build the cluster we remove the class information Remove remove = new Remove(); remove.setAttributeIndices("3"); remove.setInputFormat(data); Instances dataToBeClustered = Filter.useFilter(data, remove); SimpleKMeans kmeans = new SimpleKMeans(); kmeans.setSeed(10); kmeans.setPreserveInstancesOrder(true); kmeans.setNumClusters(3); kmeans.buildClusterer(dataToBeClustered); IntStream.range(0, 3).mapToObj(i -> { Series<Number, Number> newSeries = new XYChart.Series<>(); newSeries.setName(String.valueOf(i)); return newSeries; }).forEach(clusteredSeries::add); int[] assignments = kmeans.getAssignments(); for (int i = 0; i < assignments.length; i++) { int clusterNum = assignments[i]; clusteredSeries.get(clusterNum).getData().add(instancetoChartData(data.get(i))); } return clusteredSeries; }
Example 3
Source File: Ex06_Clusterers.java From tsml with GNU General Public License v3.0 | 4 votes |
public static void main(String[] args) throws Exception { // We'll use this data throughout, see Ex01_Datahandling int seed = 0; Instances[] trainTest = DatasetLoading.sampleItalyPowerDemand(seed); Instances inst = trainTest[0]; Instances inst2 = trainTest[1]; inst.addAll(inst2); // Create an object from one of the time series or vector clusters implemented. // Call the buildClusterer method with your data. Most clusters will need the number of clusters k to be set. UnsupervisedShapelets us = new UnsupervisedShapelets(); us.setNumberOfClusters(inst.numClasses()); us.buildClusterer(inst); // You can find the cluster assignments for each data instance by calling getAssignments(). // The index of assignments array will match the Instances object, i.e. index 0 with value 1 == first instance // of data assigned to cluster 1. int[] tsAssignments = us.getAssignments(); System.out.println("UnsupervisedShapelets cluster assignments:"); System.out.println(Arrays.toString(tsAssignments)); // A popular metric for cluster evaluation is the Rand index. A utility method is available for calculating // this. double tsRandIndex = ClusteringUtilities.randIndex(tsAssignments, inst); System.out.println("UnsupervisedShapelets Rand index:"); System.out.println(tsRandIndex); // weka also implements a range of clustering algorithms. Any class value must be removed prior to use. Instances copy = new Instances(inst); deleteClassAttribute(copy); SimpleKMeans km = new SimpleKMeans(); km.setNumClusters(inst.numClasses()); km.setPreserveInstancesOrder(true); km.buildClusterer(copy); int[] wekaAssignments = km.getAssignments(); System.out.println("SimpleKMeans cluster assignments:"); System.out.println(Arrays.toString(wekaAssignments)); double wekaRandIndex = ClusteringUtilities.randIndex(wekaAssignments, inst); System.out.println("SimpleKMeans Rand index:"); System.out.println(wekaRandIndex); }