python - Shuffling NumPy array along a given axis -
given following numpy array,
> = array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5],[1, 2, 3, 4, 5]])
it's simple enough shuffle single row,
> shuffle(a[0]) > array([[4, 2, 1, 3, 5],[1, 2, 3, 4, 5],[1, 2, 3, 4, 5]])
is possible use indexing notation shuffle each of rows independently? or have iterate on array. had in mind like,
> numpy.shuffle(a[:]) > array([[4, 2, 3, 5, 1],[3, 1, 4, 5, 2],[4, 2, 1, 3, 5]]) # not real output
though doesn't work.
you have call numpy.random.shuffle()
several times because shuffling several sequences independently. numpy.random.shuffle()
works on mutable sequence , not ufunc
. shortest , efficient code shuffle rows of two-dimensional array a
separately is
map(numpy.random.shuffle, a)
Comments
Post a Comment