Pārlūkot izejas kodu

Nearest neighbor no longer requires tensorflow installed.

Tom Flucke 6 gadi atpakaļ
vecāks
revīzija
79a031e3c6
2 mainītis faili ar 26 papildinājumiem un 13 dzēšanām
  1. 8 5
      src/classifiers/Vector.py
  2. 18 8
      src/classifiers/nearestneighbors.py

+ 8 - 5
src/classifiers/Vector.py

@@ -3,13 +3,16 @@ try:
     import cPickle as pickle
 except ImportError:
     import pickle
-import os
-import sys
 import typing
 from typing import List
-sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + \
-                '/../feature-extractor')
-import sample
+try:
+    import sample
+except ImportError:
+    import os
+    import sys
+    sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + \
+                    '/../feature-extractor')
+    import sample
 
 class FeatureVector:
     def __init__(self, s: sample, features = None):

+ 18 - 8
src/classifiers/nearestneighbors.py

@@ -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)