如何使用numpy在一个数组中使用一组向量进行向量的点积?

问题描述 投票:0回答:2

给定N×M阵列W和大小为N的向量V,如何将每个W列的点积V取出,得到大小为M的1-D阵列D,其中D的每个元素由点组成V和W [:,i]的乘积。

所以像

V = np.random.int(N)
W = np.random.int((N,M))
D = np.zeros(M)
for i in np.arange(M):
    D[i] = dotproduct(V,W[:,i])

有没有办法只使用numpy数组和numpy函数?我想避免使用for循环。

numpy-ndarray numpy-broadcasting
2个回答
1
投票

使用np.dot()

v = np.random.randint(3,size = 3)
w =np.random.randint(9, size = (3,3))
np.dot(v,w)

0
投票

使用numpy广播,您可以简单地将向量V和矩阵W相乘

V = np.random.randint(N)
W = np.random.randint((N,M))
D = np.sum(V.T*W,axis=0)
© www.soinside.com 2019 - 2024. All rights reserved.