Comparison of matrices and arrays in NUMpy

Array can realize all functions of matrix, but it is easier to operate matrix in realizing some functions, for example, matrix multiplication directly uses A*B instead of functions, but array can deal with various data more flexibly, and can represent high-dimensional array with faster speed

numpy.matrix

Create a matrix

Matrix (string/list/tuple/array) mat (string/list/tuple/array)

a= np.matrix('1 2 3; 4, 5 6 ')
b = np.mat([
    [1.2.3],
    [4.5.6]])print(a)
print(type(a))
print(b)
print(type(b))
Copy the code

c = np.array([
    [1.2.3],
    [4.5.6]
])
d = np.mat(c)
print(d)
print(type(c))
print(type(d))
Copy the code

Properties of a matrix object

attribute instructions
.dim The dimension of the matrix
.shape Shape of matrix
.size The number of elements in the matrix
.dtype The data type of the element
print(d)
print(d.ndim)
print(d.shape)
print(d.size)
print(d.dtype)
Copy the code

Matrix operations

Matrix multiplication

m1 = np.mat([
    [0.1],
    [2.3]
])

m2 = np.mat([
    [1.1],
    [2.0]])print(m1*m2)
Copy the code

Matrix transpose

Matrix transpose:.t

Inverse matrix

Matrix inverse:.i

m = np.mat([
    [0.1],
    [2.3]])print(m.T)
print(m.I)
Copy the code