import numpy as np
a=np.array([[1,2],[3,4]])
b=np.array([[1,2],[3,4]])
c=np.array([1,3])
d=np.array([0,1])
e=np.array([[2,1]])
print(a*b)#对应位置元素相乘
print(np.multiply(a,b))#对应位置元素相乘
[[ 1 4]
[ 9 16]]
[[ 1 4]
[ 9 16]]
print(a*c)
print(np.multiply(a,c))
[[ 1 6]
[ 3 12]]
[[ 1 6]
[ 3 12]]
print(c*d)
print(np.multiply(c,d))
[0 3]
[0 3]
print(d*e)
print(np.multiply(d,e))
[[0 1]]
[[0 1]]
a=np.array([[1,2],[3,4]])
b=np.array([[1,2],[3,4]])
c=np.array([1,3])
d=np.array([0,1])
e=np.array([[2,1]])
print(np.dot(a,b))#行对列,对应元素相乘再相加求和(矩阵乘法)
print(a@b)
print(np.matmul(a,b))
[1,2] [1,2]
[3,4] [3,4]
计算过程为 :
[1x1+2x3 1x2+2x4]
[3X1+4x3 3x2+4x4]
[[ 7 10]
[15 22]]
[[ 7 10]
[15 22]]
[[ 7 10]
[15 22]]
print(np.dot(a,c))
print(a@c)
print(np.matmul(a,c))
[ 7 15]
print(np.dot(c,d))
print(c@d)
print(np.matmul(c,d))
3
print(np.dot(d,e))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
----> 1 print(np.dot(d,e))
<__array_function__ internals> in dot(*args, **kwargs)
ValueError: shapes (2,) and (1,2) not aligned: 2 (dim 0) != 1 (dim 0)
print(np.matmul(d@e))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
1 #两者都报错
----> 2 print(np.matmul(d@e))
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 2)
print(d@e)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
----> 1 print(d@e)
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 2)