Repository navigation
ENH: Add axis argument to random.permutation and random.shuffle - #13829
Conversation
wkschwartz
left a comment
There was a problem hiding this comment.
Needs documentation and possibly input validation/error handling for the new argument.
51d28d2 to
c91cd0f
Compare
c91cd0f to
4ff95a7
Compare
|
Once that print statement is removed, I think this is ready for review by core maintainers. @kianasun, the docs say that the next step is for you to e-mail the NumPy mailing list to request a code review. |
4ff95a7 to
8008eb2
Compare
|
@wkschwartz OK. I will send an email after all checks are done. Thank you! |
|
Should this PR be extended to address #11583, and there by make the two functions have more similar signatures? |
bashtage
left a comment
There was a problem hiding this comment.
You can't add any features to mtrand.pyx.
|
It would be nice to mirror the api in shuffle as well since permutation and shuffle are closely related. Shuffle could be implemented using two calls to |
9f57893 to
3deeea9
Compare
|
Hi @bashtage , I have updated the code :) |
|
Some small issues but this needs to go to the mailing list since the API is a bit tricky since shuffle has 3 distinct paths/uses and only one needs axis. |
There was a problem hiding this comment.
The tests immediately above this cover a variety of input types. Should the axis argument be paired in combination with them?
There was a problem hiding this comment.
I am not sure. If combine them, we might need to test errors for different input types. For example lambda x: np.array([]) which has one dimension, if we set axis=1, do we need to assert AxisError?
There was a problem hiding this comment.
Yes, axis=1 should raise AxisError on a 1d array
There was a problem hiding this comment.
In that case, will it be better to combine them since we need to raise errors for some but not all types?
aeff0bb to
9bb7b23
Compare
|
Hi @bashtage @wkschwartz, I have updated the code as recommended (except a few questions I asked). By the way, do I need to change anything to pass the check |
|
@kianasun You should propose this enhancement on the NumPy mailing list. |
|
Hi @bashtage, I sent an email to Numpy-discussion last week. |
|
@kianasun there are conflicts and changes have been requested. |
9bb7b23 to
443d410
Compare
|
@mattip updated. |
|
resent a message to the mailing list since the one from July had no responses. |
443d410 to
d3858b0
Compare
d3858b0 to
53398fd
Compare
|
The mailing list discussion was positive. I will merge this soon if there are no more comments. |
There was a problem hiding this comment.
This doesn't make any sense to me - the message it prints out will just be wrong if a.ndim isn't 1.
There was a problem hiding this comment.
@eric-wieser It seems for untyped path, the function only shuffle on 0 axis?
There was a problem hiding this comment.
@eric-wieser But you are right, the error message is not correct. Should we raise another error?
There was a problem hiding this comment.
I think on this path, you should use:
if axis != 0:
x = np.swapaxes(x, ...)
That way, if swapaxes is supported on a duck-array, it will just work. If it isn't supported, then the duckarray will be able to report itself that its not supported.
By adding the !=0 check, you ensure that duckarrays don't need to implement swapaxes to support axis=0.
There was a problem hiding this comment.
@eric-wieser I see. If swapaxes works, we need to add another swapaxes after the blockwith self.lock ... ?
|
This should be ENH not MAINT, and needs a release note. |
53398fd to
887119b
Compare
887119b to
247282b
Compare
247282b to
c99a166
Compare
|
Thanks @kianasun and reviewers |
|
FYI: There's a small bug in the new code. I proposed a fix in #14662. |
The
random.permutationfunction now can only shuffle the first axis of a multi-dimensional array. I add an argument for the function and allow it to shuffle along a given axis.