1、矩阵
>>> import numpy as np
>>> a = np.mat([[1,2,3],[4,5,6],[7,8,9]]) # 3行3列
>>> a
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> a[0,1] # 取第一行第二个数据
2
>>> a[0,:] # 取第一行的数据
matrix([[1, 2, 3]])
>>> b = np.mat([[8,5,3],[2,9,6],[7,1,4]])
>>> b
matrix([[8, 5, 3],
[2, 9, 6],
[7, 1, 4]])
>>> b.shape # 获得矩阵的行列数
(3, 3)
>>> b.shape[0] # 获得矩阵的行数
3
>>> b.shape[1] # 获得矩阵的列数
3
>>> b.sort() # 对每行排序
>>> b
matrix([[3, 5, 8],
[2, 6, 9],
[1, 4, 7]])
# 矩阵相乘(线性代数)
>>> a = np.mat([[1,2,3],[4,5,6],[7,8,9]])
>>> b = np.mat([[8,5,3],[2,9,6],[7,1,4]])
>>> a*b
matrix([[ 33, 26, 27],
[ 84, 71, 66],
[135, 116, 105]])
>>> np.matmul(a,b)
matrix([[ 33, 26, 27],
[ 84, 71, 66],
[135, 116, 105]])
>>> np.dot(a,b)
matrix([[ 33, 26, 27],
[ 84, 71, 66],
[135, 116, 105]])
#矩阵点乘(对应位置相乘)
>>> a = np.mat([[1,2,3],[4,5,6],[7,8,9]])
>>> b = np.mat([[8,5,3],[2,9,6],[7,1,4]])
>>> np.multiply(a, b)
matrix([[ 8, 10, 9],
[ 8, 45, 36],
[49, 8, 36]])
# 矩阵转置
>>> a = np.mat([[1,2,3],[4,5,6],[7,8,9]])
>>> a.T
matrix([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
>>> np.transpose(a)
matrix([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
# 矩阵求逆
>>> a = np.mat([[0.5,1],[1,0.5]])
>>> a.I
matrix([[-0.66666667, 1.33333333],
[ 1.33333333, -0.66666667]])
#!!!若a为奇异矩阵,则不可逆,a.I将会报错
2、三角函数
>>> import numpy as np
#cos, cosh, sin sinh, tan, tanh,arccos, arccosh, arcsin, arcsinh, arctan, arctanh
>>> a = np.sin(90*np.pi/180) # sin函数计算时,括号中为弧度
>>> a
1.0
>>> np.degrees(np.pi/2) # 弧度转度
90.0
>>> np.radians(180) # 度转弧度
3.141592653589793
>>> import math
#sin,cos,tan,asin,acos,atan,atan2,sinh,cosh,tanh,asinh,acosh,atanh
>>> math.sin(math.pi/2)
1.0
>>> math.cos(0)
1.0
>>> math.tan(math.pi/6)
0.5773502691896257
>>> math.degrees(math.pi/2) # 弧度转度
90.0
>>> math.radians(180) # 度转弧度
3.141592653589793