-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_fusion_svm.py
48 lines (35 loc) · 1.31 KB
/
sensor_fusion_svm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from __future__ import print_function
from sklearn import svm
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report
from numpy import *
import numpy as np
from sklearn.externals import joblib
command_classes = {'count': 0, 'color': 1, 'focus': 2, 'no_op': 3}
data_x = np.loadtxt('data/input_x.csv', dtype=float)
data_y = np.loadtxt('data/input_y.csv', dtype=float)
#
data_x_test = np.loadtxt('data/input_x_test.csv', dtype=float)
data_y_test = np.loadtxt('data/input_y_test.csv', dtype=float)
classifier = svm.SVC(kernel='linear', gamma='scale')
classifier.fit(data_x, data_y)
cross_val_score(classifier, data_x_test, data_y_test, scoring='recall_macro')
command_classes_arr = ['count', 'color', 'focus', 'no_op']
words_file = open("data/words.txt", "r")
words = words_file.read().split('\n')[:-1]
def get_phrase(x):
phrase = ""
for i in range(len(words)):
if x[i] == 1:
phrase += words[i] + " "
return phrase
predictions = classifier.predict(data_x_test)
ar = [(command_classes_arr[int(predictions[i])], get_phrase(data_x_test[i])) for i in range(len(predictions))]
print(ar)
joblib.dump(classifier, 'saved_model.pkl')
#
#
# clf_load = joblib.load('saved_model.pkl')
print(classification_report(data_y_test, predictions))
#
# print(predictions)