NumPy笔记:运算操作(四则,矩阵)

"""
    运算操作(四则,矩阵)
"""
import numpy as np
print("--------------数组运算(+-*/)----------------")
a = np.linspace(1, 5, 5)
print(a)
print(a+1)
print(a*2)
print(a+a)
print(a/a)
 
print("--------------数组运算(+-*/)----------------")
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print("a+b={},add={}".format(a+b, np.add(a, b)))
print("a-b={},sub={}".format(a-b, np.subtract(a, b)))
print("a*b={},mul={}".format(a*b, np.multiply(a, b)))
print("a/b={},div={}".format(a/b, np.divide(a, b)))
 
print("--------------矩阵运算(dot,matmul)----------------")
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print("a*b={}".format(a.dot(b)))
print("a*b={}".format(np.dot(a, b)))
print("a*b={}".format(np.matmul(a, b)))
 
print("--------------矩阵乘法:(m,n)*(n,l) = (m,l)----------------")
a = np.linspace(1, 8, 8).reshape(2, 4)
print("a:", a.shape)
b = np.linspace(1, 10, 4).reshape(4, 1)
print("b:", b.shape)
c = np.matmul(a, b)
print("c:", c.shape)
 
--------------数组运算(+-*/)----------------
[1. 2. 3. 4. 5.]
[2. 3. 4. 5. 6.]
[ 2.  4.  6.  8. 10.]
[ 2.  4.  6.  8. 10.]
[1. 1. 1. 1. 1.]
--------------数组运算(+-*/)----------------
a+b=[5 7 9],add=[5 7 9]
a-b=[-3 -3 -3],sub=[-3 -3 -3]
a*b=[ 4 10 18],mul=[ 4 10 18]
a/b=[0.25 0.4  0.5 ],div=[0.25 0.4  0.5 ]
--------------矩阵运算(dot,matmul)----------------
a*b=32
a*b=32
a*b=32
--------------矩阵乘法:(m,n)*(n,l) = (m,l)----------------
a: (2, 4)
b: (4, 1)
c: (2, 1)
 

 

转载于:https://www.cnblogs.com/jumpkin1122/p/11503583.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值