Code review for adding axis argument to permutation and shuffle function
![](https://secure.gravatar.com/avatar/cca31993c68f22463b4bf7b5515c68cb.jpg?s=120&d=mm&r=g)
Hi, I would like to request a code review. The random.permutation and random.shuffle functions now can only shuffle along the first axis of a multi-dimensional array. I propose to add an axis argument for the functions and allow them to shuffle along a given axis. Here is the link to the PR (https://github.com/numpy/numpy/pull/13829). Thanks!
![](https://secure.gravatar.com/avatar/72f994ca072df3a3d2c3db8a137790fd.jpg?s=120&d=mm&r=g)
This proposal to add an axis argument to permutation and shuffle seems to have garnered no reply. Are people OK with it (for the new random.Generator only) ? -- Sent from: http://numpy-discussion.10968.n7.nabble.com/
![](https://secure.gravatar.com/avatar/d2aafb97833979e3668c61d36e697bfc.jpg?s=120&d=mm&r=g)
On 7/4/19, Kexuan Sun <me@kianasun.com> wrote:
Given the current semantics of 'shuffle', the proposed change makes sense. However, I would like to call attention to https://github.com/numpy/numpy/issues/5173 and to the mailing list thread from 2014 that I started here: https://mail.python.org/pipermail/numpy-discussion/2014-October/071340.html The topic of those discussions was that the current behavior of 'shuffle' is often *not* what users want or expect. What is often desired is to shuffle each row (or column, or whatever dimension is specified) *independently* of the others. So if a = np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]), then randomly shuffling 'a' along axis=1 should shuffle each row independently of the others, to create something like a = np.array([[2, 4, 0, 3, 1], [8, 6, 9, 7, 5], [11, 12, 10, 14, 13]]) An API for this was discussed (and of course that ran into the second of the two hard problems in computer science, naming things). Take a look at those discussions, and check that https://github.com/numpy/numpy/pull/13829 fits in with the possible changes mentioned in those discussions. If we don't use the name 'shuffle' for the new random permutation function(s), then the change in PR 13829 is a good one. However, if we want to try to reuse the name 'shuffle' to also allow independent shuffling along an axis, then we have to be careful with how we interpret the 'axis' argument. Warren
![](https://secure.gravatar.com/avatar/72f994ca072df3a3d2c3db8a137790fd.jpg?s=120&d=mm&r=g)
This proposal to add an axis argument to permutation and shuffle seems to have garnered no reply. Are people OK with it (for the new random.Generator only) ? -- Sent from: http://numpy-discussion.10968.n7.nabble.com/
![](https://secure.gravatar.com/avatar/d2aafb97833979e3668c61d36e697bfc.jpg?s=120&d=mm&r=g)
On 7/4/19, Kexuan Sun <me@kianasun.com> wrote:
Given the current semantics of 'shuffle', the proposed change makes sense. However, I would like to call attention to https://github.com/numpy/numpy/issues/5173 and to the mailing list thread from 2014 that I started here: https://mail.python.org/pipermail/numpy-discussion/2014-October/071340.html The topic of those discussions was that the current behavior of 'shuffle' is often *not* what users want or expect. What is often desired is to shuffle each row (or column, or whatever dimension is specified) *independently* of the others. So if a = np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]), then randomly shuffling 'a' along axis=1 should shuffle each row independently of the others, to create something like a = np.array([[2, 4, 0, 3, 1], [8, 6, 9, 7, 5], [11, 12, 10, 14, 13]]) An API for this was discussed (and of course that ran into the second of the two hard problems in computer science, naming things). Take a look at those discussions, and check that https://github.com/numpy/numpy/pull/13829 fits in with the possible changes mentioned in those discussions. If we don't use the name 'shuffle' for the new random permutation function(s), then the change in PR 13829 is a good one. However, if we want to try to reuse the name 'shuffle' to also allow independent shuffling along an axis, then we have to be careful with how we interpret the 'axis' argument. Warren
participants (5)
-
Juan Nunez-Iglesias
-
Kexuan Sun
-
Matthew Brett
-
mattip
-
Warren Weckesser