HierarchicalClustering.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package classifier;
  2. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  3. import weka.clusterers.HierarchicalClusterer;
  4. import weka.core.EuclideanDistance;
  5. import weka.core.Instance;
  6. import weka.core.Instances;
  7. /**
  8. * Hierarchical Clustering Approach
  9. * @author Andreas T. Meyer-Berg
  10. */
  11. public class HierarchicalClustering extends BasicPacketClassifier {
  12. /**
  13. * Hierarchical cluster which is used
  14. */
  15. private HierarchicalClusterer clusterer;
  16. /**
  17. * Initialize the clusterer
  18. */
  19. public HierarchicalClustering() {
  20. clusterer = new HierarchicalClusterer();
  21. clusterer.setDistanceFunction(new EuclideanDistance());
  22. clusterer.setNumClusters(16);
  23. }
  24. @Override
  25. public void trainModel(Instances instances) {
  26. try {
  27. clusterer.buildClusterer(instances);
  28. } catch (Exception e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. }
  33. @Override
  34. public double classifyInstance(Instance instance, Packet origin) throws Exception {
  35. /**
  36. * Id of the closes cluster centroid
  37. */
  38. int x = clusterer.clusterInstance(instance);
  39. /**
  40. * centroid instance
  41. */
  42. System.out.print(origin.getTextualRepresentation()+": ");
  43. double[] posteriori = clusterer.distributionForInstance(instance);
  44. for(int i = 0; i<posteriori.length; i++) {
  45. System.out.print(posteriori[i]);
  46. if(i<posteriori.length-1)
  47. System.out.print(", ");
  48. }
  49. System.out.println();
  50. return 1;
  51. }
  52. @Override
  53. public long getClassificationStart() {
  54. return 3600000;
  55. }
  56. @Override
  57. public String getAlgoName() {
  58. return "HC";
  59. }
  60. }