-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: Allow size=0 in numpy.random.choice #11383
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,8 @@ | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
include "Python.pxi" | ||
include "randint_helpers.pxi" | ||
include "numpy.pxd" | ||
include "randint_helpers.pxi" | ||
include "cpython/pycapsule.pxd" | ||
|
||
from libc cimport string | ||
|
@@ -988,9 +988,9 @@ cdef class RandomState: | |
raise ValueError("low is out of bounds for %s" % dtype) | ||
if ihigh > highbnd: | ||
raise ValueError("high is out of bounds for %s" % dtype) | ||
if ilow >= ihigh: | ||
raise ValueError("low >= high") | ||
|
||
if ilow >= ihigh and np.prod(size) != 0: | ||
raise ValueError("Range cannot be empty (low >= high) unless no samples are taken") | ||
with self.lock: | ||
ret = randfunc(ilow, ihigh - 1, size, self.state_address) | ||
|
||
|
@@ -1114,15 +1114,15 @@ cdef class RandomState: | |
# __index__ must return an integer by python rules. | ||
pop_size = operator.index(a.item()) | ||
except TypeError: | ||
raise ValueError("a must be 1-dimensional or an integer") | ||
if pop_size <= 0: | ||
raise ValueError("a must be greater than 0") | ||
raise ValueError("'a' must be 1-dimensional or an integer") | ||
if pop_size <= 0 and np.prod(size) != 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a bit unintuitive that this works for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That also has the advantage that |
||
raise ValueError("'a' must be greater than 0 unless no samples are taken") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I somewhat thought it was backticks ;). This is good, I do not think we or python has serious guidelines for errors. |
||
elif a.ndim != 1: | ||
raise ValueError("a must be 1-dimensional") | ||
raise ValueError("'a' must be 1-dimensional") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it is normal to escape argument names in error messages, see, e.g., https://github.com/numpy/numpy/blob/master/numpy/random/mtrand/mtrand.pyx#L988 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My guess is, you can find examples for everything both in numpy and the standard lib. Personally, I think quotes probably make it slightly more discoverable what Anyway, I think the PR looks good and you guys can put it in if you like. |
||
else: | ||
pop_size = a.shape[0] | ||
if pop_size is 0: | ||
raise ValueError("a must be non-empty") | ||
if pop_size is 0 and np.prod(size) != 0: | ||
raise ValueError("'a' cannot be empty unless no samples are taken") | ||
|
||
if p is not None: | ||
d = len(p) | ||
|
@@ -1136,9 +1136,9 @@ cdef class RandomState: | |
pix = <double*>PyArray_DATA(p) | ||
|
||
if p.ndim != 1: | ||
raise ValueError("p must be 1-dimensional") | ||
raise ValueError("'p' must be 1-dimensional") | ||
if p.size != pop_size: | ||
raise ValueError("a and p must have same size") | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if you test for an empty probabilities array, you will see that this check fails also, so might as well allow that too? |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -440,6 +440,15 @@ def test_choice_return_shape(self): | |
assert_equal(np.random.choice(6, s, replace=False, p=p).shape, s) | ||
assert_equal(np.random.choice(np.arange(6), s, replace=True).shape, s) | ||
|
||
# Check zero-size | ||
assert_equal(np.random.randint(0, 0, size=(3, 0, 4)).shape, (3, 0, 4)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
assert_equal(np.random.randint(0, -10, size=0).shape, (0,)) | ||
assert_equal(np.random.randint(10, 10, size=0).shape, (0,)) | ||
assert_equal(np.random.choice(0, size=0).shape, (0,)) | ||
assert_equal(np.random.choice([], size=(0,)).shape, (0,)) | ||
assert_equal(np.random.choice(['a', 'b'], size=(3, 0, 4)).shape, (3, 0, 4)) | ||
assert_raises(ValueError, np.random.choice, [], 10) | ||
|
||
def test_bytes(self): | ||
np.random.seed(self.seed) | ||
actual = np.random.bytes(10) | ||
|
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.
Was suprised for a bit here, but I guess we do it like a python
range
and allow strange ranges as empty ranges, seems fine to me.