Python 用numpy批量生产dot产品?

Python 用numpy批量生产dot产品?,python,numpy,tensordot,Python,Numpy,Tensordot,我需要得到多个向量与一个向量的点积。示例代码: a = np.array([0, 1, 2]) b = np.array([ [0, 1, 2], [4, 5, 6], [-1, 0, 1], [-3, -2, 1] ]) 我想得到b每行与a的点积。我可以迭代: result = [] for row in b: result.append(np.dot(row, a)) print(result) 其中: [5,17,2,0] 我如何在不迭代的情

我需要得到多个向量与一个向量的点积。示例代码:

a = np.array([0, 1, 2])

b = np.array([
    [0, 1, 2],
    [4, 5, 6],
    [-1, 0, 1],
    [-3, -2, 1]
])
我想得到
b
每行与
a
的点积。我可以迭代:

result = []
for row in b:
    result.append(np.dot(row, a))

print(result)
其中:

[5,17,2,0]


我如何在不迭代的情况下得到这个?谢谢

使用
numpy.dot
numpy.matmul
而不使用
进行循环:

import numpy as np

np.matmul(b, a)
# or
np.dot(b, a)
输出:

array([ 5, 17,  2,  0])

我只做
@

b@a
Out[108]: array([ 5, 17,  2,  0])

哇,不知道我怎么会忽略了这个解决方案。谢谢