-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model_Prediction.py
86 lines (59 loc) · 2.66 KB
/
Model_Prediction.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
import pandas as pd
import os.path
import os
import json
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
import pickle
class Model_Prediction():
Y_labels_reverse = {0: 'Walk', 1: 'OpenDoor', 2: 'CloseDoor', 3: 'BrushTeeth', 4: 'SitDown', 5: 'DrinkGlass',
6: 'CleanTable', 7: 'StandUp', 8: 'PourWater'}
IMU_list = ["lla", "lua", "rla", "rua", "rt", "back"]
data_path_prefix = r"/home/zaid/project/simulationdata"
model_path_prefix = r"/home/zaid/project/model"
prediction_path_prefix = r"/home/zaid/project/IMU_Predication"
output_path = r"/home/zaid/project/output"
IMU = ""
simulated_action_sequnce = ""
def load_model(self):
with open(os.path.join(self.model_path_prefix, f"{self.IMU}_pkl"), "rb") as m:
model = pickle.load(m)
return model
def load_data(self):
df = pd.read_csv(os.path.join(self.data_path_prefix, f"{self.IMU}_test_data.csv"))
return df
def load_simulation_seq(self):
with open('ADL_Simulation_Sequence.json', 'r') as f:
self.simulated_action_sequnce = json.load(f)
def true_labels(self):
y_true = []
for ADL in self.simulated_action_sequnce.keys():
temp_list = [ADL] * int(self.simulated_action_sequnce[ADL])
y_true.extend(temp_list)
return y_true
if __name__ == "__main__":
obj = Model_Prediction()
imu_pred_df = pd.DataFrame()
for IMU in obj.IMU_list:
obj.IMU = IMU
df = obj.load_data()
model = obj.load_model()
y_pred = model.predict(df)
y_pred = [obj.Y_labels_reverse[y] for y in y_pred]
column_name = f"{obj.IMU}_y_pred"
imu_pred_df[column_name] = y_pred
# pred_df.to_csv(os.path.join(obj_imu.prediction_path_prefix, f"{obj_imu.IMU}_pred.csv"), index=False)
print(f"Prediction stored for {obj.IMU} ")
print(f"\nPrediction for IMUs are done\n")
obj.load_simulation_seq()
y_true = obj.true_labels()
y_pred_voting = pd.DataFrame()
imu_pred_df['majority'] = imu_pred_df.mode(axis=1)[0]
print(f" voting output \n {imu_pred_df.head(5)}\n")
y_pred = imu_pred_df['majority']
accuracy_score = accuracy_score(y_true, y_pred)
final_df = pd.DataFrame( list(zip(y_true, y_pred)), columns = ["True", "Predicted"])
final_df.to_csv(os.path.join(obj.output_path, "final_output.csv"), index = False)
# print(f"final output \n{final_df.head(50)}\n")
print(f"\nAccuracy : {accuracy_score}\n")
print(classification_report(y_true, y_pred))