-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
32 lines (24 loc) · 958 Bytes
/
eval.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
import numpy as np
import torch
def mask_np(array, null_val):
if np.isnan(null_val):
return (~np.isnan(null_val)).astype('float32')
else:
return np.not_equal(array, null_val).astype('float32')
def masked_mape_np(y_true, y_pred, null_val=np.nan):
with np.errstate(divide='ignore', invalid='ignore'):
mask = mask_np(y_true, null_val)
mask /= mask.mean()
mape = np.abs((y_pred - y_true) / y_true)
mape = np.nan_to_num(mask * mape)
return np.mean(mape) * 100
def masked_mae_np(y_true, y_pred, null_val=np.nan):
mask = mask_np(y_true, null_val)
mask /= mask.mean()
mae = np.abs(y_true - y_pred)
return np.mean(np.nan_to_num(mask * mae))
def masked_rmse_np(y_true, y_pred, null_val=np.nan):
mask = mask_np(y_true, null_val)
mask /= mask.mean()
mse = (y_true - y_pred) ** 2
return np.sqrt(np.mean(np.nan_to_num(mask * mse)))