Operations on numpy matrices

Table of contents

1 matrix object

2 Create matrix

3 Matrix properties

4 Matrix multiplication


1 matrix object

Mathematically, a matrix is ​​a collection of complex or real numbers arranged in a rectangular array, but in NumPy, the matrix np.matrix is ​​a derived class of the array np.ndarray. This means that a matrix is ​​essentially an array and has all the properties and methods of an array; at the same time, a matrix has some characteristics and methods that are different from arrays.

  • First of all, the matrix is ​​two-dimensional and cannot be transformed into any dimension like an array. Even if it is expanded or sliced, the return will be two-dimensional;

  • Secondly, matrices and matrices, matrices and arrays can all perform addition, subtraction, multiplication and division operations, and the operation result always returns a matrix;

  • Finally, matrix multiplication is different from array multiplication.

2 Create matrix

The np.mat() function is used to create a matrix. It can accept parameters in the form of lists, arrays, or even strings. You can also use the dtype parameter to specify the data type.

import numpy as np
​
print(np.mat([[1, 2, 3], [4, 5, 6]], dtype=np.int32))  # 使用列表创建矩阵
print(np.mat(np.arange(6).reshape((2,3)), dtype=np.int32)) # 使用列表创建矩阵
print(np.mat(np.arange(6).reshape((2,3)))) # 使用数组创建矩阵
print(np.mat('1 4 7; 2 5 8; 3 6 9')) # 使用Matlab风格的字符串创建矩阵

In addition, similar to generating special value arrays, the numpy.matlib submodule () also provides multiple functions for generating special value matrices and random number matrices:

import numpy.matlib as mat # 也可以不用单独导入,直接使用numpy.matlib.mat
print(mat.zeros((2,3))) # 全0矩阵
print(mat.ones((2,3))) # 全1矩阵
print(mat.eye(3)) # 单位矩阵
print(mat.empty((2,3))) # 空矩阵
print(mat.rand((2,3))) # [0,1)区间随机数矩阵
print(mat.randn((2,3))) # 均值0方差1的高斯(正态)分布矩阵

3 Matrix properties

Matrix has several unique properties, such as transposed matrix, inverse matrix, conjugate matrix, conjugate transposed matrix, etc.

m = np.mat(np.arange(6).reshape((2,3)))
print(m)
print(m.T) # 返回自身的转置矩阵
print(m.H) # 返回自身的共轭转置矩阵
print(m.I) # 返回自身的逆矩阵
print(m.A) # 返回自身数据的视图(ndarray类)

4 Matrix multiplication

Matrix operations and array operations are roughly the same, only multiplication operations are significantly different. When talking about broadcasting and vectorization, we already know that the multiplication of two arrays is the multiplication of corresponding elements, provided that the structures of the two arrays are the same. In fact, even if the structures of the two arrays are different, multiplication can be done as long as certain conditions are met.

In addition to multiplying corresponding elements, arrays can also be multiplied using the np.dot() function.

For arrays, multiplication using asterisks and multiplication using np.dot() function are two completely different multiplications; for matrices, whether multiplication using asterisk or np.dot() function , the results are all the results of the multiplication of the np.dot() function, because the matrix does not have a corresponding concept of element multiplication. The multiplication implemented by the np.dot() function is matrix multiplication. Examples are as follows:

#数组相乘
a = np.arange(6)
b = np.arange(7,13)
print(a * b)
print(np.dot(a,b))

#矩阵的乘法
m = np.mat(np.arange(6).reshape((2,3)))
print(np.dot(m,m.T))  #m与m的转置相乘

得到:
[ 0  8 18 30 44 60]
160
[[ 5 14]
 [14 50]]

Guess you like

Origin blog.csdn.net/longhaierwd/article/details/131859807