矩阵与数组
In [3]: from numpy import *
In [4]: a = random.rand(4,3)
In [5]: a
Out[5]:
array([[ 0.18283831, 0.21897507, 0.06911263],
[ 0.56010641, 0.44530748, 0.96900526],
[ 0.07938149, 0.83705278, 0.20325507],
[ 0.90471882, 0.53764717, 0.33122263]])
In [6]: mat(a)
Out[6]:
matrix([[ 0.18283831, 0.21897507, 0.06911263],
[ 0.56010641, 0.44530748, 0.96900526],
[ 0.07938149, 0.83705278, 0.20325507],
[ 0.90471882, 0.53764717, 0.33122263]])
In [7]: type(a)
Out[7]: numpy.ndarray # 数组类型
In [8]: a = mat(a)
In [9]: type(a)
Out[9]: numpy.matrixlib.defmatrix.matrix # 矩阵类型
In [10]: a
Out[10]:
matrix([[ 0.18283831, 0.21897507, 0.06911263],
[ 0.56010641, 0.44530748, 0.96900526],
[ 0.07938149, 0.83705278, 0.20325507],
[ 0.90471882, 0.53764717, 0.33122263]])
In [11]: a.I # 矩阵求逆(没有使用方阵,这里是伪逆矩阵)
Out[11]:
matrix([[ 0.17987522, -0.31261057, -0.73297284, 1.32681177],
[ 0.19891349, -0.29239699, 1.27986334, 0.02852485],
[-0.22442968, 1.3461827 , -0.16030736, -0.77399272]])
In [21]: a * a.I # 因为不是方阵,所以没有输出单位阵
Out[21]:
matrix([[ 0.06093425, -0.02814661, 0.13516338, 0.19534558],
[-0.02814661, 0.99915636, 0.00405125, 0.00585509],
[ 0.13516338, 0.00405125, 0.98054541, -0.02811685],
[ 0.19534558, 0.00585509, -0.02811685, 0.95936398]])
In [1]: from numpy import *
In [4]: a = random.rand(4,4)
In [5]: a
Out[5]:
array([[ 0.71411637, 0.47290226, 0.55636164, 0.20517478],
[ 0.3554218 , 0.90012918, 0.9086697 , 0.14233483],
[ 0.33007245, 0.69314144, 0.40207699, 0.07290092],
[ 0.20740437, 0.83340077, 0.15481733, 0.11737046]])
In [7]: a = mat(a)
In [8]: a * a.I # 结果有误差
Out[8]:
matrix([[ 1.00000000e+00, 0.00000000e+00, -4.44089210e-16, 1.11022302e-16],
[ -1.11022302e-16, 1.00000000e+00, 0.00000000e+00, 3.33066907e-16],
[ -5.55111512e-17, -2.22044605e-16, 1.00000000e+00, -1.11022302e-16],
[ -5.55111512e-17, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]])
In [9]: eye(4) # 单位阵,array类型
Out[9]:
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
In [13]: myEye = a * a.I
In [14]: myEye - eye(4) # matrix可以直接与array运算
Out[14]:
matrix([[ 0.00000000e+00, 0.00000000e+00, -4.44089210e-16, 1.11022302e-16],
[ -1.11022302e-16, -2.22044605e-16, 0.00000000e+00, 3.33066907e-16],
[ -5.55111512e-17, -2.22044605e-16, 0.00000000e+00, -1.11022302e-16],
[ -5.55111512e-17, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]])
In [15]: type(myEye)
Out[15]: numpy.matrixlib.defmatrix.matrix
tile 函数
像铺瓷砖一样扩展数据
In [18]: a = random.rand(2,2)
In [19]: a
Out[19]:
array([[ 0.69860728, 0.35574496],
[ 0.80746093, 0.20802703]])
In [20]: tile(a,(2,2)) # 以a为子矩阵扩展数据
Out[20]:
array([[ 0.69860728, 0.35574496, 0.69860728, 0.35574496],
[ 0.80746093, 0.20802703, 0.80746093, 0.20802703],
[ 0.69860728, 0.35574496, 0.69860728, 0.35574496],
[ 0.80746093, 0.20802703, 0.80746093, 0.20802703]])
argsort函数
返回数组值从小到大的索引值,与原数组shape相同
In [43]: a = random.rand(1,5)
In [44]: a
Out[44]: array([[ 0.13951597, 0.07581534, 0.6957919 , 0.2080221 , 0.8629544 ]])
In [45]: a.argsort()
Out[45]: array([[1, 0, 3, 2, 4]]) # 升序排列数组,返回索引
In [47]: a = random.rand(4,2)
In [48]: a
Out[48]:
array([[ 0.18149948, 0.57567855],
[ 0.00914879, 0.14953625],
[ 0.48911966, 0.75737031],
[ 0.49874594, 0.07773392]])
In [50]: b=a.sum(axis=1) # 按行加和
In [51]: b
Out[51]: array([ 0.75717803, 0.15868504, 1.24648996, 0.57647986])
In [52]: c=a.sum(axis=0) # 按列加和
In [53]: c
Out[53]: array([ 1.17851386, 1.56031903])
In [54]: a.argsort(axis=0) # 按列排序,元素在一列中所占的位置
Out[54]:
array([[1, 3],
[0, 1],
[2, 0],
[3, 2]])
In [55]: a.argsort(axis=1) # 按行排序,元素在一行中所占的位置
Out[55]:
array([[0, 1],
[0, 1],
[0, 1],
[1, 0]])
In [56]: a.argsort() # 默认行排序
Out[56]:
array([[0, 1],
[0, 1],
[0, 1],
[1, 0]])