numpy使用之numpy.dot, numpy.multiply

参考自numpy帮助文档

1. numpy.dot

原型: numpy.dot(a, b, out=None)
两个numpy数组的点乘
(1). 如果两个参数 a , b a,b 都是 1 1 维的,该运算向量内积(没有复数共轭时)
np.dot([1,2],[1,2])
5
(2). 如果两个参数 a , b a,b 都是 2 2 维的,该运算做矩阵乘法,但是使用matmul或者a@b更好
np.dot([[1,2],[3,4]],[[1,0],[0,1]])

array([[1, 2],
       [3, 4]])

(3). 如果参数 a a 或者 b b 0 0 维(标量)的,该运算与multiply,但是使用np.multiply(a, b)或者a*b更好
np.dot([[1,2],[3,4]],1)

array([[1, 2],
       [3, 4]])

(4). 如果 a a N N 维的, b b 1 1 维(标量)的,它做的是 a a 在最后一根轴方向上与 b b 做点积(相乘求和)。

a = np.arange(3*2).reshape((3,2))
print(a)
print(np.dot(a, [1,1]))
[[0 1]
 [2 3]
 [4 5]]

[1 5 9]

(5). 如果 a a N N 维的, b b M ( M > = 2 ) M(M>=2) 维的,它做的是 a a 在最后一根轴方向上与 b b 在倒数第二根轴上做点积(相乘求和)。
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

2. numpy.multiply

原型: numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'multiply'>
参数逐元素相乘
Note: 从数组广播的角度,等价于x1*x2
Examples
1.
np.multiply([1,2,3],[2,3,4])
array([ 2, 6, 12])
2.
np.multiply([1,2,3],[2])
array([2, 4, 6])

猜你喜欢

转载自blog.csdn.net/alwaysyxl/article/details/83050613