如何在后者的第二个维度上将 2-d numpy 数组按元素乘积为 3-d?
How to product element-wise a 2-d numpy array into 3-d over the second dimension of the latter?
我有一个 numpy 矩阵 b = np.array([[1,0,1,0],[0,0,0,1]])
,我想将其按元素乘积到一个 3 维数组 a = np.array([[[1,2,3,4], [5,6,7,8], [9,10,11,12]], [[13,14,15,16], [17,18,19,20], [21,22,23,24]]])
,用于第二维上的每个索引。所以,我期望的结果应该是这样的:
[[[1,0,3,0], [5,0,7,0], [9,0,11,0]], [[0,0,0,16], [0,0,0,20], [0,0,0,24]]]
如果我这样做,Numpy 不会广播 a * b
。我正在考虑在其第二个维度中广播 b。我试过 np.broadcast_to(b, (2,3,4))
但我得到了错误。我尝试了 (np.broadcast_to(b, (3,2,4)).reshape(2,3,4))
但输出与预期不符。
你需要整形:
c = b.reshape(2,-1,4)*a
使用None/newaxis
添加一个新的中间维度(reshape
也这样做):
In [36]: b.shape
Out[36]: (2, 4)
In [37]: a.shape
Out[37]: (2, 3, 4)
In [38]: b[:,None,:]*a
Out[38]:
array([[[ 1, 0, 3, 0],
[ 5, 0, 7, 0],
[ 9, 0, 11, 0]],
[[ 0, 0, 0, 16],
[ 0, 0, 0, 20],
[ 0, 0, 0, 24]]])
In [39]: b[:,None,:].shape
Out[39]: (2, 1, 4)
broadcast_to
无法自动添加该额外维度。它遵循与 b*a
操作相同的规则。如果需要,它可以添加前导尺寸,并缩放尺寸 1 尺寸。但对于其他任何事情,您都必须明确。
In [41]: np.broadcast_to(b, (2,3,4))
Traceback (most recent call last):
File "<ipython-input-41-3c3268de7ce1>", line 1, in <module>
np.broadcast_to(b, (2,3,4))
File "<__array_function__ internals>", line 5, in broadcast_to
File "/usr/local/lib/python3.8/dist-packages/numpy/lib/stride_tricks.py", line 411, in broadcast_to
return _broadcast_to(array, shape, subok=subok, readonly=True)
File "/usr/local/lib/python3.8/dist-packages/numpy/lib/stride_tricks.py", line 348, in _broadcast_to
it = np.nditer(
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,4) and requested shape (2,3,4)
In [42]: np.broadcast_to(b[:,None,:], (2,3,4))
Out[42]:
array([[[1, 0, 1, 0],
[1, 0, 1, 0],
[1, 0, 1, 0]],
[[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1]]])
我有一个 numpy 矩阵 b = np.array([[1,0,1,0],[0,0,0,1]])
,我想将其按元素乘积到一个 3 维数组 a = np.array([[[1,2,3,4], [5,6,7,8], [9,10,11,12]], [[13,14,15,16], [17,18,19,20], [21,22,23,24]]])
,用于第二维上的每个索引。所以,我期望的结果应该是这样的:
[[[1,0,3,0], [5,0,7,0], [9,0,11,0]], [[0,0,0,16], [0,0,0,20], [0,0,0,24]]]
如果我这样做,Numpy 不会广播 a * b
。我正在考虑在其第二个维度中广播 b。我试过 np.broadcast_to(b, (2,3,4))
但我得到了错误。我尝试了 (np.broadcast_to(b, (3,2,4)).reshape(2,3,4))
但输出与预期不符。
你需要整形:
c = b.reshape(2,-1,4)*a
使用None/newaxis
添加一个新的中间维度(reshape
也这样做):
In [36]: b.shape
Out[36]: (2, 4)
In [37]: a.shape
Out[37]: (2, 3, 4)
In [38]: b[:,None,:]*a
Out[38]:
array([[[ 1, 0, 3, 0],
[ 5, 0, 7, 0],
[ 9, 0, 11, 0]],
[[ 0, 0, 0, 16],
[ 0, 0, 0, 20],
[ 0, 0, 0, 24]]])
In [39]: b[:,None,:].shape
Out[39]: (2, 1, 4)
broadcast_to
无法自动添加该额外维度。它遵循与 b*a
操作相同的规则。如果需要,它可以添加前导尺寸,并缩放尺寸 1 尺寸。但对于其他任何事情,您都必须明确。
In [41]: np.broadcast_to(b, (2,3,4))
Traceback (most recent call last):
File "<ipython-input-41-3c3268de7ce1>", line 1, in <module>
np.broadcast_to(b, (2,3,4))
File "<__array_function__ internals>", line 5, in broadcast_to
File "/usr/local/lib/python3.8/dist-packages/numpy/lib/stride_tricks.py", line 411, in broadcast_to
return _broadcast_to(array, shape, subok=subok, readonly=True)
File "/usr/local/lib/python3.8/dist-packages/numpy/lib/stride_tricks.py", line 348, in _broadcast_to
it = np.nditer(
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,4) and requested shape (2,3,4)
In [42]: np.broadcast_to(b[:,None,:], (2,3,4))
Out[42]:
array([[[1, 0, 1, 0],
[1, 0, 1, 0],
[1, 0, 1, 0]],
[[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1]]])