Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Clustering;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import weka.clusterers.ClusterEvaluation;
- import weka.clusterers.DensityBasedClusterer;
- import weka.clusterers.EM;
- import weka.clusterers.SimpleKMeans;
- import weka.core.Instances;
- /**
- * An example class that shows the use of Weka clusterers from Java.
- *
- * @author FracPete
- */
- public class KMeans {
- public KMeans() throws Exception{
- BufferedReader reader = new BufferedReader(
- new FileReader("input.arff"));
- Instances data = new Instances(reader);
- reader.close();
- SimpleKMeans kMeans = new SimpleKMeans();
- kMeans.setNumClusters(3);
- kMeans.buildClusterer(data);
- // print out the cluster centroids
- Instances centroids = kMeans.getClusterCentroids();
- for (int i = 0; i < centroids.numInstances(); i++) {
- System.out.print("Centroid ");
- System.out.print(i + 1);
- System.out.print(": ");
- System.out.print(centroids.instance(i));
- System.out.println();
- }
- // get cluster membership for each instance
- for (int i = 0; i < data.numInstances(); i++) {
- System.out.print(data.instance(i));
- System.out.print(" is in cluster ");
- System.out.print(kMeans.clusterInstance(data.instance(i)) + 1);
- System.out.println();
- }
- }
- public static void main(String[] args){
- try {
- KMeans p = new KMeans();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment