Python 洗牌结构化数组(记录数组)

Python 洗牌结构化数组(记录数组),python,arrays,python-2.7,numpy,structured-array,Python,Arrays,Python 2.7,Numpy,Structured Array,如何洗牌结构化数组numpy.random.shuffle似乎不起作用。此外,在下面的示例中,可以仅洗牌给定字段,例如x 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,

如何洗牌结构化数组
numpy.random.shuffle
似乎不起作用。此外,在下面的示例中,可以仅洗牌给定字段,例如
x

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')])

但我想要一个原地洗牌

numpy.random.shuffle
似乎支持多维数组。 看见文档上的示例表明可以将多维数组作为参数传递

所以我不知道为什么你的代码不起作用

但还有另一种方法可以做到这一点。比如:

哎呀

import random
shuffledIndex = random.sample(xrange(len(data1)), len(data1))
shuffledData = data1[shuffledIndex]

这是由于一个numpy错误 在
Numpy 1.8.1
中,此问题已得到解决。现在它的工作如预期

np.random.shuffle(data1)
data1
>>> array([(1.0, 2.0), (13.0, 77.0), (11.0, 30.0), (5.0, 10.0), (3.0, 4.1)], 
      dtype=[('x', '<f8'), ('y', '<f8')])
np.random.shuffle(数据1)
数据1
>>>数组([(1.0,2.0),(13.0,77.0),(11.0,30.0),(5.0,10.0),(3.0,4.1)],

dtype=[('x','谢谢。我也不明白为什么它不起作用。我知道我可以显式地给出随机索引。
data1
是一个一维数组(结构化)数组,所以我认为多维支持不相关。可能是这个numpy错误:@Warren,你是对的。在numpy 1.8.1中修复。
import random
shuffledIndex = random.sample(xrange(len(data1)), len(data1))
shuffledData = data1[shuffledIndex]
np.random.shuffle(data1)
data1
>>> array([(1.0, 2.0), (13.0, 77.0), (11.0, 30.0), (5.0, 10.0), (3.0, 4.1)], 
      dtype=[('x', '<f8'), ('y', '<f8')])