# How to Calculate SMAPE in Python?

• Last Updated : 28 Nov, 2021

In this article, we will see how to compute one of the methods to determine forecast accuracy called the Symmetric Mean Absolute Percentage Error (or simply SMAPE) in Python.

The SMAPE is one of the alternatives to overcome the limitations with MAPE forecast error measurement. In contrast to the mean absolute percentage error, SMAPE has both a lower bound and an upper bound, therefore, it is known as symmetric. The ‘S’ in SMAPE stands for symmetric, ‘M’ stands for mean which takes in the average value over a series, ‘A’ stands for absolute that uses absolute values to keep the positive and negative errors from canceling one another out, ‘P’ is the percentage which makes this accuracy metric a relative metric, and the ‘E’ stands for error since this metric helps to determine the amount of error our forecast has.

### The formula for SMAPE: SMAPE Formula

Consider the following example, where we have the sales information of a store. The day column represent the day number which we are referring to, the actual sales column represents the actual sales value for the respective day whereas the forecast sales column represents the forecast-ed values for the sales figures (probably with an ML model). The final column is the division between 3rd last and the 2nd last columns.

The SMAPE value for the above example will be the mean value of the entries in A/B column. The value comes out to be 0.0368.

## Python

 `import` `pandas as pd ` `import` `numpy as np ` ` `  `# Define the function to return the SMAPE value ` `def` `calculate_smape(actual, predicted) ``-``> ``float``: ` ` `  `    ``# Convert actual and predicted to numpy ` `    ``# array data type if not already ` `    ``if` `not` `all``([``isinstance``(actual, np.ndarray),  ` `                ``isinstance``(predicted, np.ndarray)]): ` `        ``actual, predicted ``=` `np.array(actual), ` `        ``np.array(predicted) ` ` `  `    ``return` `round``( ` `        ``np.mean( ` `            ``np.``abs``(predicted ``-` `actual) ``/`  `            ``((np.``abs``(predicted) ``+` `np.``abs``(actual))``/``2``) ` `        ``)``*``100``, ``2` `    ``) ` ` `  ` `  `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``# CALCULATE SMAPE FROM PYTHON LIST ` ` `  `    ``actual    ``=` `[``136``, ``120``, ``138``, ``155``, ``149``] ` `    ``predicted ``=` `[``134``, ``124``, ``132``, ``141``, ``149``] ` ` `  `    ``# Get SMAPE for python list as parameters ` `    ``print``(``"py list  :"``,  ` `          ``calculate_smape(actual, predicted), ``"%"``) ` ` `  `    ``# CALCULATE SMAPE FROM NUMPY ARRAY ` `    ``actual    ``=` `np.array([``136``, ``120``, ``138``, ``155``, ``149``]) ` `    ``predicted ``=` `np.array([``134``, ``124``, ``132``, ``141``, ``149``]) ` ` `  `    ``# Get SMAPE for python list as parameters ` `    ``print``(``"np array :"``,  ` `          ``calculate_smape(actual, predicted), ``"%"``) ` ` `  `    ``# CALCULATE SMAPE FROM PANDAS DATAFRAME ` `    ``# Define the pandas dataframe ` `    ``sales_df ``=` `pd.DataFrame({ ` `        ``"actual"`    `: [``136``, ``120``, ``138``, ``155``, ``149``], ` `        ``"predicted"` `: [``134``, ``124``, ``132``, ``141``, ``149``] ` `    ``}) ` ` `  `    ``# Get SMAPE for pandas series as parameters ` `    ``print``(``"pandas df:"``, calculate_smape(sales_df.actual,  ` `                                        ``sales_df.predicted), ``"%"``)`

Output:

```py list  : 3.73 %
np array : 3.73 %
pandas df: 3.73 %```

Explanation:

In the program, we have calculated the SMAPE metric value for the same dataset provided in 3 different data type formats as function parameters, namely, python list, NumPy array, and pandas dataframe. The function is generalized to work with any python series-like data as input parameters. The function first converts the datatypes as numpy array so that the calculation becomes easier using the NumPy methods. The return statement can be explained through the following image: SMAPE code Exl

My Personal Notes arrow_drop_up
Recommended Articles
Page :
{"ft_lang_label":"__label__en","ft_lang_prob":0.63379514,"math_prob":0.9594849,"size":3577,"snap":"2022-27-2022-33","text_gpt3_token_len":978,"char_repetition_ratio":0.12286594,"word_repetition_ratio":0.09009009,"special_character_ratio":0.32177803,"punctuation_ratio":0.15750371,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9993784,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-27T11:11:48Z\",\"WARC-Record-ID\":\"<urn:uuid:93cef217-65a0-4bfa-ab37-5f1338c39dd6>\",\"Content-Length\":\"171960\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cf70571d-35c6-4e3b-8007-ff20c1e2b355>\",\"WARC-Concurrent-To\":\"<urn:uuid:51c93c41-29a4-4ae2-83c1-8cb9881b8eff>\",\"WARC-IP-Address\":\"44.228.100.190\",\"WARC-Target-URI\":\"https://origin.geeksforgeeks.org/how-to-calculate-smape-in-python/\",\"WARC-Payload-Digest\":\"sha1:SWLM4QUMUL4GRBDBFISR3PTT2PIN6FUD\",\"WARC-Block-Digest\":\"sha1:DN5AY2YI3F4NV5KGCLRUKPR57VUFBUQF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103331729.20_warc_CC-MAIN-20220627103810-20220627133810-00060.warc.gz\"}"}