I have a numpy array, and I would like to shuffle parts of it. For example, with the following array:
import numpy as np
import random
a = np.arange(15)
# => array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
I want to do:
shuffle_parts(a, [(0, 3), (10, 13)])
# => array([ 2, 0, 1, 3, 4, 5, 6, 7, 8, 9, 12, 11, 10, 13, 14])
# ^^^^^^^^^ ^^^^^^^^^^
# Shuffle those 3 values and those 3 values
The following would shuffle all the array: (Not what I want)
random.shuffle(a)
# => array([10, 11, 8, 1, 13, 5, 9, 14, 4, 7, 2, 12, 3, 0, 6])
One way would be to use split / concatenate like so:
splits = np.split(a, 5)
random.shuffle(splits[0])
random.shuffle(splits[3])
np.concatenate(splits)
# => array([ 2, 0, 1, 3, 4, 5, 6, 7, 8, 11, 10, 9, 12, 13, 14])
# ^^^^^^^^^ ^^^^^^^^^^
# Correctly shuffled Shuffled but off by 1 index
This is almost what I want. My questions:
- Can I write
shuffle_partswhere the indices are custom (parts with arbitrary indices, not restricted to modulos, and parts with varying length) - Is there a method in numpy that I missed and that would help me do that?
np.random.shuffle(a[0:3])