简单的减法会导致不同阵列形状的广播问题
Simple subtraction causes a broadcasting issue for different array shapes
我尝试使用描述 numpy 广播的 link 来解决我的问题,但无济于事。如何减去以下 numpy 数组:
X = np.array([[[1,2,3,4],[1,2,3,4],[1,2,3,4]],
[[4,3,2,1],[4,3,2,1],[4,3,2,1]]])
X_mean = np.average(X_, axis=1)
当我执行 X - X_mean
时,它指出:
ValueError: operands could not be broadcast together with shapes (2,3,4) (2,4)
但是 X[0] - X_mean[0]
给出了正确的输出:
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
您需要保持尺寸对齐才能 broadcasting
发生。你有 -
In [4]: whos
Variable Type Data/Info
-------------------------------
X ndarray 2x3x4: 24 elems, type `int64`, 192 bytes
X_mean ndarray 2x4: 8 elems, type `float64`, 64 bytes
X_mean
的 Axis-0
已经与 X
的 axis-0
对齐,所以一切都很好。
X_mean
的 Axis-1
与 X
的 axis-2
对齐,所以放在 X_mean
的新轴中有 None/np.newaxis
以便 axis-1
可以 推回 到 axis-2
.
让我们验证形状对齐 -
In [7]: X_mean3D = X_mean[:,None,:]
In [8]: whos
Variable Type Data/Info
-------------------------------
X ndarray 2x3x4: 24 elems, type `int64`, 192 bytes
X_mean ndarray 2x4: 8 elems, type `float64`, 64 bytes
X_mean3D ndarray 2x1x4: 8 elems, type `float64`, 64 bytes
然后,执行将引入广播的减法-
In [5]: X - X_mean[:,None,:]
Out[5]:
array([[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]])
作为补充:根据Numpy Broadcasting Rules,
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
- they are equal
- one of them is 1
所以最好的办法是以在轴 0 上取平均值的方式来塑造数据。
你的情况:
Y=np.rollaxis(X,1) # reshape (3,2,4)
Y-Y.mean(0)
现在直接
array([[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]])
我尝试使用描述 numpy 广播的 link 来解决我的问题,但无济于事。如何减去以下 numpy 数组:
X = np.array([[[1,2,3,4],[1,2,3,4],[1,2,3,4]],
[[4,3,2,1],[4,3,2,1],[4,3,2,1]]])
X_mean = np.average(X_, axis=1)
当我执行 X - X_mean
时,它指出:
ValueError: operands could not be broadcast together with shapes (2,3,4) (2,4)
但是 X[0] - X_mean[0]
给出了正确的输出:
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
您需要保持尺寸对齐才能 broadcasting
发生。你有 -
In [4]: whos
Variable Type Data/Info
-------------------------------
X ndarray 2x3x4: 24 elems, type `int64`, 192 bytes
X_mean ndarray 2x4: 8 elems, type `float64`, 64 bytes
Axis-0
已经与X
的axis-0
对齐,所以一切都很好。Axis-1
与X
的axis-2
对齐,所以放在X_mean
的新轴中有None/np.newaxis
以便axis-1
可以 推回 到axis-2
.
X_mean
的 X_mean
的 让我们验证形状对齐 -
In [7]: X_mean3D = X_mean[:,None,:]
In [8]: whos
Variable Type Data/Info
-------------------------------
X ndarray 2x3x4: 24 elems, type `int64`, 192 bytes
X_mean ndarray 2x4: 8 elems, type `float64`, 64 bytes
X_mean3D ndarray 2x1x4: 8 elems, type `float64`, 64 bytes
然后,执行将引入广播的减法-
In [5]: X - X_mean[:,None,:]
Out[5]:
array([[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]])
作为补充:根据Numpy Broadcasting Rules,
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
- they are equal
- one of them is 1
所以最好的办法是以在轴 0 上取平均值的方式来塑造数据。
你的情况:
Y=np.rollaxis(X,1) # reshape (3,2,4)
Y-Y.mean(0)
现在直接
array([[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]])