mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-20 09:37:04 -05:00
23 lines
604 B
Python
23 lines
604 B
Python
import pandas as pd
|
|
import numpy as np
|
|
|
|
|
|
def APE(actual, pred):
|
|
"""
|
|
Calculate absolute percentage error.
|
|
Returns a vector of APE values with same length as actual/pred.
|
|
"""
|
|
return 100 * np.abs((actual - pred) / actual)
|
|
|
|
|
|
def MAPE(actual, pred):
|
|
"""
|
|
Calculate mean absolute percentage error.
|
|
Remove NA and values where actual is close to zero
|
|
"""
|
|
not_na = ~(np.isnan(actual) | np.isnan(pred))
|
|
not_zero = ~np.isclose(actual, 0.0)
|
|
actual_safe = actual[not_na & not_zero]
|
|
pred_safe = pred[not_na & not_zero]
|
|
return np.mean(APE(actual_safe, pred_safe))
|