HierarchicalClustering.java 1.7 KB

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