np.array()中“ * ”,np.dot(),np.multiply(),np.matmul()和@的用法

np.array()中, “ * ”,np.dot(),np.multiply(),np.matmul()和@的用法

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]])

(1) * 的用法,对应位置元素相乘; multiply对应元素相乘,类似 *

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]]

(2) dot 行对列,对应元素相乘再相加,@,matmul三者相似

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)

<ipython-input-88-a4f728f79257> in <module>
----> 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)

<ipython-input-90-afe09a6d039d> in <module>
      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)

<ipython-input-91-b9f371cba1e1> in <module>
----> 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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值