-
Notifications
You must be signed in to change notification settings - Fork 1
/
Predictions.py
173 lines (97 loc) · 2.9 KB
/
Predictions.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# coding: utf-8
# In[59]:
import numpy as np
import scipy
from sklearn.externals import joblib
import pandas as pd
from hmmlearn import hmm
import math
# In[60]:
def loadData(file_name='',data_type='float'):
data=np.loadtxt('./data/'+file_name, delimiter=',',dtype=data_type)
return data
# In[61]:
XRAW=loadData('Label.csv')
Obs=loadData('Observations.csv')
# In[62]:
labels_1quad = XRAW
for i in range(len(XRAW)):
labels_1quad[i,2] += 0
labels_1quad[i, 3] += 0
# In[65]:
from sklearn.externals import joblib
model = joblib.load("./pickles/Soorya/3_10000_600_6states_50iter.pkl")
# In[66]:
n_states = 6
steps = 600
train_runs = 6000
test_runs = 4000
runs = 10000
lengths_arr_train = np.array(train_runs * [steps])
lengths_arr_test = np.array(test_runs * [steps])
# In[67]:
Obs_aug_train = Obs[:train_runs,1000-steps:]
Obs_row_train = Obs_aug_train.flatten().reshape(-1,1)
Obs_aug_test = Obs[train_runs:,1000-steps:]
Obs_row_test = Obs_aug_test.flatten().reshape(-1,1)
# In[68]:
Z_train = model.predict(Obs_row_train, lengths = lengths_arr_train)
Z_train = np.reshape(Z_train, (train_runs, steps))
# In[69]:
print(Z_train.shape)
# In[70]:
length_labels = len(labels_1quad)
pair = []
state_pairs = []
labels_map = [{} for i in range(length_labels)]
#labels_map[run,{step:[x,y]},{},{}....]
for row in range(length_labels):
(labels_map[int(labels_1quad[row,0]-1)]).update({labels_1quad[row,1]-1:[labels_1quad[row,2],labels_1quad[row,3]]})
# In[71]:
states_maps = [[] for i in range(n_states)]
for run in range(train_runs):
labels_steps = labels_map[run] #dictionary {step:[x,y], step:[,]...)
for step in range(steps):
if step in labels_steps:
states_maps[Z_train[run,step]].append(labels_steps[step])
# In[72]:
states_maps_mean = [[]]*n_states
avgX = 0
avgY = 0
count = 0
for i in range(len(states_maps)):
for j in range(len(states_maps[i])):
avgX += states_maps[i][j][0]
avgY += states_maps[i][j][1]
count+=1
avgX = avgX/count
avgY = avgY/count
states_maps_mean[i]= [avgX,avgY]
avgX = 0
avgY = 0
count = 0
# In[73]:
Z_test = model.predict(Obs_row_test, lengths = lengths_arr_test)
Z_test = np.reshape(Z_test, (test_runs, steps))
# In[74]:
print(Z_test.shape)
thousandth_states = Z_test[:,steps-1]
#print(Z_test[0])
#print(thousandth_states)
next_states = []
for state in thousandth_states:
next_states.append(np.argmax(model.transmat_[state]))
# In[75]:
import csv
with open('./outputs/output_6st_50iter.csv','a') as f:
headers = ['id','value']
writer = csv.writer(f)
writer.writerow(headers)
for i in range(len(next_states)):
X,Y = states_maps_mean[next_states[i]]
xstr = str(6001+i) + "x"
ystr = str(6001+i) + "y"
row = [xstr,X-0.25]
writer.writerow(row)
row = [ystr,Y-0.25]
writer.writerow(row)