Python 在对象数组中存储记录数组

Python 在对象数组中存储记录数组,python,numpy,Python,Numpy,我想将记录数组的列表——dtype is(uint32,float32)——转换为dtypenp.object的numpy数组: X = np.array(instances, dtype = np.object) X[0] array([(67111L, 1.0), (104242L, 1.0)], dtype=object) 其中,instances是数据类型为np.dtype([('f0','p>Stéfan van der Walt,numpy开发者)的数组列表: ndarray构造

我想将记录数组的列表——dtype is(uint32,float32)——转换为dtype
np.object的numpy数组:

X = np.array(instances, dtype = np.object)
X[0]
array([(67111L, 1.0), (104242L, 1.0)], dtype=object)
其中,
instances
是数据类型为
np.dtype([('f0','p>Stéfan van der Walt,numpy开发者)的数组列表:

ndarray构造函数尽了最大努力 猜猜你是什么样的数据 喂它,但有时它需要 一点帮助

我更喜欢构造数组 明确地说,所以毫无疑问是什么 发生在引擎盖下的事件:

当你说

instance1=np.array([(67111L,1.0),(104242L,1.0)],dtype=np.dtype([('f0', '<u4'), ('f1', '<f4')]))
instance2=np.array([(67112L,2.0),(104243L,2.0)],dtype=np.dtype([('f0', '<u4'), ('f1', '<f4')]))
instances=[instance1,instance2]
Y=np.array(instances, dtype = np.object)
在大多数情况下,我认为这是人们想要的。但是, 在您的情况下,由于这不是您想要的,因此必须显式构造数组:

X=np.empty((len(instances),), dtype = np.object)
print(X.shape)
# (2,)
现在关于X的形状没有问题了:
(2,)
,所以当你输入数据时

X[:] = instances
numpy足够聪明,可以将
实例
看作是两个对象的序列

X=np.empty((len(instances),), dtype = np.object)
print(X.shape)
# (2,)
X[:] = instances