Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the Mean Average Percentage Error(mape) extremely high?

I have obtained code from machinelearningmastery

I modified the model.compile() function to add mape metrics to find out the Mean Absolute Percentage Error. After running the code, the mape at every epoch comes so huge, considering it as a percentage metric. Am I missing something obvious or is the output right? The output looks like:

Epoch 91/100
0s - loss: 0.0103 - mean_absolute_percentage_error: 1764997.4502
Epoch 92/100
0s - loss: 0.0103 - mean_absolute_percentage_error: 1765653.4924
Epoch 93/100
0s - loss: 0.0102 - mean_absolute_percentage_error: 1766505.5107
Epoch 94/100
0s - loss: 0.0102 - mean_absolute_percentage_error: 1766814.5450
Epoch 95/100
0s - loss: 0.0102 - mean_absolute_percentage_error: 1767510.8146
Epoch 96/100
0s - loss: 0.0101 - mean_absolute_percentage_error: 1767686.9054
Epoch 97/100
0s - loss: 0.0101 - mean_absolute_percentage_error: 1767076.2169
Epoch 98/100
0s - loss: 0.0100 - mean_absolute_percentage_error: 1767014.8481
Epoch 99/100
0s - loss: 0.0100 - mean_absolute_percentage_error: 1766592.8125
Epoch 100/100
0s - loss: 0.0100 - mean_absolute_percentage_error: 1766348.6332

My code that I ran (which omits the prediction part) goes as follows:

import numpy
from numpy import array
import matplotlib.pyplot as plt
from pandas import read_csv
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error

# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
        dataX, dataY = [], []
        for i in range(len(dataset)-look_back-1):
                a = dataset[i:(i+look_back), 0]
                dataX.append(a)
                dataY.append(dataset[i + look_back, 0])
        return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
dataframe = read_csv('airlinepassdata.csv', usecols=[1], engine='python', skipfooter=3)
dataset = dataframe.values

#dataset = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam', metrics=['mape'])
model.fit(trainX, trainY, nb_epoch=100, batch_size=50, verbose=2)
like image 622
Srivatsa Sharma G Avatar asked Apr 09 '18 09:04

Srivatsa Sharma G


People also ask

What does a high MAPE mean?

Since MAPE is a measure of error, high numbers are bad and low numbers are good. For reporting purposes, some companies will translate this to accuracy numbers by subtracting the MAPE from 100.

Why is MAE so high?

High MAE even after 2000 epochs means that the model is too small. Try increasing the number of Dense Layers. Also print and check if the inputs and outputs are passed on to the model correctly ( maybe some bug during normalization ). You mean increasing the number of each Dense Layersor add other Dense Layers.

Is it possible to have a MAPE higher than 100?

Expressed as a percentage, which is scale-independent and can be used for comparing forecasts on different scales. We should remember though that the values of MAPE may exceed 100%.

Is a large MAPE good?

MAPE is a percentage error metric where the value corresponds to the average amount of error that predictions have. Therefore, a lower MAPE is better, where the lower the value the more accurate the model is.


1 Answers

I solved this by setting the fuzz factor epsilon to one with keras.backend.set_epsilon(1) before calling the compile.

The hint was in the source code

def mean_absolute_percentage_error(y_true, y_pred):
diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),
                                        K.epsilon(),
                                        None))
return 100. * K.mean(diff, axis=-1)

Meaning that, for some unknown reason, the K.abs(y_true) term in the MAPE calculation on the training set is lower than the fuzz default (1e-7), so it uses that default value instead, thus the huge numbers.

like image 194
Guile Avatar answered Oct 24 '22 06:10

Guile