从列表中重塑numpy数组

时间:2016-08-15 00:41:34

标签: python arrays numpy reshape

我对ndarray的形状有以下问题:

out.shape = (20,)
reference.shape = (20,0)
norm = [out[i] / np.sum(out[i]) for i in range(len(out))]
# norm is a list now so I convert it to ndarray:
norm_array = np.array((norm))
norm_array.shape = (20,30)

# error: operands could not be broadcast together with shapes (20,30) (20,) 
diff = np.fabs(norm_array - reference)

如何将norm_array的形状从(20,30)更改为(20,)或引用(20,30),以便我可以减去它们?

编辑:有人可以解释我,为什么他们有不同的形状,如果我可以用norm_array [0] [0]和参考[0] [0]访问这两个元素?

2 个答案:

答案 0 :(得分:2)

我不确定你要做什么,但这里有关于numpy数组的一些信息。

1-d numpy数组是一个行向量,其形状是单值元组:

$stateChangeStart

您可以通过传入嵌套列表来创建多维数组。每个子列表是长度为1的1-d行向量,其中有3个。

>>> np.array([1,2,3]).shape
(3,) 

这是奇怪的部分。您可以创建相同的数组,但将列表保留为空。最终得到3个长度为0的行向量。

>>> np.array([[1],[2],[3]]).shape
(3,1)

这就是你所拥有的>>> np.array([[],[],[]]).shape (3,0) 数组,一个具有结构但没有值的数组。这让我回到原来的观点:

你不能减去一个空数组。

答案 1 :(得分:0)

如果我用你描述的形状制作2个数组,我会收到错误

In [1856]: norm_array=np.ones((20,30))
In [1857]: reference=np.ones((20,0))
In [1858]: norm_array-reference
...
ValueError: operands could not be broadcast together with shapes (20,30) (20,0) 

但它与你的不同。但是,如果我更改reference的形状,则错误消息会匹配。

In [1859]: reference=np.ones((20,))
In [1860]: norm_array-reference
 ...
ValueError: operands could not be broadcast together with shapes (20,30) (20,) 

所以你的(20,0)错了。我不知道你输错了什么。

但如果我在最后一个维度中将reference 2d设为1,则广播有效,产生与形状匹配(20,30)的差异:

In [1861]: reference=np.ones((20,1))
In [1862]: norm_array-reference

如果reference = np.zeros((20,)),那么我可以使用reference[:,None]添加该单例最后一维。

如果reference是(20,),则无法执行reference[0][0]reference[0][0]仅适用于最后一个昏暗中至少有1个的2d数组。 reference[0,0]是索引2d数组的单个元素的首选方法。

到目前为止,这是正常的阵列尺寸和广播;你将学习使用的东西。

===============

我对out的形状感到困惑。如果是(20,),norm_array如何最终为(20,30)。 out必须包含20个数组或列表,每个数组或列表包含30个元素。

如果out是2d数组,我们可以在没有迭代的情况下进行标准化

In [1869]: out=np.arange(12).reshape(3,4)

列表理解:

In [1872]: [out[i]/np.sum(out[i]) for i in range(out.shape[0])]
Out[1872]: 
[array([ 0.        ,  0.16666667,  0.33333333,  0.5       ]),
 array([ 0.18181818,  0.22727273,  0.27272727,  0.31818182]),
 array([ 0.21052632,  0.23684211,  0.26315789,  0.28947368])]
In [1873]: np.array(_)   # and to array
Out[1873]: 
array([[ 0.        ,  0.16666667,  0.33333333,  0.5       ],
       [ 0.18181818,  0.22727273,  0.27272727,  0.31818182],
       [ 0.21052632,  0.23684211,  0.26315789,  0.28947368]])

取而代之的是行和,并告诉它保留2d以便于进一步使用

In [1876]: out.sum(axis=1,keepdims=True)
Out[1876]: 
array([[ 6],
       [22],
       [38]])

现在划分

In [1877]: out/out.sum(axis=1,keepdims=True)
Out[1877]: 
array([[ 0.        ,  0.16666667,  0.33333333,  0.5       ],
       [ 0.18181818,  0.22727273,  0.27272727,  0.31818182],
       [ 0.21052632,  0.23684211,  0.26315789,  0.28947368]])