我有两个numpy数组,大小为(20 * 3 * 3),大小为b(3 * 3)。令a =(a1,a2,...,a20)。我想像这样明智地计算矩阵乘积元素: c =(c1,c2,...,c20),ci = b.T ai b,i = 1〜20。 如何使用numpy有效地做到这一点?
使用for循环的慢速版本是这样的:
a = np.random.sample((20, 3, 3))
b = np.random.sample((3, 3))
c = np.zeros_like(a)
for i0, ai in enumerate(a):
c[i0] = np.dot(b.T, np.dot(ai, b))
答案 0 :(得分:2)
因为输入是NumPy数组,所以您可以将操作以矢量化形式放置。无需显式的for
循环和索引。
P.S:感谢@yatu,他发现答案的形状不一样。现在,我添加了swapaxes
,以获得与OP的方法一致的答案
np.random.seed(1)
a = np.random.sample((4, 3, 3))
b = np.random.sample((3, 3))
c = np.dot(b.T, np.dot(a, b)).swapaxes(0,1)
print (c)
[[[0.96496962 1.30807122 0.55382266]
[1.42300972 1.98975139 0.81871374]
[0.32358338 0.45493059 0.1346777 ]]
[[1.46772447 2.15650254 0.87555186]
[2.26335921 3.33689922 1.28679305]
[0.71561413 0.96507585 0.54309736]]
[[1.50660527 2.36946435 0.59771395]
[2.49705244 3.76328176 1.06274954]
[0.96090846 1.43636151 0.31807679]]
[[1.03706878 1.94107476 0.61884642]
[1.74739926 3.07419808 1.03537019]
[0.59565039 1.09721382 0.37283626]]]
答案 1 :(得分:2)
您可以尝试np.matmul(b.T, np.dot(a,b))
:
import numpy as np
import pandas as pd
a = np.random.sample((4, 3, 3))
b = np.random.sample((3, 3))
c = np.zeros_like(a)
# using for loop
for i0, ai in enumerate(a):
c[i0] = np.dot(b.T, np.dot(ai, b))
# alternative method
e = np.zeros_like(a)
e = np.matmul(b.T, np.dot(a,b))
# checking for equal
print(np.array_equal(c, e))