Weighted MAPE behaviour on 0

### Describe the workflow you want to enable

Hello,

I create this ticket because I realized unexpected behaviour for Weighted MAPE when you weight for the real values and those values are 0.

By the formula:

<img width="217" height="73" alt="Image" src="https://github.com/user-attachments/assets/2e874378-3674-4861-9e4f-91866c7d23bd" />

In the case the weights are the same as the a_i, the values should cancel out (even at 0 because the limit is well defined and can be extended to 0) and coincide with this:

<img width="202" height="62" alt="Image" src="https://github.com/user-attachments/assets/0c6ac99d-d69f-4326-90c3-b97dd709e966" /> (1)

Looking at the current implementation:

https://github.com/scikit-learn/scikit-learn/blob/c60dae2060/sklearn/metrics/_regression.py#L408

what happens with zeros is that they are removed from the computation.


I understand this needs to be like this to allow for generic weights, that are not the real values, but this is creating an unexpected behaviour.
Maybe there should be another class that implements directly (1), to avoid problems with it.

Best,

Arnau

### Describe your proposed solution

I wold suggest create another classe wape that directly calculates:
sum(|y_pred- y|)/sum(|y|)
To avoid this unexpected behaviour.

### Describe alternatives you've considered, if relevant

I don't think changing the current class would be a good option since it is implemented in the only way that can allow for generic weights.

### Additional context

_No response_

COMMENT:
Thanks for clarifying. I now see that your concern isn't about a bug in scikit-learn's [<code>mean_absolute_percentage_error</code>](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_absolute_percentage_error.html#sklearn.metrics.mean_absolute_percentage_error).

Any implementation of the strict version of MAPE:

$$\displaystyle \mathrm{MAPE}\left(y, \hat{y} \right) = \frac{1}{n_{\mathrm{sample}}} \sum_{i=0}^{{n_{\mathrm{sample}} - 1}} \frac{\vert y_i - \hat{y}_i \vert}{\vert y_i \vert}$$

would still have to handle zero-division when $y_i = 0$ for some $i$, even if sample weights are provided. Some possible approaches are:

1. Perform input validation and raise an error if any $y_i = 0$.
2.  Return `NaN` or $\pm \infty$, as you mentioned:

> sum(|y|) would only be zero if all true values are zero — a rare edge case. I’d suggest returning inf if sum(|y_pred - y|) > 0, and NaN if both sums are zero.

scikit-learn's implementation is to avoid zero-division entirely by replacing zero entries in $y$ with a small $\varepsilon > 0$.

Since your suggestion is to have a different handling for zero-division, and the implementation of MAPE is relatively simple, I'd suggest letting users define their own version of MAPE if they need to handle the zero-division issue differently than scikit-learn's convention. They can wrap it with [<code>make_scorer</code>](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.make_scorer.html) if they wish to integrate it with scikit-learn's estimators.

With that said, I'm closing this issue. Thank you for the thoughtful discussion and for your understanding.

Labels: module:metrics