What's the easiest way to shuffle an array with python?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I don't know I used
random.shuffle()
but it return 'None' to me, so I wrote this, might helpful to someoneAlternative way to do this using sklearn
Output:
Advantage: You can random multiple arrays simultaneously without disrupting the mapping. And 'random_state' can control the shuffling for reproducible behavior.
When dealing with regular Python lists,
random.shuffle()
will do the job just as the previous answers show.But when it come to
ndarray
(numpy.array
),random.shuffle
seems to break the originalndarray
. Here is an example:Just use:
np.random.shuffle(a)
Like
random.shuffle
,np.random.shuffle
shuffles the array in-place.