-
Notifications
You must be signed in to change notification settings - Fork 0
/
clf_predict.py
87 lines (71 loc) · 2.56 KB
/
clf_predict.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
from collections import deque
import numpy as np
import pandas as pd
from sklearn import model_selection
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import sensors
class Predict():
# Xnew = [[7380, 204, 14644, 69, -117, 191]]
start_time = None
elapsed_time = None
dq = deque()
# classifier
clf = LinearDiscriminantAnalysis()
def __init__(self):
X = []
Y = []
for filename in os.listdir('data'):
data = pd.read_csv('data/'+filename, sep=',', header=None, index_col=0)
# print(data)
for i in data.index.unique():
# Index is activity ID
# Y.append(i)
# X.append(data.loc[i].values[:250]) #TODO: Change this maximum value to some better dataset
for coords in data.loc[i].values:
Y.append(i)
X.append(coords)
# Split data
X_train, X_test, y_train, y_test = model_selection.train_test_split(
X, Y,
test_size=0.3,
random_state=100)
self.clf.fit(X_train, y_train)
def predict(self, data: sensors.SensorData):
if data is None:
return None
elif self.start_time is None:
self.start_time = time.time()
self.elapsed_time = time.time() - self.start_time
self.dq.append(
{'time': self.elapsed_time, 'data': sensors.SensorData(*data.clf_data())})
# if time more than 1 second
if self.dq and self.elapsed_time - self.dq[0]['time'] >= 1:
save_data = data - self.dq.popleft()['data']
# data_tobe_classified = pd.read_csv('new_data.csv',
# sep=',',
# header=None)
return self.clf.predict(np.asarray([(list(save_data.clf_data()))]))
def main():
p = Predict()
s = sensors.Sensors()
data = None
try:
while True:
data = s.read() or data
y_pred = p.predict(data)
print("\r%s" % data, end='')
if y_pred is None:
print(" " * 25, end='')
else:
print(" Prediction: %s" % (y_pred) + " " * 5, end='')
except KeyboardInterrupt:
print("Interrupted." + " " * 36)
if __name__ == "__main__":
main()