Matrix operations in Numpy
1 Matrices and vectors
1.1 Matrix
The difference between matrix, English matrix, and array must be 2-dimensional, but array can be multi-dimensional.
As shown in the figure: This is a 3×2 matrix, that is, 3 rows and 2 columns. If m is a row and n is a column, then m×n is 3×2
The dimension of the matrix is the number of rows × the number of columns
Matrix elements (matrix items):
Aij refers to the element in row i and column j.
1.2 Vector
A vector is a special kind of matrix, which is generally a column vector. The three-dimensional column vector (3×1) is shown below. )
2 Addition and scalar multiplication
Addition of matrices: equal number of rows and columns can be added. [Add corresponding positions]
Example:
Matrix multiplication: Every element must be multiplied. [Multiply the scalar and the element at each position]
Example:
The combination algorithm is similar.
3 Matrix vector multiplication
The multiplication of matrix and vector is shown in the figure: m×n matrix multiplied by n×1 vector, the result is m×1 vector
Example:
1*1+3*5 = 16
4*1+0*5 = 4
2*1+1*5 = 7
Matrix multiplication follows the guidelines:
(M rows, N columns)*(N rows, L columns) = (M rows, L columns)
4 Matrix multiplication
Matrix multiplication:
The m×n matrix is multiplied by the n×o matrix to become the m×o matrix.
Example: For example, now there are two matrices A and B, then their product can be expressed in the form shown in the figure.
5 Properties of matrix multiplication
The matrix multiplication does not satisfy the commutative law: A×B≠B×A
The matrix multiplication satisfies the associative law. Namely: A×(B×C)=(A×B)×C
Identity matrix: In the multiplication of matrices, there is a matrix that plays a special role, just like 1 in the multiplication of numbers, we call this matrix the identity matrix . It is a square matrix, generally represented by I or E. The elements on the diagonal from the upper left corner to the lower right corner (called the main diagonal) are all 1, except for all 0. Such as:
6 Reverse and transpose
The inverse of the matrix: if matrix A is an m×m matrix (square matrix), if there is an inverse matrix, then:
Low-order matrix inversion method:
1. Undetermined coefficient method
2. Elementary transformation
Matrix transposition: Let A be a matrix of order m×n (that is, m rows and n columns), and the element in the i-th row and j column is a(i,j), namely:
A=a(i,j)
Define the transpose of A as such an n×m matrix B, satisfying B=a(j,i), that is, b (i,j)=a (j,i) (the element in the i-th row and j-th column of B is The element in the j-th row and the i-th column of A), record AT=B.
Intuitively, all the elements of A are mirror-reversed around a 45-degree ray starting from the element in the first row and the first column, and the transposition of A is obtained.
Example:
7 Matrix operations
7.1 Matrix multiplication api:
- np.matmul 【Matrix Multiplication】
- np.dot 【dot multiplication】
- [There is no difference in matrix multiplication between the two, but dot supports matrix and number multiplication]
>>> a = np.array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
>>> b = np.array([[0.7], [0.3]])
>>> np.matmul(a, b)
array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])
>>> np.dot(a,b)
array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])
The difference between np.matmul and np.dot:
Both are matrix multiplications. Multiplication of matrices and scalars is prohibited in np.matmul. There is no difference between np.matmul and np.dot in the inner product operation of vector by vector.
7 Summary
- 1.Matrices and vectors
- Matrix is a special two-dimensional array
- A vector is a row or column of data
- 2. Matrix addition and scalar multiplication
- Addition of matrices: equal number of rows and columns can be added.
- Matrix multiplication: Every element must be multiplied.
- 3. Multiply matrix and matrix (vector)
- (M rows, N columns)*(N rows, L columns) = (M rows, L columns)
- 4. Matrix properties
- The matrix does not satisfy the exchange rate and satisfies the associative law
- 5. Identity matrix
- The diagonal is a matrix of 1, and the other positions are 0
- 6. Matrix operations
- e.g. matmul
- np.dot
- Note: Both are matrix multiplications. Multiplication of matrices and scalars is prohibited in np.matmul. There is no difference between np.matmul and np.dot in the inner product operation of vector by vector.