123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package classifier;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
- import weka.clusterers.SimpleKMeans;
- import weka.core.Instance;
- import weka.core.Instances;
- /**
- * Unsupervised Example: K Means Clustering
- *
- * @author Andreas T. Meyer-Berg
- */
- public class KMeansClustering extends BasicPacketClassifier {
- /**
- * Clusterer
- */
- private SimpleKMeans clusterer;
- /**
- * Number of Clusters
- */
- protected int NUMBER_OF_CLUSTERS = 16;
-
- /**
- * Initializes the k means clusterer
- */
- public KMeansClustering() {
- super();
- clusterer = new SimpleKMeans();
- clusterer.setSeed(42);/*
- clusterer.setCanopyPeriodicPruningRate(100);
- clusterer.setCanopyT1(0.5);
- clusterer.setCanopyT2(1.0);*/
- try {
- clusterer.setNumClusters(this.NUMBER_OF_CLUSTERS);
- } catch (Exception e) {
- System.out.println("Error while building cluster");
- e.printStackTrace();
- }
- }
- @Override
- public void trainModel(Instances instances) {
- try {
- clusterer.buildClusterer(instances);
- } catch (Exception e) {
- System.out.println("Failed while training the classifier");
- e.printStackTrace();
- }
- }
- @Override
- public double classifyInstance(Instance instance, Packet origin) throws Exception {
- /**
- * Id of the closest cluster centroid
- */
- int x = clusterer.clusterInstance(instance);
- /**
- * centroid instance
- */
- Instance center = clusterer.getClusterCentroids().get(x);
- return clusterer.getDistanceFunction().distance(center, instance);
- }
- @Override
- public long getClassificationStart() {
- return 3600000;
- }
- @Override
- public String getAlgoName() {
- return "KNN";
- }
- }
|