-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSegNet_B_seen_diff.py
More file actions
624 lines (519 loc) · 23.4 KB
/
SegNet_B_seen_diff.py
File metadata and controls
624 lines (519 loc) · 23.4 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data as data
from torch.utils.data import Dataset, DataLoader
import math
from collections import defaultdict, OrderedDict
import copy
import numpy as np
import pandas as pd
from sklearn.metrics import f1_score, precision_score, recall_score
from sklearn.metrics import accuracy_score, mean_absolute_error, matthews_corrcoef
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
#%matplotlib inline
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
store = pd.HDFStore('./ukdale5.h5')
def resample_meter(store=None, building=1, meter=1, period='1min', cutoff=1000.):
key = '/building{}/elec/meter{}'.format(building,meter)
m = store[key]
v = m.values.flatten()
t = m.index
s = pd.Series(v, index=t).clip(0.,cutoff)
s[s<10.] = 0.
return s.resample('1s').ffill(limit=300).fillna(0.).resample(period).mean().tz_convert('UTC')
def get_series(datastore, house, label, cutoff):
filename = r'/public/home/jd_ylf/anaconda3_new/envs/nilmtk-env/lib/python3.6/site-packages/nilmtk/disaggregate/transferNILM-master/dataset_management/ukdale/uk2015/house_%1d/labels.dat' %house
print(filename)
labels = pd.read_csv(filename, delimiter=' ', header=None, index_col=0).to_dict()[1]
for i in labels:
if labels[i] == label:
print(i, labels[i])
s = resample_meter(store, house, i, '1min', cutoff)
#s = resample_meter(store, house, i, '6s', cutoff)
s.index.name = 'datetime'
return s
def get_status(app, threshold, min_off, min_on):
condition = app > threshold
# Find the indicies of changes in "condition"
d = np.diff(condition)
idx, = d.nonzero()
# We need to start things after the change in "condition". Therefore,
# we'll shift the index by 1 to the right.
idx += 1
if condition[0]:
# If the start of condition is True prepend a 0
idx = np.r_[0, idx]
if condition[-1]:
# If the end of condition is True, append the length of the array
idx = np.r_[idx, condition.size] # Edit
# Reshape the result into two columns
idx.shape = (-1,2)
on_events = idx[:,0].copy()
off_events = idx[:,1].copy()
assert len(on_events) == len(off_events)
if len(on_events) > 0:
off_duration = on_events[1:] - off_events[:-1]
off_duration = np.insert(off_duration, 0, 1000.)
on_events = on_events[off_duration > min_off]
off_events = off_events[np.roll(off_duration, -1) > min_off]
assert len(on_events) == len(off_events)
on_duration = off_events - on_events
on_events = on_events[on_duration > min_on]
off_events = off_events[on_duration > min_on]
s = app.copy()
#s.iloc[:] = 0.
s[:] = 0.
for on, off in zip(on_events, off_events):
#s.iloc[on:off] = 1.
s[on:off] = 1.
return s
class Power(data.Dataset):
def __init__(self, meter=None, appliance=None, status=None,
length=256, border=680, max_power=1., train=False):
self.length = length
self.border = border
self.max_power = max_power
self.train = train
self.meter = meter.copy()/self.max_power
self.appliance = appliance.copy()/self.max_power
self.status = status.copy()
self.epochs = (len(self.meter) - 2*self.border) // self.length
def __getitem__(self, index):
i = index * self.length + self.border
if self.train:
i = np.random.randint(self.border, len(self.meter) - self.length - self.border)
x = self.meter.iloc[i-self.border:i+self.length+self.border].values.astype('float32')
y = self.appliance.iloc[i:i+self.length].values.astype('float32')
s = self.status.iloc[i:i+self.length].values.astype('float32')
x -= x.mean()
return x, y, s
def __len__(self):
return self.epochs
def train_model(model, batch_size, n_epochs, filename):
# to track the training loss as the model trains
train_losses = []
# to track the validation loss as the model trains
valid_losses = []
# to track the test loss as the model trains
test_losses = []
# to track the average training loss per epoch as the model trains
avg_train_losses = []
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []
# to track the average test loss per epoch as the model trains
avg_test_losses = []
min_loss = np.inf
# initialize the early_stopping object
#patience = 10
#early_stopping = EarlyStopping(patience=patience, verbose=True)
for epoch in range(1, n_epochs + 1):
###################
# train the model #
###################
model.train() # prep model for training
for batch, (data, target_power, target_status) in enumerate(train_loader, 1):
data = data.unsqueeze(1).cuda()
target_power = target_power.cuda()
target_status = target_status.cuda()
# clear the gradients of all optimized variables
optimizer.zero_grad()
# forward pass: compute predicted outputs by passing inputs to the model
output_status = model(data).permute(0,2,1)
# calculate the loss
loss = criterion(output_status, target_status)
# backward pass: compute gradient of the loss with respect to model parameters
loss.backward()
# perform a single optimization step (parameter update)
optimizer.step()
# record training loss
train_losses.append(loss.item())
######################
# validate the model #
######################
model.eval() # prep model for evaluation
for data, target_power, target_status in valid_loader:
data = data.unsqueeze(1).cuda()
target_power = target_power.cuda()
target_status = target_status.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output_status = model(data).permute(0,2,1)
# calculate the loss
loss = criterion(output_status, target_status)
# record validation loss
valid_losses.append(loss.item())
##################
# test the model #
##################
model.eval() # prep model for evaluation
for data, target_power, target_status in test_loader:
data = data.unsqueeze(1).cuda()
target_power = target_power.cuda()
target_status = target_status.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output_status = model(data).permute(0,2,1)
# calculate the loss
loss = criterion(output_status, target_status)
# record validation loss
test_losses.append(loss.item())
# print training/validation statistics
# calculate average loss over an epoch
train_loss = np.average(train_losses)
valid_loss = np.average(valid_losses)
test_loss = np.average(test_losses)
avg_train_losses.append(train_loss)
avg_valid_losses.append(valid_loss)
avg_test_losses.append(test_loss)
epoch_len = len(str(n_epochs))
print_msg = (f'[{epoch:>{epoch_len}}/{n_epochs:>{epoch_len}}] ' +
f'train_loss: {train_loss:.5f} ' +
f'valid_loss: {valid_loss:.5f} ' +
f'test_loss: {test_loss:.5f} ')
print(print_msg)
# clear lists to track next epoch
train_losses = []
valid_losses = []
test_losses = []
# early_stopping needs the validation loss to check if it has decresed,
# and if it has, it will make a checkpoint of the current model
#early_stopping(valid_loss, model)
#if (early_stopping.early_stop and (epoch > 80)):
# break
if valid_loss < min_loss:
print(f'Validation loss decreased ({min_loss:.6f} --> {valid_loss:.6f}). Saving model ...')
torch.save(model.state_dict(), filename)
min_loss = valid_loss
# load the last checkpoint with the best model
model.load_state_dict(torch.load(filename))
return model, avg_train_losses, avg_valid_losses, avg_test_losses
def evaluate_activation(model, loader, a):
x_true = []
s_true = []
p_true = []
s_hat = []
model.eval()
with torch.no_grad():
for x, p, s in loader:
x = x.unsqueeze(1).cuda()
p = p.permute(0,2,1)[:,a,:]
s = s.permute(0,2,1)[:,a,:]
sh = model(x)
sh = torch.sigmoid(sh[:,a,:])
s_hat.append(sh.contiguous().view(-1).detach().cpu().numpy())
x_true.append(x[:,:,BORDER:-BORDER].contiguous().view(-1).detach().cpu().numpy())
s_true.append(s.contiguous().view(-1).detach().cpu().numpy())
p_true.append(p.contiguous().view(-1).detach().cpu().numpy())
x_true = np.hstack(x_true)
s_true = np.hstack(s_true)
p_true = np.hstack(p_true)
s_hat = np.hstack(s_hat)
return x_true, p_true, s_true, s_hat
APPLIANCE = ['fridge', 'dish_washer', 'washing_machine']
THRESHOLD = [50., 10., 20.]
MIN_ON = [1., 30., 30.]
MIN_OFF = [1., 30., 3.]
METER = 'aggregate'
SEQ_LEN = 60*8
#SEQ_LEN = 512
# BORDER = 16
BORDER = 0
BATCH_SIZE = 32
MAX_POWER = 2000.
ds_meter = []
ds_appliance = []
ds_status = []
for i in range(5):
ds = pd.read_feather('./UKDALE_%d_train.feather' %(i+1))
ds.set_index('datetime', inplace=True)
meter = ds[METER]
appliances = ds[APPLIANCE]
status = pd.DataFrame()
for a in range(len(APPLIANCE)):
status = pd.concat([status, get_status(ds[APPLIANCE[a]], THRESHOLD[a], MIN_OFF[a], MIN_ON[a])], axis=1)
ds_meter.append(meter)
ds_appliance.append(appliances)
ds_status.append(status)
ds_len = [len(ds_meter[i]) for i in range(5)]
(ds_status[1].diff()==1).sum()
ds_status[1].describe()
ds_house_train = [Power(ds_meter[i][:int(0.8*ds_len[i])],
ds_appliance[i][:int(0.8*ds_len[i])],
ds_status[i][:int(0.8*ds_len[i])],
SEQ_LEN, BORDER, MAX_POWER, True) for i in range(5+0)]
ds_house_valid = [Power(ds_meter[i][int(0.8*ds_len[i]):int(0.9*ds_len[i])],
ds_appliance[i][int(0.8*ds_len[i]):int(0.9*ds_len[i])],
ds_status[i][int(0.8*ds_len[i]):int(0.9*ds_len[i])],
SEQ_LEN, BORDER, MAX_POWER, False) for i in range(5+0)]
ds_house_test = [Power(ds_meter[i][int(0.9*ds_len[i]):],
ds_appliance[i][int(0.9*ds_len[i]):],
ds_status[i][int(0.9*ds_len[i]):],
SEQ_LEN, BORDER, MAX_POWER, False) for i in range(5+0)]
ds_house_total = [Power(ds_meter[i], ds_appliance[i], ds_status[i],
SEQ_LEN, BORDER, MAX_POWER, False) for i in range(5+0)]
ds_train_seen = torch.utils.data.ConcatDataset([ds_house_train[0],
ds_house_train[1],
#ds_house_train[2],
#ds_house_train[3],
ds_house_train[4]
])
ds_valid_seen = torch.utils.data.ConcatDataset([ds_house_valid[0],
#ds_house_valid[1],
#ds_house_valid[2],
#ds_house_valid[3],
#ds_house_valid[4]
])
dl_train_seen = DataLoader(dataset = ds_train_seen, batch_size = BATCH_SIZE, shuffle=True)
dl_valid_seen = DataLoader(dataset = ds_valid_seen, batch_size = BATCH_SIZE, shuffle=False)
dl_test_seen = DataLoader(dataset = ds_house_test[0], batch_size = BATCH_SIZE, shuffle=False)
ds_train_unseen = torch.utils.data.ConcatDataset([ds_house_train[0],
#ds_house_train[1],
#ds_house_train[2],
#ds_house_train[3],
ds_house_train[4]
])
ds_valid_unseen = torch.utils.data.ConcatDataset([ds_house_valid[0],
#ds_house_valid[1],
#ds_house_valid[2],
#ds_house_valid[3],
ds_house_valid[4]
])
dl_train_unseen = DataLoader(dataset = ds_train_unseen, batch_size = BATCH_SIZE, shuffle=True)
dl_valid_unseen = DataLoader(dataset = ds_valid_unseen, batch_size = BATCH_SIZE, shuffle=False)
dl_test_unseen = DataLoader(dataset = ds_house_total[1], batch_size = BATCH_SIZE, shuffle=False)
dl_house_test = [DataLoader(dataset = ds_house_test[i], batch_size = 1, shuffle=False) for i in range(5)]
dl_house_valid = [DataLoader(dataset = ds_house_valid[i], batch_size = 1, shuffle=False) for i in range(5)]
dl_house_total = [DataLoader(dataset = ds_house_total[i], batch_size = 1, shuffle=False) for i in range(5)]
dataiter = iter(dl_house_test[1])
plt.figure(figsize=(15,8))
#x, y, s = dataiter.next()
a = 1
for i in range(100):
x, y, s = dataiter.next()
if y[0,:,a].sum() > 0:
break
if s[0,:,a].sum() > 0:
break
plt.plot(np.arange(-BORDER, SEQ_LEN + BORDER), x[0,:].detach().numpy(), 'k-')
plt.plot(y[0,:,a].detach().numpy())
plt.plot(s[0,:,a].detach().numpy())
plt.ylim([-0.5,1.5])
##### SegNet Bayesian
bn_momentum = 0.1
class Encoder(nn.Module):
def __init__(self, input_channels):
super(Encoder, self).__init__()
self.enco1 = nn.Sequential(
nn.Conv1d(input_channels, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(64, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(64, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(64, momentum=bn_momentum),
nn.ReLU()
)
self.enco2 = nn.Sequential(
nn.Conv1d(64, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(128, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(128, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(128, momentum=bn_momentum),
nn.ReLU()
)
self.enco3 = nn.Sequential(
nn.Conv1d(128, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(256, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(256, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(256, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(256, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(256, momentum=bn_momentum),
nn.ReLU()
)
self.enco4 = nn.Sequential(
nn.Conv1d(256, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU()
)
self.enco5 = nn.Sequential(
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU()
)
self.drop = nn.Dropout(0.5)
def forward(self, x):
id = []
x = self.enco1(x)
x, id1 = F.max_pool1d(x, kernel_size=2, stride=2, return_indices=True) # 保留最大值的位置
id.append(id1)
x = self.enco2(x)
x, id2 = F.max_pool1d(x, kernel_size=2, stride=2, return_indices=True)
id.append(id2)
x = self.enco3(x)
x, id3 = F.max_pool1d(x, kernel_size=2, stride=2, return_indices=True)
id.append(id3)
#
x = self.drop(x)
x = self.enco4(x)
x, id4 = F.max_pool1d(x, kernel_size=2, stride=2, return_indices=True)
id.append(id4)
x = self.drop(x)
x = self.enco5(x)
x, id5 = F.max_pool1d(x, kernel_size=2, stride=2, return_indices=True)
id.append(id5)
x = self.drop(x)
return x, id
# 编码器+解码器
class SegNet_B(nn.Module):
def __init__(self, input_channels, output_channels):
super(SegNet_B, self).__init__()
self.weights_new = self.state_dict()
self.encoder = Encoder(input_channels)
self.deco1 = nn.Sequential(
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU()
)
self.deco2 = nn.Sequential(
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(512, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(512, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(256, momentum=bn_momentum),
nn.ReLU()
)
self.deco3 = nn.Sequential(
nn.Conv1d(256, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(256, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(256, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(256, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(256, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(128, momentum=bn_momentum),
nn.ReLU()
)
self.deco4 = nn.Sequential(
nn.Conv1d(128, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(128, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(128, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(64, momentum=bn_momentum),
nn.ReLU()
)
self.deco5 = nn.Sequential(
nn.Conv1d(64, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(64, momentum=bn_momentum),
nn.ReLU(),
nn.Conv1d(64, output_channels, kernel_size=3, stride=1, padding=1),
)
self.dropout = nn.Dropout(0.5)
def forward(self, x):
x, id = self.encoder(x)
x = F.max_unpool1d(x, id[4], kernel_size=2, stride=2)
x = self.deco1(x)
######
x = self.dropout(x)
x = F.max_unpool1d(x, id[3], kernel_size=2, stride=2)
x = self.deco2(x)
######
x = self.dropout(x)
x = F.max_unpool1d(x, id[2], kernel_size=2, stride=2)
x = self.deco3(x)
######
x = self.dropout(x)
x = F.max_unpool1d(x, id[1], kernel_size=2, stride=2)
x = self.deco4(x)
x = F.max_unpool1d(x, id[0], kernel_size=2, stride=2)
x = self.deco5(x)
return x
# input = torch.randn(32,1,480).cuda()
# model = SegNet_B(1,3)
# model.eval()
# print(model)
# output = model(input)
# print('SegNet_B', output.size())
# UNet(n_channels=1, n_classes=3, bilinear=False)
bn_momentum = 0.1
batch_size = BATCH_SIZE
n_epochs = 200
train_loader = dl_train_seen
valid_loader = dl_valid_seen
test_loader = dl_test_seen
#i = 0
for i in range(1):
print('TRAINING MODEL %d' %i)
# Instantiate the model
model = SegNet_B(1,3).cuda()
optimizer = optim.Adam(model.parameters(), lr=1.E-4)
#optimizer = torch.optim.SGD(SegNet.parameters(), lr=LR, momentum=MOMENTUM)
criterion = nn.BCEWithLogitsLoss()
fn = 'SegNet_B_model_UKDALE_seen_%d.pth' %i
model, train_loss, valid_loss, test_loss = train_model(model, batch_size, n_epochs, fn)
scores = {}
for a in range(3):
scores[a] = {}
scores[a]['F1'] = []
scores[a]['Precision'] = []
scores[a]['Recall'] = []
scores[a]['Accuracy'] = []
scores[a]['MCC'] = []
scores[a]['MAE'] = []
scores[a]['SAE'] = []
thr = 0.5
for i in range(1):
#filename = '/content/gdrive/My Drive/NILM/UKDALE_seen_%d.pth' %i
filename = 'SegNet_B_model_UKDALE_seen_%d.pth' %i
print(filename)
model.load_state_dict(torch.load(filename))
for a in range(3):
#x_true, p_true, s_true, s_hat = evaluate_activation(model, dl_house_total[0], a)
#pm = p_true.sum() / s_true.sum()
#pm = (ds_appliance[0][APPLIANCE[a]] *
# ds_status[0][APPLIANCE[a]]).sum() / ds_status[0][APPLIANCE[a]].sum() / MAX_POWER
pm = ds_appliance[0][APPLIANCE[a]].sum() / ds_status[0][APPLIANCE[a]].sum() / MAX_POWER
x_true, p_true, s_true, s_hat = evaluate_activation(model, dl_house_test[0], a)
s_hat = get_status(s_hat, thr, MIN_OFF[a], MIN_ON[a])
p_hat = pm * s_hat
scores[a]['F1'].append(f1_score(s_true, s_hat))
scores[a]['Precision'].append(precision_score(s_true, s_hat))
scores[a]['Recall'].append(recall_score(s_true, s_hat))
scores[a]['Accuracy'].append(accuracy_score(s_true, s_hat))
scores[a]['MCC'].append(matthews_corrcoef(s_true, s_hat))
scores[a]['MAE'].append(mean_absolute_error(p_true, p_hat)*MAX_POWER)
scores[a]['SAE'].append((p_hat.sum() - p_true.sum()) / p_true.sum())
for i,a in enumerate(APPLIANCE):
print()
print(a)
print('F1 score : %.3f ' %(np.mean(scores[i]['F1'])))
print('Precision : %.3f ' %(np.mean(scores[i]['Precision'])))
print('Recall : %.3f ' %(np.mean(scores[i]['Recall'])))
print('Accuracy : %.3f ' %(np.mean(scores[i]['Accuracy'])))
print('MCC : %.3f ' %(np.mean(scores[i]['MCC'])))
print('MAE : %.2f ' %(np.mean(scores[i]['MAE'])))
print('SAE : %.3f ' %(np.mean(scores[i]['SAE'])))