-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.py
95 lines (80 loc) · 2.88 KB
/
metrics.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
from nilmtk.electric import align_two_meters
import numpy as np
def tp_tn_fp_fn(states_pred, states_ground):
tp = np.sum(np.logical_and(states_pred == 1, states_ground == 1))
fp = np.sum(np.logical_and(states_pred == 1, states_ground == 0))
fn = np.sum(np.logical_and(states_pred == 0, states_ground == 1))
tn = np.sum(np.logical_and(states_pred == 0, states_ground == 0))
return tp, tn, fp, fn
def recall_precision_accuracy_f1(pred, ground):
aligned_meters = align_two_meters(pred, ground)
threshold = ground.on_power_threshold()
chunk_results = []
sum_samples = 0.0
for chunk in aligned_meters:
sum_samples += len(chunk)
pr = np.array([0 if (p)<threshold else 1 for p in chunk.iloc[:,0]])
gr = np.array([0 if p<threshold else 1 for p in chunk.iloc[:,1]])
tp, tn, fp, fn = tp_tn_fp_fn(pr,gr)
p = sum(pr)
n = len(pr) - p
chunk_results.append([tp,tn,fp,fn,p,n])
if sum_samples == 0:
return None
else:
[tp,tn,fp,fn,p,n] = np.sum(chunk_results, axis=0)
res_recall = recall(tp,fn)
res_precision = precision(tp,fp)
res_f1 = f1(res_precision,res_recall)
res_accuracy = accuracy(tp,tn,p,n)
return (res_recall,res_precision,res_accuracy,res_f1)
def relative_error_total_energy(pred, ground):
aligned_meters = align_two_meters(pred, ground)
chunk_results = []
sum_samples = 0.0
for chunk in aligned_meters:
chunk.fillna(0, inplace=True)
sum_samples += len(chunk)
E_pred = sum(chunk.iloc[:,0])
E_ground = sum(chunk.iloc[:,1])
chunk_results.append([
E_pred,
E_ground
])
if sum_samples == 0:
return None
else:
[E_pred, E_ground] = np.sum(chunk_results,axis=0)
return abs(E_pred - E_ground) / float(max(E_pred,E_ground))
def mean_absolute_error(pred, ground):
aligned_meters = align_two_meters(pred, ground)
total_sum = 0.0
sum_samples = 0.0
for chunk in aligned_meters:
chunk.fillna(0, inplace=True)
sum_samples += len(chunk)
total_sum += sum(abs((chunk.iloc[:,0]) - chunk.iloc[:,1]))
if sum_samples == 0:
return None
else:
return total_sum / sum_samples
def root_mean_squared_error(pred, ground):
aligned_meters = align_two_meters(pred, ground)
total_sum = 0.0
sum_samples = 0.0
for chunk in aligned_meters:
chunk.fillna(0, inplace=True)
sum_samples += len(chunk)
total_sum += sum(pow((chunk.iloc[:,0] - chunk.iloc[:,1]),2))
if sum_samples == 0:
return None
else:
return pow(total_sum / sum_samples, 0.5)
def recall(tp,fn):
return tp/float(tp+fn)
def precision(tp,fp):
return tp/float(tp+fp)
def f1(prec,rec):
return 2 * (prec*rec) / float(prec+rec)
def accuracy(tp, tn, p, n):
return (tp + tn) / float(p + n)