-
Notifications
You must be signed in to change notification settings - Fork 2
/
time_slice.py
270 lines (204 loc) · 8.23 KB
/
time_slice.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from preprocessing import *
from hmm import *
import pytz # allows accurate and cross platform timezone calculations
def probability_distribution(seq1, seq2):
n = 1 + max(seq1);
m = 1 + max(seq2)
M = np.zeros((n, m))
# Conta delle occorrenze
for s1, s2 in zip(seq1, seq2):
M[s1][s2] += 1
# Pone a 'epsilon' le probabilità che sono zero
M[M == 0] = 10e-2
# Calcolo delle distribuzioni di probabilità
row_sums = M.sum(axis=1)
M = M / row_sums[:, np.newaxis]
return M
# Calcola la distribuzione di probabilità degli stati
def prior(transitions):
g = transitions.groupby(transitions)
result = g.count() / g.count().sum()
return result.values
# Calcola la matrice di transizione data la sequenza di stati ad ogni tempo t
def transition_matrix(sequence):
return probability_distribution(sequence, sequence[1:])
# Calcola la distribuzione di probabilità delle osservazioni per ogni stato
def obs_matrix(seq, obs):
return probability_distribution(seq, obs)
def date_to_timestamp(m):
return int(datetime.strptime(m.strip(), "%Y-%m-%d %H:%M:%S").timestamp())
def print_numpy_matrix(m):
import sys
np.savetxt(sys.stdout, m, '%6.4f')
# CALCOLA LE MATRICI DI PROBABILITA' PER I TIME SLICE
def slice_prob(dt, days):
# controllo se DATASET A oppure B
if dt == 1:
try:
# prende il dataset mergiato dal csv
df = pd.read_csv('dataset_csv/OrdonezA.csv')
except:
# se non trova il csv esegue il preprocessing
df = generate_dataset()[0]
else:
try:
# prende il dataset mergiato dal csv
df = pd.read_csv('dataset_csv/OrdonezB.csv')
except:
# se non trova il csv esegue il preprocessing
df = generate_dataset()[1]
# Discretizza le osservazioni dei sensori
df[['sensors']] = df[['sensors']].apply(lambda x: x.astype('category'))
mapping = dict(enumerate(df['sensors'].cat.categories))
df[['sensors']] = df[['sensors']].apply(lambda x: x.cat.codes)
df['date'] = df['timestamp'].apply(lambda x: datetime.fromtimestamp(x, tz=pytz.UTC))
if dt == 1:
# DATASET A: TRAIN E TEST SET
if days == 1:
trainIndex = range(0, 16401)
testIndex = range(16402, len(df.index))
train = df.loc[trainIndex, :]
test = df.loc[testIndex, :]
elif days == 2:
trainIndex = range(2371, len(df.index))
testIndex = range(0, 2370)
train = df.loc[trainIndex, :]
test = df.loc[testIndex, :]
elif days == 3:
trainIndex = range(0, 10930)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(13544, len(df.index)), :])
testIndex = range(10931, 13543)
test = df.loc[testIndex, :]
elif days == 4:
trainIndex = range(0, 13676)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(16402, len(df.index)), :])
testIndex = range(13677, 16401)
test = df.loc[testIndex, :]
elif days == 5:
trainIndex = range(0, 4063)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(6619, len(df.index)), :])
testIndex = range(4064, 6618)
test = df.loc[testIndex, :]
# test da 3 giorni
if days == 6:
trainIndex = range(0, 15051)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(17899, len(df.index)), :])
testIndex = range(15052, 17898)
test = df.loc[testIndex, :]
elif days == 7:
trainIndex = range(4065, len(df.index))
testIndex = range(0, 4064)
train = df.loc[trainIndex, :]
test = df.loc[testIndex, :]
elif days == 8:
trainIndex = range(0, 9660)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(13678, len(df.index)), :])
testIndex = range(9661, 13677)
test = df.loc[testIndex, :]
elif days == 9:
trainIndex = range(0, 12297)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(16403, len(df.index)), :])
testIndex = range(12298, 16402)
test = df.loc[testIndex, :]
elif days == 10:
trainIndex = range(0, 2662)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(6856, len(df.index)), :])
testIndex = range(2663, 6855)
test = df.loc[testIndex, :]
else :
# DATASET B: TRAIN E TEST SET
# test da 3 giorni
if days == 1:
trainIndex = range(0, 6673)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(9036, len(df.index)), :])
testIndex = range(6674, 9035)
test = df.loc[testIndex, :]
elif days == 2:
trainIndex = range(0, 22329)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(24809, len(df.index)), :])
testIndex = range(22330, 24808)
test = df.loc[testIndex, :]
elif days == 3:
trainIndex = range(0, 275)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(2672, len(df.index)), :])
testIndex = range(276, 2671)
test = df.loc[testIndex, :]
elif days == 4:
trainIndex = range(0, 21092)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(23564, len(df.index)), :])
testIndex = range(21091, 23563)
test = df.loc[testIndex, :]
elif days == 5:
trainIndex = range(0, 13164)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(15765, len(df.index)), :])
testIndex = range(13165, 15764)
test = df.loc[testIndex, :]
# test da 7 giorni
if days == 6:
trainIndex = range(0, 6671)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(15966, len(df.index)), :])
testIndex = range(6672, 15965)
test = df.loc[testIndex, :]
elif days == 7:
trainIndex = range(0, 15764)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(23565, len(df.index)), :])
testIndex = range(15765, 23564)
test = df.loc[testIndex, :]
elif days == 8:
# trainIndex = range(0, 274)
# train = df.loc[trainIndex, :]
# train = train.append(df.loc[range(9037, len(df.index)), :])
# testIndex = range(275, 9036)
# test = df.loc[testIndex, :]
trainIndex = range(0, 4108)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(16165, len(df.index)), :])
testIndex = range(4109, 13164)
test = df.loc[testIndex, :]
elif days == 9:
trainIndex = range(0, 11728)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(21092, len(df.index)), :])
testIndex = range(11729, 21091)
test = df.loc[testIndex, :]
elif days == 10:
trainIndex = range(0, 13164)
train = df.loc[trainIndex, :]
train = train.append(df.loc[range(22273, len(df.index)), :])
testIndex = range(13165, 22272)
test = df.loc[testIndex, :]
trainset_s = train['activity']
trainset_o = train['sensors']
testset_s = test['activity'].tolist()
testset_o = test['sensors'].tolist()
# Calcolo delle distribuzioni della HMM
P = prior(trainset_s)
T = transition_matrix(trainset_s)
O = obs_matrix(trainset_s, trainset_o)
# VITERBI
viterbi_result, p, x = hmm.viterbi(testset_o, T, O, P)
# CONTO QUANTI STATI HO INDOVINATO
c = 0
for i, j in zip(viterbi_result, testset_s):
if i == j:
c += 1
accuracy = c/len(viterbi_result) * 100
# converte la lista in ndarray
testset_s = np.asarray(testset_s)
return testset_s, viterbi_result, accuracy
# if __name__ == '__main__':
# slice_prob(1,2)