-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAPE_CNR_metric.py
42 lines (31 loc) · 1.44 KB
/
CAPE_CNR_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
"""
Example of custom metric script.
The custom metric script must contain the definition of custom_metric_function and a main function
that reads the two csv files with pandas and evaluate the custom metric.
"""
import numpy as np
def CAPE_CNR_function(dataframe_y_true, dataframe_y_pred):
"""
CAPE (Cumulated Absolute Percentage Error) function used by CNR for the evaluation of predictions
Args
dataframe_y_true: Pandas Dataframe
Dataframe containing the true values of y.
This dataframe was obtained by reading a csv file with following instruction:
dataframe_y_true = pd.read_csv(CSV_1_FILE_PATH, index_col=0, sep=',')
dataframe_y_pred: Pandas Dataframe
This dataframe was obtained by reading a csv file with following instruction:
dataframe_y_pred = pd.read_csv(CSV_2_FILE_PATH, index_col=0, sep=',')
Returns
score: Float
The metric evaluated with the two dataframes. This must not be NaN.
"""
# CAPE function
cape_cnr = 100 * np.sum(np.abs(dataframe_y_pred - dataframe_y_true)) / np.sum(dataframe_y_true)
return cape_cnr
if __name__ == '__main__':
import pandas as pd
CSV_FILE_Y_TRUE = 'Y_test.csv'
CSV_FILE_Y_PRED = 'Y_test_benchmark.csv'
df_y_true = pd.read_csv(CSV_FILE_Y_TRUE, index_col=0, sep=',')
df_y_pred = pd.read_csv(CSV_FILE_Y_PRED, index_col=0, sep=',')
print(CAPE_CNR_function(df_y_true, df_y_pred))