skbonus.metrics package

Module contents

Various loss functions and metrics.

skbonus.metrics.mean_absolute_deviation(y_true: numpy.ndarray, y_pred: numpy.ndarray, sample_weight: Optional[numpy.ndarray] = None, multioutput: str = 'uniform_average')float

Return the MAD (Mean Absolute Deviation) of a prediction.

The formula is np.mean(np.abs(y_true - y_pred)).

Parameters
  • y_true (np.ndarray) – Observed values.

  • y_pred (np.ndarray) – Predicted values.

  • sample_weight (Optional[np.ndarray], default=None) – Individual weights for each sample.

  • multioutput ({"raw_values", "uniform_average"} or array-like) –

    Defines aggregating of multiple output values. Array-like value defines weights used to average errors. If input is list then the shape must be (n_outputs,).

    • ”raw_values”: Returns a full set of errors in case of multioutput input.

    • ”uniform_average”: Errors of all outputs are averaged with uniform weight.

Returns

MAD value of the input.

Return type

float

Examples

>>> import numpy as np
>>> y_true = np.array([1, 2, 4])
>>> y_pred = np.array([1, 1, 2])
>>> mean_absolute_deviation(y_true, y_pred)
1.0
skbonus.metrics.mean_absolute_percentage_error(y_true: numpy.ndarray, y_pred: numpy.ndarray, sample_weight: Optional[numpy.ndarray] = None, multioutput: str = 'uniform_average')float

Return the MAPE (Mean Absolute Percentage Error) of a prediction.

The formula is np.mean(np.abs((y_true - y_pred) / y_true)).

Parameters
  • y_true (np.ndarray) – Observed values.

  • y_pred (np.ndarray) – Predicted values.

  • sample_weight (Optional[np.ndarray], default=None) – Individual weights for each sample.

  • multioutput ({"raw_values", "uniform_average"} or array-like) –

    Defines aggregating of multiple output values. Array-like value defines weights used to average errors. If input is list then the shape must be (n_outputs,).

    • ”raw_values”: Returns a full set of errors in case of multioutput input.

    • ”uniform_average”: Errors of all outputs are averaged with uniform weight.

Returns

MAPE value of the input.

Return type

float

Examples

>>> import numpy as np
>>> y_true = np.array([1, 2, 4])
>>> y_pred = np.array([1, 1, 2])
>>> mean_absolute_percentage_error(y_true, y_pred)
0.3333333333333333
skbonus.metrics.mean_arctangent_absolute_percentage_error(y_true: numpy.ndarray, y_pred: numpy.ndarray, sample_weight: Optional[numpy.ndarray] = None, multioutput: str = 'uniform_average')float

Return the MAAPE (Mean Arctangent Absolute Percentage Error) of a prediction.

The formula is np.mean(np.arctan(np.abs((y_true - y_pred) / y_true))).

Parameters
  • y_true (np.ndarray) – Observed values.

  • y_pred (np.ndarray) – Predicted values.

  • sample_weight (Optional[np.ndarray], default=None) – Individual weights for each sample.

  • multioutput ({"raw_values", "uniform_average"} or array-like) –

    Defines aggregating of multiple output values. Array-like value defines weights used to average errors. If input is list then the shape must be (n_outputs,).

    • ”raw_values”: Returns a full set of errors in case of multioutput input.

    • ”uniform_average”: Errors of all outputs are averaged with uniform weight.

Returns

MAAPE value of the input.

Return type

float

Examples

>>> import numpy as np
>>> y_true = np.array([1, 2, 4])
>>> y_pred = np.array([1, 1, 2])
>>> mean_arctangent_absolute_percentage_error(y_true, y_pred)
0.3090984060005374

Notes

See “A new metric of absolute percentage error for intermittent demand forecasts” by Kim & Kim.

skbonus.metrics.mean_directional_accuracy(y_true: numpy.ndarray, y_pred: numpy.ndarray, sample_weight: Optional[numpy.ndarray] = None, multioutput: str = 'uniform_average')float

