12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package classifier;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
- import weka.clusterers.HierarchicalClusterer;
- import weka.core.EuclideanDistance;
- import weka.core.Instance;
- import weka.core.Instances;
- /**
- * Hierarchical Clustering Approach
- * @author Andreas T. Meyer-Berg
- */
- public class HierarchicalClustering extends BasicPacketClassifier {
- /**
- * Hierarchical cluster which is used
- */
- private HierarchicalClusterer clusterer;
-
- /**
- * Initialize the clusterer
- */
- public HierarchicalClustering() {
- clusterer = new HierarchicalClusterer();
- clusterer.setDistanceFunction(new EuclideanDistance());
- clusterer.setNumClusters(16);
- }
-
- @Override
- public void trainModel(Instances instances) {
- try {
- clusterer.buildClusterer(instances);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public double classifyInstance(Instance instance, Packet origin) throws Exception {
- /**
- * Id of the closes cluster centroid
- */
- int x = clusterer.clusterInstance(instance);
- /**
- * centroid instance
- */
- System.out.print(origin.getTextualRepresentation()+": ");
- double[] posteriori = clusterer.distributionForInstance(instance);
- for(int i = 0; i<posteriori.length; i++) {
- System.out.print(posteriori[i]);
- if(i<posteriori.length-1)
- System.out.print(", ");
- }
- System.out.println();
- return 1;
- }
- @Override
- public long getClassificationStart() {
- return 3600000;
- }
- @Override
- public String getAlgoName() {
- return "HC";
- }
- }
|