@programmer12,
To calculate the mase
metric correctly, you should use the formula
mase = mae / tf.reduce_mean(tf.abs(ytrue[1:] - ytrue[:-1]))
Please refer to the working code below
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.metrics import mean_absolute_error, mean_absolute_percentage_error, mean_squared_error
def evaluate_metrics(ytrue, ypred):
ytrue = np.float32(ytrue)
ypred = np.float32(ypred)
mae = mean_absolute_error(ytrue, ypred)
mape = mean_absolute_percentage_error(ytrue, ypred)
mse = mean_squared_error(ytrue, ypred)
rmse = tf.sqrt(mse)
mase = mae / tf.reduce_mean(tf.abs(ytrue[1:] - ytrue[:-1]))
return {
"mae": mae,
"mape": mape,
"mse": mse,
"rmse": rmse,
"mase": mase
}
#creating python variables to testing the function
ytrue = np.array([9226.48582088, 8794.35864452, 8798.04205463, 9081.18687849,
8711.53433917, 8760.89271814, 8749.52059102, 8656.97092235,
8500.64355816, 8469.2608989])
ypred = np.array([57107.12067189, 58788.20967893, 58102.19142623, 55715.54665129,
56573.5554719, 52147.82118698, 49764.1320816, 50032.69313676,
47885.62525472, 45604.61575361])
#calling the function using the above variables
evaluate_metrics(ytrue, ypred)
Output:
{'mae': <tf.Tensor: shape=(), dtype=float32, numpy=44397.26>,
'mape': <tf.Tensor: shape=(), dtype=float32, numpy=505.44952>,
'mse': <tf.Tensor: shape=(), dtype=float32, numpy=1989516000.0>,
'rmse': <tf.Tensor: shape=(), dtype=float32, numpy=44603.992>,
'mase': <tf.Tensor: shape=(), dtype=float32, numpy=279.50198>}
Thank you!