How can I shuffle structured array. numpy.random.shuffle
does not seem to work. Further is it possible to shuffle only a given field say x
in the following example.
import numpy as np
data = [(1, 2), (3, 4.1), (13, 77), (5, 10), (11, 30)]
dtype = [('x', float), ('y', float)]
data1=np.array(data, dtype=dtype)
data1
>>> array([(1.0, 2.0), (3.0, 4.1), (13.0, 77.0), (5.0, 10.0), (11.0, 30.0)],
dtype=[('x', '<f8'), ('y', '<f8')])
np.random.seed(10)
np.random.shuffle(data)
data
>>> [(13, 77), (5, 10), (1, 2), (11, 30), (3, 4.1)]
np.random.shuffle(data1)
data1
>>> array([(1.0, 2.0), (3.0, 4.1), (1.0, 2.0), (3.0, 4.1), (1.0, 2.0)],
dtype=[('x', '<f8'), ('y', '<f8')])
I understand that I can explicitly give the randomized index,
data1[np.random.permutation(data1.shape[0])]
but I want a in place shuffling.