在 numpy 中,一个形状与另一个形状 "broadcastible" 是什么意思?

What does it mean for a shape to be "broadcastible" to another in numpy?

我正在尝试在另一个库中复制 numpy 行为。我的图书馆缺少的一件事是广播行为。运算符的广播行为在 numpy 中使用非赋值运算符定义得很好,documentation stating:

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when

  1. they are equal, or
  2. one of them is 1

a * b 很容易,但是 a *= b 或 a[:] = b 呢? numpy 中的分配似乎遵循 some 广播规则,即:

a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a = a.reshape(3,1,1,3)
b = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = b.reshape(1,1,3,3)

c = a * b # works!
a *= b # fails, non-broadcastable output operand with shape (1,1,3,3) doesn't match the broadcast shape (3,1,1,3)
a[:] = b #fails, could not broadcast input array from shape (1,1,3,3) into to shape (3,1,1,3)
b[:,:,:] = a[1,0,0,:] # works!

现在,我可以直观地理解一些规则,为什么对其中一些作业没有意义。问题是我看不到整个分配广播的规则集。我假设它与输出位置形状匹配有关,尽管即使最后一个工作示例的逻辑失败(他们没有)。我能找到的最接近的东西是一个 statement in the docs (强调我的):

As mentioned, one can select a subset of an array to assign to using a single index, slices, and index and mask arrays. The value being assigned to the indexed array must be shape consistent (the same shape or broadcastable to the shape the index produces). For example, it is permitted to assign a constant to a slice:

这里"broadcastible to the shape"的定义是什么?

(1,1,3,3) into to shape (3,1,1,3)

b 形状为 (1,1,3,3) 可以广播到 (3,1,3,3) 但不能广播到 (3,1,1,3)。第 3 个 dim 是 3,不能更改为 1

a*=a[:]=时,a的形状没有改变。

a[1,0,0,:] 是 (1,1,3) 可以广播到 (1,1,1,3) 和 (1,1,3,3)。

c = a*b,广播是:

(3,1,1,3) * (1,1,3,3) => (3,1,3,3)

a 的一个 dim 增加到 3,b 的另一个 dim 也增加到 3。使用二元运算符 * 两个参数都被广播。在分配中,只有右手是。