1. Build a matrix
* 1), build a matrix
asmatrix() function in set form .
(1) an array establishing a matrix
function matrix (data, dtype = None, copy = True), data of a numeric type set
together the object, the type dtype specify the output matrix, copy = True deep copy build
establish new matrix objects, copy=False only builds views based on collection objects (
see section 5.2 for the principles of deep copy and view). The function is similar to the mat() function,
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]]) #先建立数组
A = np.matrix([[1,2,3],[4,5,6],[7,8,9]]) #后建立矩阵
print(a,b)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
A
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
2. Construct a nested matrix
function bmat(obj), where obj is a collection object, where the main index of the collection Group, matrix.
A1 = np.matrix([1,2,3]) #构建二维矩阵A1,函数matrix()自动会将一维列表转二维
B1 = np.matrix([4,5,6]) #构建二维矩阵B1
np.bmat([[A1],[B1]]) #矩阵嵌套,这里要求A1,B1长度一致,否则出错
matrix([[1, 2, 3],
[4, 5, 6]])
**3. Construct coordinate (grid) matrix
function meshgrid(*xi, **kwargs), *xi represents a one-dimensional coordinate array object , Such as x, y, z representing a one-dimensional array object of (x, y, z) coordinate values; kwargs accepts key-value pair parameters, such as sparsel=True returns a sparse matrix, copy=False returns a view of the original array.
x = np.arange(3)
y = np.arange(4)
X,Y=np.meshgrid(x,y)
print('x数组: ',x)
print('y数组: ',y)
print('X矩阵: ',X)
print('Y矩阵: ',Y)
x array: [0 1 2]
y array: [0 1 2 3]
X matrix: [[0 1 2]
[0 1 2]
[0 1 2]
[0 1 2]]
Y matrix: [[0 0 0 ]
[1 1 1]
[2 2 2]
[3 3 3]]
2. Matrix transposition and dimension adjustment
Firstly establish the original matrix D to be transposed.
1) Transpose matrix
Use matrix attribute T to turn each column of the matrix into each row (turn 90 degrees counterclockwise).
d = np.arange(9).reshape((3,3))
D = np.matrix(d)
D
matrix([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
D.T #矩阵转置
matrix([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])
2), move the axis position to the new position
function moveaxis(a, source, destination), a is the set Object, source is the starting position of the axis, and destination is the position where the axis moves. From 0 to -1 means moving from left to right, and from -1 to 0 means moving from right to left.
m1 = np.arange(24).reshape(2,3,4) #三维为2,二维3,一维为4
m1
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
md = np.moveaxis(m1,0,-1) #0为m1的第三维值下标,-1指向m1的第一维位置
md
array([[[ 0, 12],
[ 1, 13],
[ 2, 14],
[ 3, 15]],
[[ 4, 16],
[ 5, 17],
[ 6, 18],
[ 7, 19]],
[[ 8, 20],
[ 9, 21],
[10, 22],
[11, 23]]])
3). Scroll the specified axis backward to the end position.
Function rollaxis(a, axis, start=0), a is an array or matrix object, axis is the position where the scrolling ends, and start is the axis that starts to roll back.
m2 = np.arange(24).reshape(2,3,4)
print(m2)
np.rollaxis(m2,1,0).shape
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
(3, 2, 4)
4). Swap the positions of two axes.
Function swapaxes(a, axis1, axis2), a is an array or matrix object, axis1 is the dimension of the first axis to be exchanged, and axis2 is the dimension of the second axis to be exchanged.
m3 = np.arange(8).reshape(2,2,2)
m3
varray([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
np.swapaxes(m3,0,2) #第一维的值与第三维的值对换
array([[[0, 4],
[2, 6]],
[[1, 5],
[3, 7]]])
5), Transpose the dimension
function of the array transpose(a, axes=None), a is an array or matrix object, and axes is the transposed dimension list or tuple (under the default value of None, the overall transposition is the same as the T attribute) .
t1 = np.array([[1,2],[3,4]])
np.transpose(t1) #数组转置
array([[1, 3],
[2, 4]])
3. Find the inverse matrix .
In linear algebra, the inverse matrix of a matrix will be found to facilitate calculations between matrices. The necessary and sufficient condition for a matrix A to be invertible is that the determinant |A|≠0.
1) The function inv(a) finds the inverse matrix of a square matrix, a is a matrix or array object.
a = np.array([[1,2],[3,4]]) #必须是方阵
m1 = np.matrix(a)
mv = np.linalg.inv(m1) #求矩阵逆矩阵
mv
matrix([[-2., 1. ],
[1.5, -0.5]]) The method to
check whether the calculation result of the inverse matrix is correct is that the product of the original matrix and the inverse matrix is the identity matrix.
m1*mv
matrix([[1.00000000e+00, 1.11022302e-16],
[0.00000000e+00, 1.00000000e+00]])
2) Generalized inverse matrix (pseudo-inverse matrix)
In addition to finding the inverse matrix of a square matrix, Numpy provides a function pinv(a, rcond=1e-15) to find a pseudo-inverse matrix for general matrices, where a is any matrix or array, rcond is the error value (small singular value).
a = np.arange(9).reshape((3,3))
np.linalg.pinv(a) #求伪逆矩阵
array([[-5.55555556e-01, -1.66666667e-01, 2.22222222e-01],
[-5.55555556e-02, 1.83880688e-16, 5.55555556e-02],
[ 4.44444444e-01, 1.66666667e-01, -1.11111111e-01]])