-
Notifications
You must be signed in to change notification settings - Fork 2
/
Metrics.py
28 lines (18 loc) · 1.02 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
import numpy as np
def evaluate(y_pred: np.array, y_true: np.array, precision=4):
print('MSE:', round(MSE(y_pred, y_true), precision))
print('RMSE:', round(RMSE(y_pred, y_true), precision))
print('MAE:', round(MAE(y_pred, y_true), precision))
print('MAPE:', round(MAPE(y_pred, y_true)*100, precision), '%')
print('PCC:', round(PCC(y_pred, y_true), precision))
return MSE(y_pred, y_true), RMSE(y_pred, y_true), MAE(y_pred, y_true), MAPE(y_pred, y_true)
def MSE(y_pred: np.array, y_true: np.array):
return np.mean(np.square(y_pred - y_true))
def RMSE(y_pred:np.array, y_true:np.array):
return np.sqrt(MSE(y_pred, y_true))
def MAE(y_pred:np.array, y_true:np.array):
return np.mean(np.abs(y_pred - y_true))
def MAPE(y_pred:np.array, y_true:np.array, epsilon=1e-0): # avoid zero division
return np.mean(np.abs(y_pred - y_true) / (y_true + epsilon))
def PCC(y_pred:np.array, y_true:np.array): # Pearson Correlation Coefficient
return np.corrcoef(y_pred.flatten(), y_true.flatten())[0,1]