python存储对象的数组_在对象数组中存储记录数组

Stéfan van der Walt(numpy开发者)explains:The ndarray constructor does its best

to guess what kind of data you are

feeding it, but sometimes it needs a

bit of help....

I prefer to construct arrays

explicitly, so there is no doubt what

is happening under the hood:

当你说像instance1=np.array([(67111L,1.0),(104242L,1.0)],dtype=np.dtype([('f0', '

instance2=np.array([(67112L,2.0),(104243L,2.0)],dtype=np.dtype([('f0', '

instances=[instance1,instance2]

Y=np.array(instances, dtype = np.object)

np.array被迫猜测所需数组的维数。

instances是两个对象的列表,每个对象的长度为2。因此,相当合理地,np.array猜测{}应该有形状(2,2):

^{pr2}$

在大多数情况下,我认为这是我们想要的。然而,

在您的情况下,由于这不是您想要的,因此必须显式地构造数组:X=np.empty((len(instances),), dtype = np.object)

print(X.shape)

# (2,)

现在对X的形状没有疑问:(2, )所以当你输入数据时X[:] = instances

numpy足够聪明,可以将instances看作两个对象的序列。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值