[英]Reshaping numpy array from list
我对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]和reference [0] [0]访问两个单个元素?
我不确定您到底想做什么,但是这里有一些有关numpy数组的信息。
一维numpy数组是形状为单值元组的行向量:
>>> np.array([1,2,3]).shape
(3,)
您可以通过传递嵌套列表来创建多维数组。 每个子列表是长度为1的一维行向量,其中有3个。
>>> np.array([[1],[2],[3]]).shape
(3,1)
这是奇怪的部分。 您可以创建相同的数组,但将列表留空。 您最终得到3个长度为0的行向量。
>>> np.array([[],[],[]]).shape
(3,0)
这就是您要reference
数组,它是具有结构但没有值的数组。 这使我回到原来的观点:
您不能减去一个空数组。
如果我用您描述的形状制作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)
是错误的。 我不知道您是否输错了什么。
但是,如果我在最后一个维度中用2 reference
2d,则广播工作正常,产生的差异与形状匹配(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]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.