问题
I have two numpy arrays, like
A: = array([[0, 1],
[2, 3],
[4, 5]])
B = array([[ 6, 7],
[ 8, 9],
[10, 11]])
For each row of A and B, say Ra and Rb respectively, I want to calculate transpose(Ra)*Rb. So for given value of A and B, i want following answer:
array([[[ 0, 0],
[ 6, 7]],
[[ 16, 18],
[ 24, 27]],
[[ 40, 44],
[ 50, 55]]])
I have written the following code to do so:
x = np.outer(np.transpose(A[0]), B[0])
for i in range(1,len(A)):
x = np.append(x,np.outer(np.transpose(A[i]), B[i]),axis=0)
Is there any better way to do this task.
回答1:
You can use extend dimensions of A
and B
with np.newaxis/None to bring in broadcasting for a vectorized solution like so -
A[...,None]*B[:,None,:]
Explanation : np.outer(np.transpose(A[i]), B[i])
basically does elementwise multiplications between a columnar version of A[i]
and B[i]
. You are repeating this for all rows in A
against corresoinding rows in B
. Please note that the np.transpose()
doesn't seem to make any impact as np.outer
takes care of the intended elementwise multiplications.
I would describe these steps in a vectorized language and thus implement, like so -
- Extend dimensions of
A
andB
to form3D
shapes for both of them such that we keepaxis=0
aligned and keep asaxis=0
in both of those extended versions too. Thus, we are left with deciding the last two axes. - To bring in the elementwise multiplications, push
axis=1
of A in its original 2D version toaxis=1
in its3D
version, thus creating a singleton dimension ataxis=2
for extended version ofA
. - This last singleton dimension of
3D
version ofA
has to align with the elements fromaxis=1
in original2D
version ofB
to letbroadcasting
happen. Thus, extended version ofB
would have the elements fromaxis=1
in its 2D version being pushed toaxis=2
in its3D
version, thereby creating a singleton dimension foraxis=1
.
Finally, the extended versions would be : A[...,None]
& B[:,None,:]
, multiplying whom would give us the desired output.
来源:https://stackoverflow.com/questions/35162197/numpy-matrix-multiplication-of-2d-matrix-to-give-3d-matrix