-
Notifications
You must be signed in to change notification settings - Fork 18
/
metrics.py
240 lines (213 loc) · 5.65 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
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
# -*-Encoding: utf-8 -*-
################################################################################
#
# Copyright (c) 2022 Baidu.com, Inc. All Rights Reserved
#
################################################################################
"""
Description: Some useful metrics
Authors: Lu,Xinjiang ([email protected])
Date: 2022/03/10
"""
import pandas as pd
import numpy as np
def ignore_zeros(predictions, grounds):
"""
Desc:
Ignore the zero values for evaluation
Args:
predictions:
grounds:
Returns:
Predictions and ground truths
"""
preds = predictions[np.where(grounds != 0)]
gts = grounds[np.where(grounds != 0)]
return preds, gts
def rse(pred, ground_truth):
"""
Desc:
Root square error
Args:
pred:
ground_truth: ground truth vector
Returns:
RSE value
"""
_rse = 0.
if len(pred) > 0 and len(ground_truth) > 0:
_rse = np.sqrt(np.sum((ground_truth - pred)**2)) / np.sqrt(
np.sum((ground_truth - ground_truth.mean())**2))
return _rse
def corr(pred, gt):
"""
Desc:
Correlation between the prediction and ground truth
Args:
pred:
gt: ground truth vector
Returns:
Correlation
"""
_corr = 0.
if len(pred) > 0 and len(gt) > 0:
u = ((gt - gt.mean(0)) * (pred - pred.mean(0))).sum(0)
d = np.sqrt(((gt - gt.mean(0))**2 * (pred - pred.mean(0))**2).sum(0))
_corr = (u / d).mean(-1)
return _corr
def mae(pred, gt):
"""
Desc:
Mean Absolute Error
Args:
pred:
gt: ground truth vector
Returns:
MAE value
"""
_mae = 0.
if len(pred) > 0 and len(gt) > 0:
_mae = np.mean(np.abs(pred - gt))
return _mae
def mse(pred, gt):
"""
Desc:
Mean Square Error
Args:
pred:
gt: ground truth vector
Returns:
MSE value
"""
_mse = 0.
if len(pred) > 0 and len(gt) > 0:
_mse = np.mean((pred - gt)**2)
return _mse
def rmse(pred, gt):
"""
Desc:
Root Mean Square Error
Args:
pred:
gt: ground truth vector
Returns:
RMSE value
"""
return np.sqrt(mse(pred, gt))
def mape(pred, gt):
"""
Desc:
Mean Absolute Percentage Error
Args:
pred:
gt: ground truth vector
Returns:
MAPE value
"""
_mape = 0.
if len(pred) > 0 and len(gt) > 0:
_mape = np.mean(np.abs((pred - gt) / gt))
return _mape
def mspe(pred, gt):
"""
Desc:
Mean Square Percentage Error
Args:
pred:
gt: ground truth vector
Returns:
MSPE value
"""
return np.mean(np.square((pred - gt) / gt)) if len(pred) > 0 and len(
gt) > 0 else 0
def regressor_scores(prediction, gt):
"""
Desc:
Some common metrics for regression problems
Args:
prediction:
gt: ground truth vector
Returns:
A tuple of metrics
"""
_mae = mae(prediction, gt)
_rmse = rmse(prediction, gt)
return _mae, _rmse
def turbine_scores(pred, gt, raw_data, examine_len, stride=1):
"""
Desc:
Calculate the MAE and RMSE of one turbine
Args:
pred: prediction for one turbine
gt: ground truth
raw_data: the DataFrame of one wind turbine
examine_len:
stride:
Returns:
The averaged MAE and RMSE
"""
nan_cond = pd.isna(raw_data).any(axis=1)
invalid_cond = (raw_data['Patv'] < 0) | \
((raw_data['Patv'] == 0) & (raw_data['Wspd'] > 2.5)) | \
((raw_data['Pab1'] > 89) | (raw_data['Pab2'] > 89) | (raw_data['Pab3'] > 89)) | \
((raw_data['Wdir'] < -180) | (raw_data['Wdir'] > 180) | (raw_data['Ndir'] < -720) |
(raw_data['Ndir'] > 720))
cond = (~invalid_cond) & (~nan_cond)
maes, rmses = [], []
cnt_sample, out_seq_len, _ = pred.shape
for i in range(0, cnt_sample, stride):
indices = np.where(cond[i:out_seq_len + i])
prediction = pred[i]
prediction = prediction[indices]
targets = gt[i]
targets = targets[indices]
_mae, _rmse = regressor_scores(prediction[-examine_len:] / 1000,
targets[-examine_len:] / 1000)
if _mae != _mae or _rmse != _rmse:
continue
maes.append(_mae)
rmses.append(_rmse)
avg_mae = np.array(maes).mean()
avg_rmse = np.array(rmses).mean()
return avg_mae, avg_rmse
def regressor_detailed_scores(predictions, gts, raw_df_lst, capacity,
output_len):
"""
Desc:
Some common metrics for regression problems
Args:
predictions:
gts: ground truth vector
raw_df_lst:
settings:
Returns:
A tuple of metrics
"""
all_mae, all_rmse = [], []
for i in range(capacity):
prediction = predictions[i]
gt = gts[i]
raw_df = raw_df_lst[i]
_mae, _rmse = turbine_scores(prediction, gt, raw_df, output_len, 1)
all_mae.append(_mae)
all_rmse.append(_rmse)
total_mae = np.array(all_mae).sum()
total_rmse = np.array(all_rmse).sum()
return total_mae, total_rmse
def regressor_metrics(pred, gt):
"""
Desc:
Some common metrics for regression problems
Args:
pred:
gt: ground truth vector
Returns:
A tuple of metrics
"""
_mae = mae(pred, gt)
_mse = mse(pred, gt)
_rmse = rmse(pred, gt)
# pred, gt = ignore_zeros(pred, gt)
_mape = mape(pred, gt)
_mspe = mspe(pred, gt)
return _mae, _mse, _rmse, _mape, _mspe