[Numpy-discussion] Code review for adding axis argument to permutation and shuffle function

Warren Weckesser warren.weckesser at gmail.com
Fri Sep 13 16:18:06 EDT 2019

On 7/4/19, Kexuan Sun <me at kianasun.com> wrote:
> 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).

Given the current semantics of 'shuffle', the proposed change makes
sense.  However, I would like to call attention to


and to the mailing list thread from 2014 that I started here:


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


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.


> Thanks!
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list