在两个不同形状的数组上广播 (Numpy-Python)

Broadcasting over two arrays with different shapes (Numpy-Python)

假设我有以下数组:

first_array = array([[1, 8, 3, 9, 2],
                     [2, 6, 4, 1, 9],
                     [4, 2, 12, 8, 16],
                     [5, 3, 7, 18, 21],
                     [6, 20, 4, 8, 24]])

所以数组的形状为 (5, 5)

现在我有了第二个数组,它是第一个数组的一部分:

second_array = array([[1, 8, 3, 9, 2],
                     [2, 6, 4, 1, 9]])

形状为 (2, 5) 的数组。

现在我想用第二个数组的向量减去第一个数组的每个向量(不包括-在第一个数组中-我用来减去的第二个数组的向量),逐个元素。我想对第二个数组的每个向量都这样。

所以我想输出:

subtracted_array = array([[[1, -2, 1, -8, 7],
                           [3, -6, 9, -1, 14],
                           [4, -5, 4, 9, 19],
                           [5, 12, 1, -1, 22]],
                          [[-1, 2, -1, 8, -7],
                           [2, -4, 8, 7, 7],
                           [3, -3, 3, 17, 12],
                           [4, 14, 0, 7, 15]]])

所以这是一个形状为 (2, 4, 5)

的数组

我们如何使用广播来做到这一点?

此代码是否按照您的意思执行?
欢迎您在您的测试用例上对其进行测试,如果您需要更多帮助,请更新我。

import numpy as np
arr = np.arange(50).reshape(10, 5)
arr_slice = arr[:2, :]

# "outer" tensor subtraction
arr_sub = arr_slice[:, None, :] - arr[None, :, :]
# create an index map of the vector combinations
idx0, idx1 = np.mgrid[:2, :10]
idx0, idx1 = idx0.flatten(), idx1.flatten()
# find the irrelevent combinations and remove them
remove_same = (1 - (idx0 == idx1)).astype(bool)
idx0, idx1 = idx0[remove_same], idx1[remove_same]

arr_sub = arr_sub[idx0, idx1, :].reshape(2, 9, 5)

编辑: 这是一个更有效的方法:

import numpy as np
arr = np.arange(50).reshape(10, 5)
arr_slice = arr[:2, :]

# create an index map of the vector combinations
idx0, idx1 = np.mgrid[:2, :10]
idx0, idx1 = idx0.flatten(), idx1.flatten()
# find the irrelevent combinations and remove them
remove_same = (1 - (idx0 == idx1)).astype(bool)
idx0, idx1 = idx0[remove_same], idx1[remove_same]

arr_sub = arr_slice[idx0, :] - arr[idx1, :]

arr_sub = arr_sub.reshape(2, 9, 5)

输出:

arr = 
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]
 [25 26 27 28 29]
 [30 31 32 33 34]
 [35 36 37 38 39]
 [40 41 42 43 44]
 [45 46 47 48 49]]

arr_slice = 
[[0 1 2 3 4]
 [5 6 7 8 9]]

arr_sub = 
[[[ -5  -5  -5  -5  -5]
  [-10 -10 -10 -10 -10]
  [-15 -15 -15 -15 -15]
  [-20 -20 -20 -20 -20]
  [-25 -25 -25 -25 -25]
  [-30 -30 -30 -30 -30]
  [-35 -35 -35 -35 -35]
  [-40 -40 -40 -40 -40]
  [-45 -45 -45 -45 -45]]

 [[  5   5   5   5   5]
  [ -5  -5  -5  -5  -5]
  [-10 -10 -10 -10 -10]
  [-15 -15 -15 -15 -15]
  [-20 -20 -20 -20 -20]
  [-25 -25 -25 -25 -25]
  [-30 -30 -30 -30 -30]
  [-35 -35 -35 -35 -35]
  [-40 -40 -40 -40 -40]]]

编辑 2: 我以错误的顺序减去数组(整个数组从切片而不是相反)。所以这是一个修复,这次我使用你的例子:

import numpy as np

first_array = np.array(
    [[1, 8, 3, 9, 2],
     [2, 6, 4, 1, 9],
     [4, 2, 12, 8, 16],
     [5, 3, 7, 18, 21],
     [6, 20, 4, 8, 24]]
)

arr_slice = slice(None, 2)
second_array = first_array[arr_slice]

expected_subtracted_array = np.array(
    [[[[1, -2, 1, -8, 7],
       [3, -6, 9, -1, 14],
       [4, -5, 4, 9, 19],
       [5, 12, 1, -1, 22]],
      [[-1, 2, -1, 8, -7],
       [2, -4, 8, 7, 7],
       [3, -3, 3, 17, 12],
       [4, 14, 0, 7, 15]]]]
)

# create an index map of the vector combinations
idx0, idx1 = np.mgrid[:second_array.shape[0], :first_array.shape[0]]
idx0, idx1 = idx0.flatten(), idx1.flatten()
# find the irrelevent combinations and remove them
remove_same = (1 - (idx0 == idx1)).astype(bool)
idx0, idx1 = idx0[remove_same], idx1[remove_same]
arr_sub = first_array[idx1, :] - second_array[idx0, :]
arr_sub = arr_sub.reshape(second_array.shape[0], first_array.shape[0]-1, first_array.shape[1])

(arr_sub == expected_subtracted_array).all()

输出:

True