Skip to content

Improve error message for negative values in sample_weight #20036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
GMavgo opened this issue May 4, 2021 · 1 comment · Fixed by #20880
Closed

Improve error message for negative values in sample_weight #20036

GMavgo opened this issue May 4, 2021 · 1 comment · Fixed by #20880

Comments

@GMavgo
Copy link

GMavgo commented May 4, 2021

With sklearn Version 0.22.1 a linear regression with negative sample weights fails with the following (unclear) error message.
Would be helpful if (e.g. _check_sample_weight) checks for negative weights and raises an appropriate error message.

  File "C:\Users\gm889704\.conda\envs\gm\lib\site-packages\sklearn\pipeline.py", line 354, in fit
    self._final_estimator.fit(Xt, y, **fit_params)

  File "C:\Users\gm889704\.conda\envs\gm\lib\site-packages\sklearn\linear_model\_base.py", line 533, in fit
    linalg.lstsq(X, y)

  File "C:\Users\gm889704\.conda\envs\gm\lib\site-packages\scipy\linalg\basic.py", line 1157, in lstsq
    a1 = _asarray_validated(a, check_finite=check_finite)

  File "C:\Users\gm889704\.conda\envs\gm\lib\site-packages\scipy\_lib\_util.py", line 246, in _asarray_validated
    a = toarray(a)

  File "C:\Users\gm889704\.conda\envs\gm\lib\site-packages\numpy\lib\function_base.py", line 499, in asarray_chkfinite
    "array must not contain infs or NaNs")

ValueError: array must not contain infs or NaNs
@glemaitre
Copy link
Member

We could add a parameter only_positive that will be a boolean in _check_sample_weight.
By default, it would be False to be backward compatible and we can it to True and raise an error for negative weight in the estimator where it makes sense (I recall that we did something like this in AdaBoost).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants