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看作两个对象的序列。在