I have a pretty straightforward question that I couldn't figure out quickly from the numpy reference documentation.
Say I have a numpy array labels = np.array([1, 2, 3]).
I have another array arr = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3]).
I'd like to randomly sample indices of arr. Say our indices are [0, 3, 6].
Now, I'd like to make the elements corresponding to those indices cycle by one in labels. So, since arr[0] == 1, we would set arr[0] = 2. Since arr[3] == 2, we would set arr[3] = 3. Since arr[6] == 3, we would set arr[6] = 1.
so, to recap:
arr = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3])
labels = np.unique(arr)
idx = np.array([0, 3, 6]) # randomly generate indices [0, 3, 6]
new_arr == np.array(2, 1, 1, 3, 2, 2, 1, 3, 3]) # this is the array I want
This seems like it'd be pretty straightforward, but I can't find an elegant way of doing this quickly!