Return the MDA (Mean Directional Accuracy) of a prediction.

This is the mean of the vector 1_{sgn(y_true - y_true_lag_1) = sgn(y_pred - y_true_lag_1)}. In plain words, it computes how often the model got the direction of the time series movement right.

Parameters
  • y_true (np.ndarray (non-negative numbers)) – Observed values.

  • y_pred (np.ndarray) – Predicted values.

  • sample_weight (Optional[np.ndarray], default=None) – Individual weights for each sample. The first entry is ignored since the MDA loss term consists of len(y_true) - 1 summands.

  • multioutput ({"raw_values", "uniform_average"} or array-like) –

    Defines aggregating of multiple output values. Array-like value defines weights used to average errors. If input is list then the shape must be (n_outputs,).

    • ”raw_values”: Returns a full set of errors in case of multioutput input.

    • ”uniform_average”: Errors of all outputs are averaged with uniform weight.

Returns

MDA value of the input.

Return type

float

Examples

>>> import numpy as np
>>> y_true = np.array([1, 2, 4])
>>> y_pred = np.array([1, 1, 3])
>>> mean_directional_accuracy(y_true, y_pred)
0.5
skbonus.metrics.mean_log_quotient(y_true: numpy.ndarray, y_pred: numpy.ndarray, sample_weight: Optional[numpy.ndarray] = None, multioutput: str = 'uniform_average')float

Return the MLQ (Mean Log Quotient) of a prediction.

This is np.mean(np.log(y_pred / y_true)**2).

Parameters
  • y_true (np.ndarray (non-negative numbers)) – Observed values.

  • y_pred (np.ndarray) – Predicted values.

  • sample_weight (Optional[np.ndarray], default=None) – Individual weights for each sample.

  • multioutput ({"raw_values", "uniform_average"} or array-like) –

    Defines aggregating of multiple output values. Array-like value defines weights used to average errors. If input is list then the shape must be (n_outputs,).

    • ”raw_values”: Returns a full set of errors in case of multioutput input.

    • ”uniform_average”: Errors of all outputs are averaged with uniform weight.

Returns

MLQ value of the input.

Return type

float

Examples

>>> import numpy as np
>>> y_true = np.array([1, 2, 4])
>>> y_pred = np.array([1, 1, 3])
>>> mean_log_quotient(y_true, y_pred)
0.1877379962427844

Notes

See Tofallis, C (2015) “A Better Measure of Relative Prediction Accuracy for Model Selection and Model Estimation”, Journal of the Operational Research Society, 66(8),1352-1362.

skbonus.metrics.symmetric_mean_absolute_percentage_error(y_true: numpy.ndarray, y_pred: numpy.ndarray, sample_weight: Optional[numpy.ndarray] = None, multioutput: str = 'uniform_average')float

Return the SMAPE (Symmetric Mean Absolute Percentage Error) of a prediction.

The formula is np.mean(np.abs((y_true - y_pred)) / (np.abs(y_true) + np.abs(y_pred))).

Parameters
  • y_true (np.ndarray (non-negative numbers)) – Observed values.

  • y_pred (np.ndarray) – Predicted values.

  • sample_weight (Optional[np.ndarray], default=None) – Individual weights for each sample.

  • multioutput ({"raw_values", "uniform_average"} or array-like) –

    Defines aggregating of multiple output values. Array-like value defines weights used to average errors. If input is list then the shape must be (n_outputs,).

    • ”raw_values”: Returns a full set of errors in case of multioutput input.

    • ”uniform_average”: Errors of all outputs are averaged with uniform weight.

Returns

SMAPE value of the input.

Return type

float

Examples

>>> import numpy as np
>>> y_true = np.array([1, 2, 4])
>>> y_pred = np.array([1, 1, 2])
>>> symmetric_mean_absolute_percentage_error(y_true, y_pred)
0.2222222222222222