package classifier; import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet; import weka.clusterers.SimpleKMeans; import weka.core.Instance; import weka.core.Instances; import weka.core.SelectedTag; /** * Unsupervised Example: K Means Clustering * * @author Andreas T. Meyer-Berg */ public class KMeansClustering extends BasicPacketClassifier { /** * Clusterer */ private SimpleKMeans clusterer; /** * Number of Clusters */ //17 works fine //34 found value anomalies protected int NUMBER_OF_CLUSTERS = 34; protected double[] stdv = new double[NUMBER_OF_CLUSTERS]; /** * Initializes the k means clusterer */ public KMeansClustering() { super(); clusterer = new SimpleKMeans(); clusterer.setSeed(42); //clusterer.setDisplayStdDevs(true); clusterer.setInitializationMethod(new SelectedTag(SimpleKMeans.FARTHEST_FIRST,SimpleKMeans.TAGS_SELECTION)); //clusterer.setCanopyPeriodicPruningRate(100); //clusterer.setCanopyT1(0.001); //clusterer.setCanopyT2(0.1); 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); double[] sumOfSquares = new double[NUMBER_OF_CLUSTERS]; for(Instance i: instances) { /** * Id of the closest cluster centroid */ int x = clusterer.clusterInstance(i); /** * centroid instance */ Instance center = clusterer.getClusterCentroids().get(x); /** * Distance */ double dist = clusterer.getDistanceFunction().distance(center, i); sumOfSquares[x] += dist*dist; } /** * Calculate Standard Deviations */ for(int i = 0; i