瀏覽代碼

added file writing utility

Ethan Goldfarb 6 年之前
父節點
當前提交
ff1cd4c969
共有 2 個文件被更改,包括 14 次插入4 次删除
  1. 4 1
      ethan_data_processing_scripts/README.md
  2. 10 3
      ethan_data_processing_scripts/nearestneighbors.py

+ 4 - 1
ethan_data_processing_scripts/README.md

@@ -10,7 +10,10 @@ Results should be ints in an array, each result accoring to the list of features
 
 ### Current state of nearestneighbors.py
 
-Usage: nearestneighbors.py datafile.bin classificationsfile.bin testdatafile.bin
+Usage: nearestneighbors.py datafile.bin classificationsfile.bin testdatafile.bin -p/e
+
+if -p, a tuple of the pickle dump of the test data array and their classifications are written
+if -e, an english copy of the printout is written
 
 A command line utility that reads in FeatureVectors and runs a KNN classification on them.
 

+ 10 - 3
ethan_data_processing_scripts/nearestneighbors.py

@@ -8,8 +8,8 @@ def main():
     # a test of this method using an arbitrarily generated list of 5 vectors with 3 features each
     # nearestNeighbors([[1, 1, 0], [1, 0, 0], [0, 0, 0], [0, 5, 5]], [[1, 1, 4]])
     print(len(sys.argv))
-    if len(sys.argv) != 4:
-        print("Usage: nearestneighbors.py datafile.bin classificationsfile.bin testdatafile.bin")
+    if len(sys.argv) != 5:
+        print("Usage: nearestneighbors.py datafile.bin classificationsfile.bin testdatafile.bin -(p/e)")
         exit()
     data = readPickledData(sys.argv[1])
     classifcations = readPickledData(sys.argv[2])
@@ -29,9 +29,16 @@ def main():
 def kNearestNeighbors(data: list, classifications: list, test_data: list):
     kn = KNeighborsClassifier(n_neighbors=2)
     kn.fit(data, classifications)
+    p = kn.predict(test_data)
     print("Predictions, matching test_data by index: ")
     print(test_data)
-    print(kn.predict(test_data))
+    print(p)
+    writestr = "Predictions, matching test_data by index:\n" + str(test_data) + "\n" + str(p)
+    if sys.argv[4][1] == 'p':
+        pickle.dump((test_data, p), open("results.bin", "wb"))
+    else:
+        with open("results.txt", "w+") as file:
+            file.write(writestr)
 
 
 def nearestNeighbors(data: list, test_data: list):