[英]Elementwise multiplication of NumPy arrays of matrices
I have two NumPy arrays (of equal length), each with (equally-sized, square) NumPy matrices as elements. 我有两个NumPy数组(长度相等),每个数组都有(相等大小的正方形)NumPy矩阵作为元素。 I want to do elementwise matrix multiplication of these two arrays, ie get back a single array where the i-th element is the matrix product of the i-th elements of my two arrays. 我想对这两个数组进行逐元素矩阵乘法,即返回单个数组,其中第i个元素是我的两个数组的第i个元素的矩阵乘积。
When I simply try to multiply the arrays together, it seems that the program tries to calculate the matrix product of the arrays , and then fails because their dimensionality is too high (1 for the array + 2 for the matrices which are its elements). 当我只是简单地尝试将数组相乘时,程序似乎尝试计算数组的矩阵乘积,然后失败,因为它们的维数过高(数组的1表示矩阵,矩阵的2表示矩阵)。
The problem could of course be solved with a for-loop, but I was hoping that there was some way in which it could be done that keeps everything internal to NumPy, in order to take full advantage of its increased efficiency.f 这个问题当然可以通过for循环来解决,但是我希望可以通过某种方法将所有内容都保留在NumPy内部,以便充分利用其提高的效率。f
EDIT: 编辑:
To clarify, say I have two arrays np.array([A, B, C])
and np.array([X, Y, Z])
where A
, B
, C
, X
, Y
and Z
are all 3x3 square matrices, what I need is a function that will return np.array([A*X, B*Y, C*Z])
, where *
is matrix multiplication. 为了澄清,假设我有两个数组np.array([A, B, C])
和np.array([X, Y, Z])
,其中A
, B
, C
, X
, Y
和Z
均为3x3方阵,我需要的是一个将返回np.array([A*X, B*Y, C*Z])
函数,其中*
是矩阵乘法。
Operators are "element-wise" by default for numpy
arrays. 默认情况下,对于numpy
数组,运算符为“按元素排列”。 Just use the @
operator (matrix multiplication) instead of *
: 只需使用@
运算符(矩阵乘法)代替*
:
In [24]: A = np.arange(9).reshape(3,3)
In [25]: X = np.array([A[:], A[:]*2, A[:]*3])
In [26]: Y = X[:]
In [27]: X @ Y
Out[27]:
array([[[ 15, 18, 21],
[ 42, 54, 66],
[ 69, 90, 111]],
[[ 60, 72, 84],
[168, 216, 264],
[276, 360, 444]],
[[135, 162, 189],
[378, 486, 594],
[621, 810, 999]]])
In [28]: X[0] @ Y[0]
Out[28]:
array([[ 15, 18, 21],
[ 42, 54, 66],
[ 69, 90, 111]])
In [29]: X[1] @ Y[1]
Out[29]:
array([[ 60, 72, 84],
[168, 216, 264],
[276, 360, 444]])
In [30]: X[2] @ Y[2]
Out[30]:
array([[135, 162, 189],
[378, 486, 594],
[621, 810, 999]])
HTH. HTH。
*
in numpy will do elementwise operations, ie: *
在numpy中将执行元素操作,即:
>>> a
array([[[0.86812606, 0.16249293, 0.61555956],
[0.12381998, 0.84800823, 0.80731896],
[0.56910074, 0.4071833 , 0.069167 ]],
[[0.69742877, 0.45354268, 0.7220556 ],
[0.86638233, 0.97552151, 0.85580334],
[0.01171408, 0.35997806, 0.72999056]]])
>>> b
array([[[0.17162968, 0.52103661, 0.05433799],
[0.19999652, 0.01852179, 0.7936977 ],
[0.22392469, 0.34535168, 0.92808129]],
[[0.7044144 , 0.03183893, 0.16469416],
[0.6214784 , 0.57722859, 0.23789282],
[0.934214 , 0.61396596, 0.5356328 ]]])
>>> a * b
array([[[0.1489962 , 0.08466477, 0.03344827],
[0.02476357, 0.01570663, 0.6407672 ],
[0.12743571, 0.14062144, 0.06419259]],
[[0.49127887, 0.01444031, 0.11891834],
[0.5384379 , 0.5630989 , 0.20358947],
[0.01094346, 0.22101428, 0.39100689]]])
Isn't this what you looking for? 这不是您要找的东西吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.