mean_absolute_percentage_error#
- mean_absolute_percentage_error(y_true, y_pred, horizon_weight=None, multioutput='uniform_average', symmetric=False, relative_to='y_true', **kwargs)[source]#
Mean absolute percentage error (MAPE) or symmetric MAPE (sMAPE).
If
symmetricis False, calculates MAPE (mean absolute percentage error). By default, this is relative to the true values. Ifrelative_to="y_pred", then MAPE is relative to the predicted values.If
symmetricis True, calculates sMAPE (symmetric mean absolute percentage error).
All variants output a non-negative floating point value. The best value is 0.0.
MAPE is measured in percentage error relative to the true values.
sMAPE is measured in symmetric percentage error relative to the sum of absolute true and predicted values.
sMAPE is measured in percentage error relative to the test data. Because it takes the absolute value rather than square the percentage forecast error, it penalizes large errors less than MSPE, RMSPE, MdSPE or RMdSPE.
There is no limit on how large the error can be, particularly when denominator values are close to zero. In such cases the function returns a large value instead of
inf.- Parameters:
- y_truepd.Series, pd.DataFrame or np.array of shape (fh,) or (fh, n_outputs) where fh is the forecasting horizon
Ground truth (correct) target values.
- y_predpd.Series, pd.DataFrame or np.array of shape (fh,) or (fh, n_outputs) where fh is the forecasting horizon
Forecasted values.
- horizon_weightarray-like of shape (fh,), default=None
Forecast horizon weights.
- multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’
Defines how to aggregate metric for multivariate (multioutput) data. If array-like, values used as weights to average the errors. If ‘raw_values’, returns a full set of errors in case of multioutput input. If ‘uniform_average’, errors of all outputs are averaged with uniform weight.
- symmetricbool, default=False
Calculates symmetric version of metric if True.
- relative_to{‘y_true’, ‘y_pred’}, default=’y_true’
Determines the denominator of the percentage error.
If ‘y_true’, the denominator is the true values,
If ‘y_pred’, the denominator is the predicted values.
- Returns:
- lossfloat
MAPE or sMAPE loss.
If multioutput is ‘raw_values’, then MAPE or sMAPE is returned for each output separately.
If multioutput is ‘uniform_average’ or an ndarray of weights, then the weighted average MAPE or sMAPE of all output errors is returned.
See also
References
Hyndman, R. J and Koehler, A. B. (2006). “Another look at measures of forecast accuracy”, International Journal of Forecasting, Volume 22, Issue 4.
Examples
>>> import numpy as np >>> from sktime.performance_metrics.forecasting import mean_absolute_percentage_error >>> # Univariate MAPE >>> y_true = np.array([3, -0.5, 2, 7, 2]) >>> y_pred = np.array([2.5, 0.0, 2, 8, 1.25]) >>> mean_absolute_percentage_error(y_true, y_pred, symmetric=False) np.float64(0.33690476190476193) >>> # Univariate sMAPE >>> mean_absolute_percentage_error(y_true, y_pred, symmetric=True) np.float64(0.5553379953379953) >>> # Multivariate MAPE >>> y_true = np.array([[0.5, 1], [-1, 1], [7, -6]]) >>> y_pred = np.array([[0, 2], [-1, 2], [8, -5]]) >>> mean_absolute_percentage_error(y_true, y_pred, symmetric=False) np.float64(0.5515873015873016) >>> # Multivariate sMAPE >>> mean_absolute_percentage_error(y_true, y_pred, symmetric=True) np.float64(0.6080808080808081) >>> # Raw output per dimension >>> mean_absolute_percentage_error(y_true, y_pred, multioutput='raw_values', symmetric=False) array([0.38095238, 0.72222222]) >>> mean_absolute_percentage_error(y_true, y_pred, multioutput='raw_values', symmetric=True) array([0.71111111, 0.50505051]) >>> # Weighted multioutput >>> mean_absolute_percentage_error(y_true, y_pred, multioutput=[0.3, 0.7], symmetric=False) np.float64(0.6198412698412699) >>> mean_absolute_percentage_error(y_true, y_pred, multioutput=[0.3, 0.7], symmetric=True) np.float64(0.5668686868686869) >>> # Univariate MAPE relative to predicted values >>> mean_absolute_percentage_error(y_true, y_pred, relative_to='y_pred')