Guest User

Untitled

a guest
Nov 4th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package Clustering;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7.  
  8. import weka.clusterers.ClusterEvaluation;
  9. import weka.clusterers.DensityBasedClusterer;
  10. import weka.clusterers.EM;
  11. import weka.clusterers.SimpleKMeans;
  12. import weka.core.Instances;
  13.  
  14. /**
  15. * An example class that shows the use of Weka clusterers from Java.
  16. *
  17. * @author FracPete
  18. */
  19.  
  20. public class KMeans {
  21.  
  22.  
  23. public KMeans() throws Exception{
  24.  
  25. BufferedReader reader = new BufferedReader(
  26. new FileReader("input.arff"));
  27. Instances data = new Instances(reader);
  28. reader.close();
  29.  
  30. SimpleKMeans kMeans = new SimpleKMeans();
  31. kMeans.setNumClusters(3);
  32. kMeans.buildClusterer(data);
  33.  
  34. // print out the cluster centroids
  35. Instances centroids = kMeans.getClusterCentroids();
  36. for (int i = 0; i < centroids.numInstances(); i++) {
  37. System.out.print("Centroid ");
  38. System.out.print(i + 1);
  39. System.out.print(": ");
  40. System.out.print(centroids.instance(i));
  41. System.out.println();
  42. }
  43.  
  44. // get cluster membership for each instance
  45. for (int i = 0; i < data.numInstances(); i++) {
  46. System.out.print(data.instance(i));
  47. System.out.print(" is in cluster ");
  48. System.out.print(kMeans.clusterInstance(data.instance(i)) + 1);
  49. System.out.println();
  50.  
  51. }
  52.  
  53. }
  54.  
  55.  
  56.  
  57. public static void main(String[] args){
  58. try {
  59. KMeans p = new KMeans();
  60. } catch (Exception e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. }
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment