Talip7/scikit-learn-issues-multilabel huggingface dataset:
DOC: Add user guide entry for sample weights

> [!WARNING]
> Note to potential contributors: this is not a good issue to work on, please don't comment "can I work on it" or you may be banned from the repo.
> There is NO action that needs to be taken for this issue atm - there are probably some questions around weights that need to be addressed first. 

### Describe the issue linked to the documentation

As discussed in #30564 it is probably worthwhile to add a 'sample weight' section in the user guide.

Here are some notable additions, that we thought were too long to include in the glossary entry:

* Examples of uses of sample weights
   * in supervised learning one of the main reasons for weights is to account for the different size/time/exposure/... of each observation. We are assuming weights are inversely proportional to the variance of the target, e.g., we use exposure as weights in [Poisson regression and non-normal loss](https://scikit-learn.org/dev/auto_examples/linear_model/plot_poisson_regression_non_normal_loss.html) and [Tweedie regression on insurance claims](https://scikit-learn.org/dev/auto_examples/linear_model/plot_tweedie_regression_insurance_claims.html), with longer exposure duration being weighted higher, as these would be considered more reliable, or lower variance. (details: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2583340580, https://github.com/scikit-learn/scikit-learn/pull/30564#discussion_r2338421172) 
   * a nice collection of papers with examples of weight use here: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2567939034
* Should we try to use similar terms as other statistical packages (e.g., stata, julia) - "analytical weights", "frequency weights"... (stata reference  http://www.stata.com/manuals13/u20.pdf#u20.23Weightedestimation , see section 20.23)
* Re-weighting for transfer learning (ref: https://github.com/scikit-learn/scikit-learn/pull/30564#pullrequestreview-2552200734)
* Third-party libraries can also use `sample_weight`-compatible estimators as building blocks to reduce a specific statistical task into a weighted regression or classification task

cc @ogrisel @lorentzenchr 

### Suggest a potential alternative/fix

_No response_

COMMENT:
Hi, I’d like to work on this issue if it’s still open

Labels: Documentation, Hard
DOC: Add user guide entry for sample weights

> [!WARNING]
> Note to potential contributors: this is not a good issue to work on, please don't comment "can I work on it" or you may be banned from the repo.
> There is NO action that needs to be taken for this issue atm - there are probably some questions around weights that need to be addressed first. 

### Describe the issue linked to the documentation

As discussed in #30564 it is probably worthwhile to add a 'sample weight' section in the user guide.

Here are some notable additions, that we thought were too long to include in the glossary entry:

* Examples of uses of sample weights
   * in supervised learning one of the main reasons for weights is to account for the different size/time/exposure/... of each observation. We are assuming weights are inversely proportional to the variance of the target, e.g., we use exposure as weights in [Poisson regression and non-normal loss](https://scikit-learn.org/dev/auto_examples/linear_model/plot_poisson_regression_non_normal_loss.html) and [Tweedie regression on insurance claims](https://scikit-learn.org/dev/auto_examples/linear_model/plot_tweedie_regression_insurance_claims.html), with longer exposure duration being weighted higher, as these would be considered more reliable, or lower variance. (details: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2583340580, https://github.com/scikit-learn/scikit-learn/pull/30564#discussion_r2338421172) 
   * a nice collection of papers with examples of weight use here: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2567939034
* Should we try to use similar terms as other statistical packages (e.g., stata, julia) - "analytical weights", "frequency weights"... (stata reference  http://www.stata.com/manuals13/u20.pdf#u20.23Weightedestimation , see section 20.23)
* Re-weighting for transfer learning (ref: https://github.com/scikit-learn/scikit-learn/pull/30564#pullrequestreview-2552200734)
* Third-party libraries can also use `sample_weight`-compatible estimators as building blocks to reduce a specific statistical task into a weighted regression or classification task

cc @ogrisel @lorentzenchr 

### Suggest a potential alternative/fix

_No response_

COMMENT:
Hi,I am new here.If you need a contributor to act this in the future,you can assign this to me.I woud like to do my first contribute(because here is no "good first issue" for a lang time)

Labels: Documentation, Hard
DOC: Add user guide entry for sample weights

> [!WARNING]
> Note to potential contributors: this is not a good issue to work on, please don't comment "can I work on it" or you may be banned from the repo.
> There is NO action that needs to be taken for this issue atm - there are probably some questions around weights that need to be addressed first. 

### Describe the issue linked to the documentation

As discussed in #30564 it is probably worthwhile to add a 'sample weight' section in the user guide.

Here are some notable additions, that we thought were too long to include in the glossary entry:

* Examples of uses of sample weights
   * in supervised learning one of the main reasons for weights is to account for the different size/time/exposure/... of each observation. We are assuming weights are inversely proportional to the variance of the target, e.g., we use exposure as weights in [Poisson regression and non-normal loss](https://scikit-learn.org/dev/auto_examples/linear_model/plot_poisson_regression_non_normal_loss.html) and [Tweedie regression on insurance claims](https://scikit-learn.org/dev/auto_examples/linear_model/plot_tweedie_regression_insurance_claims.html), with longer exposure duration being weighted higher, as these would be considered more reliable, or lower variance. (details: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2583340580, https://github.com/scikit-learn/scikit-learn/pull/30564#discussion_r2338421172) 
   * a nice collection of papers with examples of weight use here: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2567939034
* Should we try to use similar terms as other statistical packages (e.g., stata, julia) - "analytical weights", "frequency weights"... (stata reference  http://www.stata.com/manuals13/u20.pdf#u20.23Weightedestimation , see section 20.23)
* Re-weighting for transfer learning (ref: https://github.com/scikit-learn/scikit-learn/pull/30564#pullrequestreview-2552200734)
* Third-party libraries can also use `sample_weight`-compatible estimators as building blocks to reduce a specific statistical task into a weighted regression or classification task

cc @ogrisel @lorentzenchr 

### Suggest a potential alternative/fix

_No response_

COMMENT:
What would need to be contributed? I'm interested in helping if that's ok.

Labels: Documentation, Hard
DOC: Add user guide entry for sample weights

> [!WARNING]
> Note to potential contributors: this is not a good issue to work on, please don't comment "can I work on it" or you may be banned from the repo.
> There is NO action that needs to be taken for this issue atm - there are probably some questions around weights that need to be addressed first. 

### Describe the issue linked to the documentation

As discussed in #30564 it is probably worthwhile to add a 'sample weight' section in the user guide.

Here are some notable additions, that we thought were too long to include in the glossary entry:

* Examples of uses of sample weights
   * in supervised learning one of the main reasons for weights is to account for the different size/time/exposure/... of each observation. We are assuming weights are inversely proportional to the variance of the target, e.g., we use exposure as weights in [Poisson regression and non-normal loss](https://scikit-learn.org/dev/auto_examples/linear_model/plot_poisson_regression_non_normal_loss.html) and [Tweedie regression on insurance claims](https://scikit-learn.org/dev/auto_examples/linear_model/plot_tweedie_regression_insurance_claims.html), with longer exposure duration being weighted higher, as these would be considered more reliable, or lower variance. (details: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2583340580, https://github.com/scikit-learn/scikit-learn/pull/30564#discussion_r2338421172) 
   * a nice collection of papers with examples of weight use here: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2567939034
* Should we try to use similar terms as other statistical packages (e.g., stata, julia) - "analytical weights", "frequency weights"... (stata reference  http://www.stata.com/manuals13/u20.pdf#u20.23Weightedestimation , see section 20.23)
* Re-weighting for transfer learning (ref: https://github.com/scikit-learn/scikit-learn/pull/30564#pullrequestreview-2552200734)
* Third-party libraries can also use `sample_weight`-compatible estimators as building blocks to reduce a specific statistical task into a weighted regression or classification task

cc @ogrisel @lorentzenchr 

### Suggest a potential alternative/fix

_No response_

COMMENT:
@youbos @freesoulinempty @jorgepaezcodes no it's not a good issue to start contributing. I would suggest you look at the GitHub's [How to contribute to Open-Source guide](https://opensource.guide/how-to-contribute/) to have a good overview of how open-source works in general.

Please then have a look at our [contributing doc](https://scikit-learn.org/dev/developers/contributing.html) and in particular the section [Issues for new contributors](https://scikit-learn.org/dev/developers/contributing.html#new-contributors).

In particular we don't assign issues, there is no need to say "please assign this to me" not sure where you got this impression from ... as a maintainer this is going to sound a bit harsh, but this feels like unwanted noise. I will try to clarify this point in our contribution guide if it's not clear already.

Labels: Documentation, Hard
DOC: Add user guide entry for sample weights

> [!WARNING]
> Note to potential contributors: this is not a good issue to work on, please don't comment "can I work on it" or you may be banned from the repo.
> There is NO action that needs to be taken for this issue atm - there are probably some questions around weights that need to be addressed first. 

### Describe the issue linked to the documentation

As discussed in #30564 it is probably worthwhile to add a 'sample weight' section in the user guide.

Here are some notable additions, that we thought were too long to include in the glossary entry:

* Examples of uses of sample weights
   * in supervised learning one of the main reasons for weights is to account for the different size/time/exposure/... of each observation. We are assuming weights are inversely proportional to the variance of the target, e.g., we use exposure as weights in [Poisson regression and non-normal loss](https://scikit-learn.org/dev/auto_examples/linear_model/plot_poisson_regression_non_normal_loss.html) and [Tweedie regression on insurance claims](https://scikit-learn.org/dev/auto_examples/linear_model/plot_tweedie_regression_insurance_claims.html), with longer exposure duration being weighted higher, as these would be considered more reliable, or lower variance. (details: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2583340580, https://github.com/scikit-learn/scikit-learn/pull/30564#discussion_r2338421172) 
   * a nice collection of papers with examples of weight use here: https://github.com/scikit-learn/scikit-learn/pull/30564#issuecomment-2567939034
* Should we try to use similar terms as other statistical packages (e.g., stata, julia) - "analytical weights", "frequency weights"... (stata reference  http://www.stata.com/manuals13/u20.pdf#u20.23Weightedestimation , see section 20.23)
* Re-weighting for transfer learning (ref: https://github.com/scikit-learn/scikit-learn/pull/30564#pullrequestreview-2552200734)
* Third-party libraries can also use `sample_weight`-compatible estimators as building blocks to reduce a specific statistical task into a weighted regression or classification task

cc @ogrisel @lorentzenchr 

### Suggest a potential alternative/fix

_No response_

COMMENT:
@lesteve  I've actually specifically said this in the original comment:

> Note to contributors: there is NO action that needs to be taken for this issue atm - there are probably some questions around weights that need to be addressed first.

If this has been read they would realise this is not an issue that needs any action at the moment. Thus I don't think you were harsh and I think those comments are noise.

Labels: Documentation, Hard
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:
Hi @arnauplanasbahi-rgb! 

I'm interested in implementing this feature. Before I start working on it, 
I have a few questions to ensure we're aligned:

Questions:
1. Should this be a completely new function (e.g., `weighted_absolute_percentage_error`) 
   or a parameter added to the existing `mean_absolute_percentage_error`?

2. For the proposed formula `sum(|y_pred - y|) / sum(|y|)`, what should happen when 
   `sum(|y|) = 0`? Should it raise an error or return infinity/NaN?

3. Should this follow the same multioutput handling as MAPE 
   ('raw_values', 'uniform_average', etc.)?

4. Any specific edge cases you'd like handled?

I'd be happy to draft a PR once we agree on the design approach. 
Looking forward to feedback from the maintainers as well!

Thanks!

Labels: module:metrics
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:
Hi @bhaskargurram-ai 

Thanks a lot for your interest and for the thoughtful questions! I’ll try to answer them as best I can, though the maintainers will likely have stronger opinions on some of these points.

1. My initial thought was that this behavior could have been the expected one for mean_absolute_percentage_error when weights are provided. But on second thought, implementing it cleanly without breaking the current logic might be tricky. Creating a separate function could be simpler, even if it feels a bit redundant. So I don’t have a strong preference here.
2. 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.
3. Yes, that’s my understanding too. Keeping the same multioutput handling as MAPE ('raw_values', 'uniform_average', etc.) would make sense for consistency.
4. Apart from the all-zero case, the other relevant edge cases (e.g., NaNs, empty arrays, arrays of different sizes, etc.) could follow the same behavior as MAPE.


Thanks again for engaging so constructively with this — I really appreciate it!

Best,
Arnau

Labels: module:metrics
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 @arnauplanasbahi-rgb for the detailed responses!

Before I start implementing, I'd like to get input from the maintainers on the design approach, since this is a new feature rather than a bug fix.

@ogrisel @glemaitre @adrinjalali - Could you please advise on:

1. **API Design**: Should this be:
   - A new function `weighted_absolute_percentage_error()` 
   - OR a parameter in existing `mean_absolute_percentage_error()` (e.g., `use_weights_as_values=True`)
   - OR something else?

2. **Naming**: If a new function, what would be the preferred name?
   - `weighted_absolute_percentage_error()`
   - `wape()` / `weighted_ape()`
   - Something else?

3. **Scope**: Should this be part of sklearn 1.7 roadmap or considered for a future release?

I'm happy to implement this once we have agreement on the design! 🚀

Thanks!

Labels: module:metrics
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:
I think the current implementation and documentation are consistent, and I don't see any unexpected behaviour.

The [user guide](https://scikit-learn.org/stable/modules/model_evaluation.html#mean-absolute-percentage-error) explicitly defines MAPE as:

$$\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}{\max \left( \varepsilon, \vert y_i \vert \right)}$$

instead of:

$$\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}$$

"to avoid undefined results when y is zero." This behaviour is intentional and well-documented.

Secondly, when all sample weights are zero, an `ZeroDivisorError` will raise, even when `y_pred` is zero:

```python
from sklearn.metrics import mean_absolute_percentage_error

y_true = [1., 0., 2.4, 7.]
y_pred = [1.2, 0.1, 2.4, 8.]
sample_weight = [0., 0., 0., 0.]

# ZeroDivisionError: Weights sum to zero, can't be normalized
mean_absolute_percentage_error(y_true, y_pred, sample_weight = sample_weight)
```

If you still believe there's an inconsistency, please provide a minimal, reproducible example showing how the current implementation produces an unexpected result. That would help clarify where the bug is.

Labels: module:metrics