The difference between 0/ matrix and array

In Numpy, np.array(), which is actually Ndarray, stands for arrays, which can be one-dimensional, two-dimensional, or multidimensional. In NUMpy, np.mat() represents a matrix, which can only be two-dimensional, i.e. rows and columns. So Np. matrix is a special case of Np. array, so it inherits all of array's functions, but also develops its own new functions specifically for matrix. In short, matrix can use any function array can use, and array may not use any function matrix can use.Copy the code

1/ How to generate arrays and matrices

Import numpy as NP <1> generates a one-dimensional array np.array([1,2,3,4]) [1,2,3,4] <2> Generates a two-dimensional array Np. array([[1,2,3,4],[1,2,3,4]]) [[1,2,3,4] [1,2,3,4] 4]] < 3 > generate three-dimensional array data_array = np. The zeros ((3 and 6), dtype = np, int) [[[0 0 0 0 0 0] [0 0 0 0 0 0] [0 0 0 0 0 0] [0 0 0 0 0 0] [0 0 0 0 0 0]] [[0 0 0 0 0 0] [0 0 0 0 0 0] [0 0 0 0 0 0] [0 0 0 0 0 0] [0 0 0 0 0 0]] [[0 0 0 0 0 0] [0 0 0 0 0 0] [0 0 0 Mat ([[1,2,3,4]]) [[1,2,3,4]]] <4> generate 2-dimensional matrix Np.mat ([[1,2,3,4]]) [[1,2,3,4]] # 2-dimensional matrix, one row and four columnsCopy the code

One over the matrices

Matrices with the same structure (row and column) can be added and subtracted, while matrices with different structures cannot be added and subtracted between corresponding elements. A data is multiplied by a matrix, then the data is multiplied by each data in the matrix, without changing the matrix's row and column structure. The number of columns in one matrix must equal the number of rows in the other matrix in order for the two matrices to multiply, i.e., (m,n) * (n, L) = (m, L) the result of a matrix is also a matrixCopy the code

Multiplication of 3 over matrices, according to the rules of matrices

Row 1 times column 1, that's going to be row 1, column 1, row 1 times column 2, that's going to be row 1, column 2, and so onCopy the code

4 over the multiplication of arrays