|
|
@@ -1,16 +1,17 @@
|
|
|
#!/usr/bin/python3
|
|
|
from sklearn.model_selection import KFold
|
|
|
-from sklearn.neighbors import NearestNeighbors, KNeighborsClassifier
|
|
|
-from sklearn.ensemble import RandomForestClassifier
|
|
|
-from functools import reduce
|
|
|
-import tensorflow as tf
|
|
|
import numpy as np
|
|
|
-import sys
|
|
|
-from Vector import FeatureVector
|
|
|
+try:
|
|
|
+ import sample
|
|
|
+except ImportError:
|
|
|
+ import os
|
|
|
+ import sys
|
|
|
+ sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + \
|
|
|
+ '/../feature-extractor')
|
|
|
+ import sample
|
|
|
|
|
|
DEFAULT_FEATURES = ["average_iat", "high.avg_burst_size", "high.burst_count"]
|
|
|
|
|
|
-
|
|
|
def main():
|
|
|
# a test of this method using an arbitrarily generated list of 5 vectors with
|
|
|
# 3 features each
|
|
|
@@ -24,6 +25,7 @@ def main():
|
|
|
from random import shuffle
|
|
|
shuffle(samples)
|
|
|
features = args.feature if args.feature else DEFAULT_FEATURES
|
|
|
+ from Vector import FeatureVector
|
|
|
data, labels = zip(*[(FeatureVector(p, features).get(), p.user)
|
|
|
for p in samples])
|
|
|
res = kNearestNeighbors(np.array(data), np.array(labels),
|
|
|
@@ -62,6 +64,7 @@ def parse_args():
|
|
|
|
|
|
def kNearestNeighbors(data: list, labels: list,
|
|
|
n=5, verbose=0, k=5, weights="uniform", guesses=1):
|
|
|
+ from sklearn.neighbors import NearestNeighbors, KNeighborsClassifier
|
|
|
folds = KFold(n_splits=n)
|
|
|
i = 1
|
|
|
avg = 0
|
|
|
@@ -91,7 +94,11 @@ def kNearestNeighbors(data: list, labels: list,
|
|
|
return accuracies
|
|
|
|
|
|
|
|
|
+# TODO: This should be in a separate file.
|
|
|
+# If we need a unified interface we can make an aggregater.
|
|
|
+# TODO: KFold validation
|
|
|
def multiLayerPerceptronClassifier(classifications: int, data: list, results: list, testdata: list, testresults: list):
|
|
|
+ import tensorflow as tf
|
|
|
numberOfNeurons = (len(data[0]) + classifications)/2
|
|
|
model = tf.keras.models.Sequential()
|
|
|
model.add(tf.keras.layers.Flatten())
|
|
|
@@ -108,8 +115,11 @@ def multiLayerPerceptronClassifier(classifications: int, data: list, results: li
|
|
|
print(loss)
|
|
|
print(accuracy)
|
|
|
|
|
|
-
|
|
|
+# TODO: This should be in a separate file.
|
|
|
+# If we need a unified interface we can make an aggregater.
|
|
|
+# TODO: KFold validation
|
|
|
def randomForest(data: list, labels: list, test_data: list, test_data_labels: list):
|
|
|
+ from sklearn.ensemble import RandomForestClassifier
|
|
|
rfc = RandomForestClassifier(n_estimators=10)
|
|
|
rfc.fit(data, labels)
|
|
|
predictions = rfc.predict(test_data)
|