|
@@ -4,6 +4,7 @@ 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
|
|
@@ -19,8 +20,10 @@ public class KMeansClustering extends BasicPacketClassifier {
|
|
|
|
|
|
/**
|
|
|
* Number of Clusters
|
|
|
- */
|
|
|
- protected int NUMBER_OF_CLUSTERS = 17;
|
|
|
+ */
|
|
|
+ //17 works fine
|
|
|
+ //22 found value anomalies
|
|
|
+ protected int NUMBER_OF_CLUSTERS = 34;
|
|
|
protected double[] stdv = new double[NUMBER_OF_CLUSTERS];
|
|
|
/**
|
|
|
* Initializes the k means clusterer
|
|
@@ -28,10 +31,12 @@ public class KMeansClustering extends BasicPacketClassifier {
|
|
|
public KMeansClustering() {
|
|
|
super();
|
|
|
clusterer = new SimpleKMeans();
|
|
|
- clusterer.setSeed(42);/*
|
|
|
- clusterer.setCanopyPeriodicPruningRate(100);
|
|
|
- clusterer.setCanopyT1(0.5);
|
|
|
- clusterer.setCanopyT2(1.0);*/
|
|
|
+ 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) {
|
|
@@ -70,7 +75,7 @@ public class KMeansClustering extends BasicPacketClassifier {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ private boolean test = true;
|
|
|
@Override
|
|
|
public double classifyInstance(Instance instance, Packet origin) throws Exception {
|
|
|
/**
|
|
@@ -83,6 +88,20 @@ public class KMeansClustering extends BasicPacketClassifier {
|
|
|
Instance center = clusterer.getClusterCentroids().get(x);
|
|
|
|
|
|
double dist = clusterer.getDistanceFunction().distance(center, instance);
|
|
|
+ if(test && dist<stdv[x] && origin.getLabel()!=0) {
|
|
|
+ test = false;
|
|
|
+ System.out.println("Analysis of: "+origin.getTextualRepresentation());
|
|
|
+ System.out.println("Classified as: "+x+" Dist: "+dist+" Stdv: "+stdv[x]);
|
|
|
+ for(int i=0; i<NUMBER_OF_CLUSTERS; i++) {
|
|
|
+ Instance centroid = clusterer.getClusterCentroids().get(i);
|
|
|
+ if(centroid == null)continue;
|
|
|
+ double d = clusterer.getDistanceFunction().distance(centroid, instance);
|
|
|
+
|
|
|
+ System.out.println("Cluster: "+i+" Dist: "+d+" Stdv: "+stdv[i]);
|
|
|
+ }
|
|
|
+ test = false;
|
|
|
+ System.out.println("");
|
|
|
+ }
|
|
|
if(dist < stdv[x])
|
|
|
return 0;
|
|
|
else
|