Numpy中的三维矩阵乘法

ѕня*_*gнι 5 python numpy matrix python-3.x numpy-ndarray

我必须将两个2-D矩阵相乘,bob并且tim在Numpy Python 3.x中

bob.shape(2,4)

tim.shape(7,4)

这段代码给出了一个形状为的三维矩阵 (2,7,4)

np.array([foo*tim for foo in bob])
Run Code Online (Sandbox Code Playgroud)

它给出了我想要的输出.但是,我想知道是否有更优雅/更快的方式在numpy中执行此操作而不是我必须迭代bob

小智 5

参见Python广播

bob.reshape((2, 1, 4)) * tim.reshape((1, 7, 4))
Run Code Online (Sandbox Code Playgroud)