【发布时间】:2014-02-14 00:53:31
【问题描述】:
MWE:
def showArrayOfList(a,b,c):
wlist = [np.zeros((szNext,szThis)) for (szThis,szNext) in [(a,b),(b,b),(b,b),(b,c)]]
print "wlist:", map(np.shape,wlist)
wArray = np.asarray(wlist)
print "wArray:", map(np.shape,wArray)
print "shape wArray:", shape(wArray)
np.zeros 可以替换任何其他返回给定形状的矩阵的矩阵函数
以下输出是我所期望(并得到)的:
In[1]: ShowArrayOfList(1,4,5)
Out[1]: wlist: [(4, 1), (4, 4), (4, 4), (5, 4)]
wArray: [(4, 1), (4, 4), (4, 4), (5, 4)]
shape wArray: (4,) #An array of 4 references(?), to arrays of various sizes
In[2]: ShowArrayOfList(5,5,5)
Out[2]: wlist: [(5, 5), (5, 5), (5, 5), (5, 5)]
wArray: [(5, 5), (5, 5), (5, 5), (5, 5)]
shape wArray: (4, 5, 5) #4 arrays of shape (5,5)
但对于a!=b 和b==c 形式的输入,情况完全不同
Int[3]: showArrayOfList(6,5,5)
Out[3]: wlist: [(5, 6), (5, 5), (5, 5), (5, 5)]
wArray: [(5,), (5,), (5,), (5,)] #Where did my second Dimension Go?
shape wArray: (4, 5)
Int[4]: showArrayOfList(2,4,4)
Out[4]:
wlist: [(4, 2), (4, 4), (4, 4), (4, 4)]
wArray: [(4,), (4,), (4,), (4,)] #Where did my second Dimension Go?
shape wArray: (4, 4)
这导致我很难找到错误, 稍微想了想,我觉得这和广播系统有关。
我想知道发生了什么,解释一下。 (我脑子里有一个模糊的概念)
作为参考,我制作数组数组的原因是为了减法:wArray=wArray-dWs 比 wList=[w-dW, (w,dW) in zip(wList,dWs)] 更易读
【问题讨论】:
-
在版本
1.9.0dev中,最后两种情况下的wArray是None的数组。如果我以交互方式尝试它,我会得到ValueError: could not broadcast input array from shape (5,5) into shape (5)。无论这是否是我的版本中的错误,这都不是构造数组的可靠方法。我认为wArray应该始终具有(4,)的形状,而不管a,b,c。有时不是 2d 或 3d。
标签: python arrays numpy multidimensional-array jagged-arrays