[英]Confusion regarding np.tile and numpy broadcasting
假设我有一个形状为(m, n)
的2D numpy
数组A
我想创建一个形状(m, n, k)
的3D数组B
,使得B[:, :, l]
是任何切片l
的A
的副本。 我可以想到两种方法:
np.tile(A, (m, n, k))
要么
np.repeat(A[:, :, np.newaxis], k, axis=-1)
第一种方法似乎更简单,但是我提到了np.tile
的文档:
Note: Although tile may be used for broadcasting, it is strongly
recommended to use numpy's broadcasting operations and functions.
为什么会这样,这也是np.repeat
的问题吗?
我的另一个担心是,如果m == n == k
,那么np.tile()
引起关于哪个轴增加的混乱吗?
总而言之,我有两个问题:
np.tile
,并且在某些情况下m == n == k
会导致意外行为? 您说要扩展一个shape- (m, n)
数组和一个shape- (n, k)
数组以同时成形(m, n, k)
并将它们加在一起。 在这种情况下,您根本不需要物理扩展阵列; 对齐轴并进行广播将正常工作:
A = something of shape (m, n)
B = something of shape (n, k)
C = A[..., np.newaxis] + B
这不需要复制A
和B
中的数据,并且运行速度要比涉及物理副本的任何东西都要快得多。
In [100]: A = np.arange(12).reshape(3,4)
使用重复在末尾添加新尺寸:
In [101]: B = np.repeat(A[:,:,np.newaxis], 2, axis=-1)
In [102]: B.shape
Out[102]: (3, 4, 2)
使用平铺并重复以在开始时添加新尺寸:
In [104]: np.tile(A, (2,1,1)).shape
Out[104]: (2, 3, 4)
In [105]: np.repeat(A[None,:,:], 2, axis=0).shape
Out[105]: (2, 3, 4)
如果我们在图块的最后一个维度上指定2个重复,则其形状会有所不同
In [106]: np.tile(A, (1,1,2)).shape
Out[106]: (1, 3, 8)
请注意,关于重复元组在尺寸前添加的tile
表示的内容大于形状。
但是,如果您按照注释中的描述在计算中使用了扩展数组,则无需进行完整的重复复制。 可以利用broadcasting
来使用正确形状的临时视图。
In [107]: A1=np.arange(12).reshape(3,4)
In [108]: A2=np.arange(8).reshape(4,2)
In [109]: A3=A1[:,:,None] + A2[None,:,:]
In [110]: A3.shape
Out[110]: (3, 4, 2)
In [111]: A3
Out[111]:
array([[[ 0, 1],
[ 3, 4],
[ 6, 7],
[ 9, 10]],
[[ 4, 5],
[ 7, 8],
[10, 11],
[13, 14]],
[[ 8, 9],
[11, 12],
[14, 15],
[17, 18]]])
使用None
( np.newaxis
),数组视图的形状为(3,4,1)和(1,4,2),它们一起广播为(3,4,2)。 在第二种情况下,我可以省略“ None
”,因为广播会自动添加。 但是尾随的None
是必需的。
In [112]: (A1[:,:,None] + A2).shape
Out[112]: (3, 4, 2)
要添加一维数组(最后一维):
In [113]: (A1[:,:,None] + np.array([1,2])[None,None,:]).shape
Out[113]: (3, 4, 2)
In [114]: (A1[:,:,None] + np.array([1,2])).shape
Out[114]: (3, 4, 2)
两个基本广播步骤:
[None,....]
) 这组计算说明了这一点:
In [117]: np.ones(2) + np.ones(3)
ValueError: operands could not be broadcast together with shapes (2,) (3,)
In [118]: np.ones(2) + np.ones((1,3))
ValueError: operands could not be broadcast together with shapes (2,) (1,3)
In [119]: np.ones(2) + np.ones((3,1))
Out[119]:
array([[2., 2.],
[2., 2.],
[2., 2.]])
In [120]: np.ones((1,2)) + np.ones((3,1))
Out[120]:
array([[2., 2.],
[2., 2.],
[2., 2.]])
缺少中间尺寸
In [126]: np.repeat(A[:,None,:],2,axis=1)+np.ones(4)
Out[126]:
array([[[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.]],
[[ 5., 6., 7., 8.],
[ 5., 6., 7., 8.]],
[[ 9., 10., 11., 12.],
[ 9., 10., 11., 12.]]])
还有一个更“高级”的选择(但不一定更快):
In [127]: np.broadcast_to(A[:,None,:],(3,2,4))+np.ones(4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.