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)
 

猜你喜欢

转载自www.cnblogs.com/jumpkin1122/p/11503583.html