import
pandas as pd
import
numpy as np
def
calculate_mape(actual, predicted)
-
>
float
:
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
((
actual
-
predicted)
/
actual))
*
100
,
2
)
if
__name__
=
=
'__main__'
:
actual
=
[
136
,
120
,
138
,
155
,
149
]
predicted
=
[
134
,
124
,
132
,
141
,
149
]
print
(
"py list :"
,
calculate_mape(actual,
predicted),
"%"
)
actual
=
np.array([
136
,
120
,
138
,
155
,
149
])
predicted
=
np.array([
134
,
124
,
132
,
141
,
149
])
print
(
"np array :"
,
calculate_mape(actual,
predicted),
"%"
)
sales_df
=
pd.DataFrame({
"actual"
: [
136
,
120
,
138
,
155
,
149
],
"predicted"
: [
134
,
124
,
132
,
141
,
149
]
})
print
(
"pandas df:"
,
calculate_mape(sales_df.actual,
sales_df.predicted),
"%"
)