-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: check for probabilities containing nan-value in random.choice #11264
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
Conversation
numpy/random/mtrand/mtrand.pyx
Outdated
@@ -1144,6 +1144,8 @@ cdef class RandomState: | |||
raise ValueError("probabilities are not non-negative") | |||
if abs(kahan_sum(pix, d) - 1.) > atol: | |||
raise ValueError("probabilities do not sum to 1") | |||
if np.logical_or.reduce(np.isnan(p)): | |||
raise ValueError("probabilities contain a value that is not a number") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Easier fix - change the previous if to if not (abs(kahan_sum(pix, d) - 1) <= atol):
32938b5
to
84bea81
Compare
thanks eric! i fixed that |
You should add a test too. Can you add a comment explaining that negation is to handled nans? |
I did, but I don't get the test to work. Do you understand what's wrong here?
|
The comparison gives a warning in the tests, and we set up the tests to error on warnings (because we want to know about spurious warnings). Now, you probably do not care about the warning, so the easiest thing is to disable it from the numpy side using: def test():
setup_stuff
with np.seterr(invalid='ignore'):
function_call() |
84bea81
to
bbc0a03
Compare
bbc0a03
to
4c8642f
Compare
done, thanks! |
@@ -1142,7 +1142,9 @@ cdef class RandomState: | |||
raise ValueError("a and p must have same size") | |||
if np.logical_or.reduce(p < 0): | |||
raise ValueError("probabilities are not non-negative") | |||
if abs(kahan_sum(pix, d) - 1.) > atol: | |||
|
|||
# negated to handle NaNs in p |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about it, this would make sense to do to the p < 0
above instead - that would make the warning dissappear too.
So if not np.all(p >= 0): raise ...
@Googl1 this is waiting for a reaction to the suggested change |
@nritsche ping |
fixes #11250