-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnilm_metric.py
231 lines (167 loc) · 5.25 KB
/
nilm_metric.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
import numpy as np
# from sklearn.metrics import confusion_matrix
def get_TP(target, prediction, threshold):
'''
compute the number of true positive
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
threshold: float
'''
assert (target.shape == prediction.shape)
target = 1 - np.clip(target, threshold, 0) / threshold
prediction = 1 - np.clip(prediction, threshold, 0) / threshold
tp_array = np.logical_and(target, prediction) * 1.0
tp = np.sum(tp_array)
return tp
def get_FP(target, prediction, threshold):
'''
compute the number of false positive
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
threshold: float
'''
assert (target.shape == prediction.shape)
target = np.clip(target, threshold, 0) / threshold
prediction = 1 - np.clip(prediction, threshold, 0) / threshold
fp_array = np.logical_and(target, prediction) * 1.0
fp = np.sum(fp_array)
return fp
def get_FN(target, prediction, threshold):
'''
compute the number of false negtive
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
threshold: float
'''
assert (target.shape == prediction.shape)
target = 1 - np.clip(target, threshold, 0) / threshold
prediction = np.clip(prediction, threshold, 0) / threshold
fn_array = np.logical_and(target, prediction) * 1.0
fn = np.sum(fn_array)
return fn
def get_TN(target, prediction, threshold):
'''
compute the number of true negative
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
threshold: float
'''
assert (target.shape == prediction.shape)
target = np.clip(target, threshold, 0) / threshold
prediction = np.clip(prediction, threshold, 0) / threshold
tn_array = np.logical_and(target, prediction) * 1.0
tn = np.sum(tn_array)
return tn
def get_recall(target, prediction, threshold):
'''
compute the recall rate
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
threshold: float
'''
tp = get_TP(target, prediction, threshold)
fn = get_FN(target, prediction, threshold)
print('tp={0}'.format(tp))
print('fn={0}'.format(fn))
if tp + fn <= 0.0:
recall = tp / (tp + fn + 1e-9)
else:
recall = tp / (tp + fn)
return recall
def get_precision(target, prediction, threshold):
'''
compute the precision rate
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
threshold: float
'''
tp = get_TP(target, prediction, threshold)
fp = get_FP(target, prediction, threshold)
print('tp={0}'.format(tp))
print('fp={0}'.format(fp))
if tp + fp <= 0.0:
precision = tp / (tp + fp + 1e-9)
else:
precision = tp / (tp + fp)
return precision
def get_F1(target, prediction, threshold):
'''
compute the F1 score
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
threshold: float
'''
recall = get_recall(target, prediction, threshold)
print(recall)
precision = get_precision(target, prediction, threshold)
print(precision)
if precision == 0.0 or recall == 0.0:
f1 = 0.0
else:
f1 = 2 * precision * recall / (precision + recall)
return f1
def get_accuracy(target, prediction, threshold):
'''
compute the accuracy rate
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
threshold: float
'''
tp = get_TP(target, prediction, threshold)
tn = get_TN(target, prediction, threshold)
accuracy = (tp + tn) / target.size
return accuracy
def get_relative_error(target, prediction):
'''
compute the relative_error
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
'''
assert (target.shape == prediction.shape)
return np.mean(np.nan_to_num(np.abs(target - prediction) / np.maximum(target, prediction)))
def get_abs_error(target, prediction):
'''
compute the absolute_error
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
'''
assert (target.shape == prediction.shape)
return np.mean(np.abs(target - prediction))
def get_nde(target, prediction):
'''
compute the normalized disaggregation error
Parameters:
----------------
target: the groud truth , np.array
prediction: the prediction, np.array
'''
return np.sum((target - prediction) ** 2) / np.sum((target ** 2))
def get_sae(target, prediction, sample_second):
'''
compute the signal aggregate error
sae = |\hat(r)-r|/r where r is the ground truth total energy;
\hat(r) is the predicted total energy.
'''
r = np.sum(target * sample_second * 1.0 / 3600.0)
rhat = np.sum(prediction * sample_second * 1.0 / 3600.0)
return np.abs(r - rhat) / np.abs(r)