【发布时间】:2021-05-20 21:53:02
【问题描述】:
考虑以下 MWE:
import numpy as np
n=2
N = 6
a = np.random.randint(0,10,size=(N,1))
b = np.random.randint(0,10,size=(N,n))
c = np.random.randint(0,10,size=(n,N,5))
c 在哪里,例如(随机召回):
array([[[7 5 1 7 0]
[2 8 2 1 4]
[0 4 1 7 3]
[1 6 6 9 6]
[9 6 0 0 2]
[9 6 0 6 7]]
[[0 3 9 0 3]
[4 7 5 3 8]
[8 0 6 7 9]
[5 4 9 5 2]
[5 6 6 8 7]
[7 7 2 6 0]]])
形状为(2,6,5)。
我们从中制造:
out = a+b
>>>out
array([[ 9, 7],
[ 5, 7],
[ 7, 3],
[ 9, 9],
[15, 10],
[ 8, 9]])
形状为(6,2)。
现在这是我想要做的:我想将out 的第一列添加到c 的第一个矩阵(即矩阵由c 的第一个维度索引的位置),第二列的out 到c 的第二列等等(你得到了漂移)。目前,我正在尝试使用广播来做到这一点,但我似乎对自己感到困惑。
我不想使用循环,因为我的真正问题非常大。
期望的输出:
>>>np.stack([out[:,i][:,np.newaxis] + c[i] for i in range(2)])
array([[[16, 14, 10, 16, 9],
[ 7, 13, 7, 6, 9],
[ 7, 11, 8, 14, 10],
[10, 15, 15, 18, 15],
[24, 21, 15, 15, 17],
[17, 14, 8, 14, 15]],
[[ 7, 10, 16, 7, 10],
[11, 14, 12, 10, 15],
[11, 3, 9, 10, 12],
[14, 13, 18, 14, 11],
[15, 16, 16, 18, 17],
[16, 16, 11, 15, 9]]])
形状为(2,6,5)。
尝试:
out[None, :,:] + c
这会导致以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-702-d54cfe51ec32> in <module>
----> 1 out[None, :,:] + c
ValueError: operands could not be broadcast together with shapes (1,6,2) (2,6,5)
我们将不胜感激。
【问题讨论】:
标签: python numpy linear-algebra broadcasting