问题描述
在python中,假设我有一个正方形numpy
矩阵 X ,大小为 nxn ,并且我有一个numpy
向量 a 大小为 n .
In python, suppose I have a square numpy
matrix X, of size n x n and I have a numpy
vector a of size n.
非常简单,我想执行 X-a 的广播减法,但是我希望能够指定沿哪个维度,以便我可以将减法指定为沿轴0或轴1.
Very simply, I want to perform a broadcasting subtraction of X - a, but I want to be able to specify along which dimension, so that I can specify for the subtraction to be either along axis 0 or axis 1.
如何指定轴?
推荐答案
让我们生成带有随机元素的数组
Let's generate arrays with random elems
输入:
In [62]: X
Out[62]:
array([[ 0.32322974, 0.50491961, 0.40854442, 0.36908488],
[ 0.58840196, 0.1696713 , 0.75428203, 0.01445901],
[ 0.27728281, 0.33722084, 0.64187916, 0.51361972],
[ 0.39151808, 0.6883594 , 0.93848072, 0.48946276]])
In [63]: a
Out[63]: array([ 0.01278876, 0.01854458, 0.16953393, 0.37159562])
I.沿axis=1
I. Subtraction along axis=1
让我们沿axis=1
进行减法,即我们要从X
的第一行,X
的第二行中减去a
.为了便于检查正确性,我们只使用X
的第一行:
Let's do the subtraction along axis=1
, i.e. we want to subtract a
from the first row of X
, the second row of X
and so on. For ease of inspecting correctness, let's just use the first row of X
:
In [64]: X[0] - a
Out[64]: array([ 0.31044099, 0.48637503, 0.23901049, -0.00251074])
深入那里,发生了什么事:
Going deeper there, what's happening there is :
X[0,0] - a[0], X[0,1] - a[1], X[0,2] - a[2] , X[0,3] - a[3]
因此,我们将X
的第二个轴与a
的第一个轴匹配.由于X
是2D
并且a
是1D
,所以两个都已经对齐:
So, we are matching the second axis of X
with the first axis of a
. Since, X
is 2D
and a
is 1D
, both are already aligned :
X : n x n
a : n
因此,我们只需执行X-a
即可得到所有减法:
So, we simply do X-a
to get all subtractions :
In [65]: X-a
Out[65]:
array([[ 0.31044099, 0.48637503, 0.23901049, -0.00251074],
[ 0.5756132 , 0.15112672, 0.5847481 , -0.3571366 ],
[ 0.26449405, 0.31867625, 0.47234523, 0.1420241 ],
[ 0.37872932, 0.66981482, 0.76894679, 0.11786714]])
最后,看看这里是否有早先获得的X[0] - a
.
And, finally see if we have X[0] - a
obtained earlier is here.
重要说明:这里要注意的是,a
元素将沿一个轴,并且将相减,而广播将沿另一轴发生.因此,在这种情况下,即使沿axis=1
发生减法,也会沿axis=0
广播a
的元素.
Important Note : Thing to be noted here is that a
elems would be along one axis and along that subtraction would be done and the broadcasting would happen along the other axis. So, in this case, even though subtraction is happening along axis=1
, elems of a
would be broadcasted along the axis=0
.
II.沿axis=0
II. Subtraction along axis=0
类似地,让我们沿axis=0
进行减法,即我们要从X
的第一个col减去X
的第二个col减去a
,依此类推.为了便于检查正确性,我们只使用X
的第一个col:
Similarly, let's do the subtraction along axis=0
, i.e. we want to subtract a
from the first col of X
, the second col of X
and so on. For ease of inspecting correctness, let's just use the first col of X
:
In [67]: X[:,0]-a
Out[67]: array([ 0.31044099, 0.56985738, 0.10774888, 0.01992247])
深入那里,发生了什么事:
Going deeper there, what's happening there is :
X[0,0] - a[0], X[1,0] - a[1], X[2,0] - a[2] , X[3,0] - a[3]
因此,我们将X
的第一轴与a
的第一轴匹配.由于X
是2D
并且a
是1D
,我们需要将a
扩展到2D
,并使用a[:,None]
沿其第一轴保留所有元素:
So, we are matching the first axis of X
with the first axis of a
. Since, X
is 2D
and a
is 1D
, we need to extend a
to 2D
and keep all elems along its first axis with a[:,None]
:
X : n x n
a[:,None] : n x 1
因此,我们执行X-a[:,None]
以获得所有减法:
So, we do X-a[:,None]
to get all subtractions :
In [68]: X-a[:,None]
Out[68]:
array([[ 0.31044099, 0.49213085, 0.39575566, 0.35629612],
[ 0.56985738, 0.15112672, 0.73573745, -0.00408557],
[ 0.10774888, 0.16768691, 0.47234523, 0.34408579],
[ 0.01992247, 0.31676379, 0.5668851 , 0.11786714]])
最后,看看这里是否有早先获得的X[:,0] - a
.
And, finally see if we have X[:,0] - a
obtained earlier is here.
这篇关于在python中沿特定轴广播操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!