Skip to content

Commit 4b132f5

Browse files
committed
Code for calculating CVRMSE of each case
1 parent f46498e commit 4b132f5

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

models/cal_cvrmse.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
5+
# Function for calculating CVRMSE
6+
def cal_cvrmse(y_pred, y_true):
7+
n_pred = np.shape(y_pred)[0]
8+
cvrmse = 100*np.sqrt(np.sum(np.square(y_pred-y_true))/(n_pred-1))/np.mean(y_true)
9+
10+
return cvrmse
11+
12+
# Function for computing the mean of each column
13+
def col_means(y_pred_raw):
14+
df = y_pred_raw
15+
col_mean_df = df.mean(axis=0)
16+
17+
return col_mean_df
18+
19+
20+
name_lst = [case_name list] # Put case names here
21+
case_num = len(name_lst)
22+
cvrmse_df = pd.DataFrame(columns=['CVRMSE']) # Create a dataframe for storing CVRMSE results
23+
parent_fd = "Pata of results" # Put the path of results here
24+
25+
# Loop through each output of each case
26+
for i, name in enumerate(name_lst):
27+
for j in range(case_num):
28+
conf = yaml.load(open(os.path.join(parent_fd, "conf_{}_{}.yaml".format(name_lst[i], j+1))), Loader=yaml.FullLoader)
29+
n_y = len(conf['vc_keys'])+len(conf['yc_keys'])
30+
31+
for k in range(n_y):
32+
# Read the pred data of each output of each case
33+
y_pred_raw = pd.read_csv(os.path.join(os.getcwd(), '_case_{}_{}_y_pred{}.csv'.format(name_lst[i], j+1, k+1)), index_col=0)
34+
y_pred_raw = y_pred_raw.T.reset_index(drop=True)
35+
36+
# Read the DataField of each output of each case
37+
y_true = pd.read_csv(os.path.join(os.getcwd(), 'Path_of_Datafield + {}_{}'.format(name_lst[i], j+1)))
38+
39+
# Calculate CVRMSE of each output of each case
40+
cvrmse = cal_cvrmse(col_means(y_pred_raw.T), y_true.iloc[:, k])
41+
cvrmse_df.loc['{}_{}_y_pred{}'.format(name_lst[i], j+1, k+1)] = cvrmse
42+
43+
# Create folder for storing the results
44+
if not os.path.exists('./data'):
45+
os.makedirs('./data')
46+
cvrmse_df.to_csv('./data/cvrmse_res.csv')

0 commit comments

Comments
 (0)