Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MAPE calculation in python

I want to calculate Mean Absolute percentage error (MAPE) of predicted and true values. I found a solution from here, but this gives error and shows invalid syntax in the line mask = a <> 0

    def mape_vectorized_v2(a, b): 
    mask = a <> 0
    return (np.fabs(a - b)/a)[mask].mean() 

   def mape_vectorized_v2(a, b): 
       File "<ipython-input-5-afa5c1162e83>", line 1
         def mape_vectorized_v2(a, b):
                                       ^
     SyntaxError: unexpected EOF while parsing

I am using spyder3. My predicted value is a type np.array and true value is dataframe

type(predicted)
Out[7]: numpy.ndarray
type(y_test)
Out[8]: pandas.core.frame.DataFrame

How do i clear this error and proceed with MAPE Calculation ?

Edit :

predicted.head()
Out[22]: 
   Total_kWh
0   7.163627
1   6.584960
2   6.638057
3   7.785487
4   6.994427

y_test.head()
Out[23]: 
     Total_kWh
79         7.2
148        6.7
143        6.7
189        7.2
17         6.4

np.abs(y_test[['Total_kWh']] - predicted[['Total_kWh']]).head()
Out[24]: 
   Total_kWh
0        NaN
1        NaN
2        NaN
3        NaN
4   0.094427
like image 393
Magg_rs Avatar asked Dec 05 '17 07:12

Magg_rs


People also ask

What is MAPE Python?

Absolute Percentage Error (or simply MAPE) also known as Mean Absolute Percentage Deviation (MAPD) in python. The MAPE term determines how better accuracy does our forecast gives.

How do you calculate MAPE?

Once you have the absolute percent error for each data entry, you can calculate the MAPE. Add all the absolute percent errors together, then divide the sum by the number of errors. For example, if your dataset included 12 entries, you would divide the sum by 12. The final result is the MAPE.

How does Python calculate MAPE when actual is zero?

1. Since the formula to calculate absolute percent error is |actual-prediction| / |actual| this means that MAPE will be undefined if any of the actual values are zero. 2. MAPE should not be used with low volume data.

How do you calculate MAPE in linear regression?

The MAPE is calculated by finding the absolute difference between the actual and predicted values, divided by the actual value. These ratios are added for all values and the mean is taken.


3 Answers

In python for compare by not equal need !=, not <>.

So need:

def mape_vectorized_v2(a, b): 
    mask = a != 0
    return (np.fabs(a - b)/a)[mask].mean()

Another solution from stats.stackexchange:

def mean_absolute_percentage_error(y_true, y_pred): 
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
like image 69
jezrael Avatar answered Oct 31 '22 03:10

jezrael


The new version of scikit-learn (v0.24) has a function that will calculate MAPE. sklearn.metrics.mean_absolute_percentage_error

All what you need is two array-like variables: y_true storing the actual/real values, and y_pred storing the predicted values.

You can refer to the official documentation here.

like image 43
O. Mohsen Avatar answered Oct 31 '22 04:10

O. Mohsen


Both solutions are not working with zero values. This is working form me:

def percentage_error(actual, predicted):
    res = np.empty(actual.shape)
    for j in range(actual.shape[0]):
        if actual[j] != 0:
            res[j] = (actual[j] - predicted[j]) / actual[j]
        else:
            res[j] = predicted[j] / np.mean(actual)
    return res

def mean_absolute_percentage_error(y_true, y_pred): 
    return np.mean(np.abs(percentage_error(np.asarray(y_true), np.asarray(y_pred)))) * 100

I hope it helps.

like image 29
Pablo Andrés Castañeda Avatar answered Oct 31 '22 02:10

Pablo Andrés Castañeda