Home > Article > Backend Development > Detailed explanation of the operation of arrays by Python Numpy library
1. Introduction
NumPy (Numerical Python) is an extension library for the Python language that supports a large number of Dimensional array and matrix operations, in addition to providing a large number of mathematical function libraries for array operations. The main data structure is the ndarray array.
NumPy is often used together with SciPy (Scientific Python) and Matplotlib (plotting library), a combination widely used as a replacement for MatLab.
SciPy is an open source Python algorithm library and mathematical toolkit. SciPy includes modules for optimization, linear algebra, integration, interpolation, special functions, fast Fourier transform, signal processing and image processing, solving ordinary differential equations, and other calculations commonly used in science and engineering.
Matplotlib is a visual operating interface for the Python programming language and its numerical mathematics extension package NumPy.
2. Create
Create one-dimensional array
(1) Create directly: np.array([1, 2, 3, 4, 5, 6])
(2) Create from python list: np.array(list([1, 2, 3, 4, 5, 6]))
Create constants One-dimensional data of value
(1) Create a constant value with 0: np.zeros(n,dytpe=float/int)
(2) Create a constant value with 1 Value: np.ones(n)
(3) Create an empty array: np.empty(4)
Create an array with increasing elements
( 1) Incremental array starting from 0: np.arange(8)
(2) Given interval, custom step size: np.arange(0,1,0.2)
(3) Given an interval, customize the number: np.linspace(-1,1,50)
Create a multi-dimensional array: Create a single-dimensional array and then add it to the multi-dimensional array
# 数组的结构一定是np.array([]) 无论数组中间存放的是多少“层”数据 # 二维数组相当于存放的是“两层”数组而已 arr1=np.array(list([1, 2, 3, 4, 5])) arr2=np.array([arr1,[1,0,0,1,0]]) # 2*5的两维数组 arr3=np.array(list([[0,0,1,1,1],[1,1,1,0,0],[2,3,4,5,6]])) # 3*5的两维数组 arrx=np.array([arr1,list([1, 2, 3, 4, 5],[1,1,1,0,0])]) # 报错 arry=np.array([list([[ 1,2,3, 7, 11],[2,3,4,5,6]]),[1, 2, 3, 4, 5]]) # 报错
Related recommendations: "Python Video Tutorial"
Create (n*m)-dimensional data with constant values
(1) Create a constant value of 0: np.zeros((n*m),dytpe=float/int)
(2) Create a constant value with 1: np.ones((n*m))
(3 )Create an empty array: np.empty((n*m))
Create an array of random numbers
Generate a random number seed:
(1) np.random.seed()
(2) np.random.RandomState()
Generate random numbers:
Generates yes Random array with regular distribution
(1) Binomial distribution: np.random.binomial(n, p, size)
(2) Normal distribution: np.random.normal(loc , scale, size)
Convert csv files into arrays or arrays
Use np.genfromtxt('csv file name', delimiter = 'delimiter in the file') function Convert the file into an array
csv_array = np.genfromtxt('sample.csv', delimiter=',') print(csv_array)
3. Transformation of the array
Generates the function of array/matrix transposition, that is, the exchange of row and column numbers, use .T
a = np.array([[32, 15, 6, 9, 14], [12, 10, 5, 23, 1], [2, 16, 13, 40, 37]]) print(a.T) ------------------- # 结果如下 [[32 12 2] [15 10 16] [ 6 5 13] [ 9 23 40] [14 1 37]]
Change the shape of the array:
(1) arr.resize(n,m): The arr.resize(n,m) function modifies the array in place, requiring: the number of elements must be consistent
a=np.arange(8) a.resize(2,4) print(a) --------------------------- [[0 1 2 3] [4 5 6 7]]
(2) arr.reshape(n,m): If the parameter of a certain dimension is -1, it means that the total number of elements will be calculated based on the other dimension.
a=np.arange(8).reshape(-1,1) print(a) ----------------- [[0] [1] [2] [3] [4] [5] [6] [7]]
Will one Raising the dimension to two dimensions: np.newaxis
np.newaxis actually means directly increasing the dimension. We generally do not add too many dimensions to the array. Here is an example of increasing one dimension to two dimensions:
(1) Increase the row dimension: arr[np.newaxis, :]
(2) Increase the column dimension: arr[:, np.newaxis]
a=np.arange(8) a # array([0, 1, 2, 3, 4, 5, 6, 7]) a.shape # (8,) a[np.newaxis, :] # array([[0, 1, 2, 3, 4, 5, 6, 7]]) a.shape # (8,) a[: , np.newaxis] # array([[0],[1],[2],[3],[4],[5],[6],[7]]) a.shape # (8,)
Dimensionality reduction : arr.ravel()
arr.ravel() function when reducing dimensions: the default is to generate a new array in row order (that is, read line by line); if the parameter "F" is passed in, the column order is reduced Dimensions generate new array
a=np.array([[1,2],[3,4]]) a.ravel() a.ravel('F') ---------------------------- # 结果 array([1, 2, 3, 4]) # 结果 array([1, 3, 2, 4])
4. Calculation
Perform calculation operations on arrays
(1) Add and subtract elements
a=np.arange(8).reshape(2,4) # array([[0, 1, 2, 3], [4, 5, 6, 7]]) b=np.random.randint(8,size=(2,4)) # array([[1, 2, 5, 3], [4, 1, 0, 6]]) a+b a-b ---------------------------- # a+b和a-b结果分别是: array([[ 1, 3, 7, 6], [ 8, 6, 6, 13]]) array([[-1, -1, -3, 0], [ 0, 4, 6, 1]])
(2) Multiplication: square/multiply the elements in the matrix
a=np.arange(8).reshape(2,4) # array([[0, 1, 2, 3], [4, 5, 6, 7]]) b=np.random.randint(8,size=(2,4)) # array([[1, 2, 5, 3], [4, 1, 0, 6]]) a**2 a*b ----------------------- # a矩阵平方/a*b矩阵中元素相乘结果分别: array([[ 0, 1, 4, 9], [16, 25, 36, 49]]) array([[ 0, 2, 10, 9], [16, 5, 0, 42]])
(3) Matrix*matrix:
# 要求a矩阵的行要等于b矩阵的列数;且a矩阵的列等于b矩阵的行数 a=np.arange(8).reshape(2,4) # array([[0, 1, 2, 3], [4, 5, 6, 7]]) b=np.random.randint(8,size=(4,2)) # array([[3, 0],[3, 3],[5, 6],[6, 7]]) c1 = np.dot(a,b) c2 = a.dot(b) ---------------------- # ab矩阵相乘的结果:c1=c2 array([[ 31, 36], [ 99, 100]])
(4) Logical calculation
[Note] The list cannot be used as a whole to make logical judgments on the individual elements in it!
# 结果返回:一个数组,其中每个元素根据逻辑判断的布尔类型的结果 a > 3 ----------------------------- # 结果如下: array([[False, False, False, False], [ True, True, True, True]])
5. Value
Get an element in a one-dimensional array: The operation is the same as the index of the list list
a = np.array([5, 2, 7, 0, 11]) a[0] # 结果为 5 a[:4] # 结果为 从头开始到索引为4结束 a[2:] # 结果为 从索引为2的开始到结尾 a[::2] # 结果为 从头开始到结尾,每2个取一个值
Get a multi-dimensional array An element, a row or a column value
a = np.array([[32, 15, 6, 9, 14], [12, 10, 5, 23, 1], [2, 16, 13, 40, 37]]) a[2,1] # 结果是一个元素 16 a[2][1] # 结果是一个元素 16 a[1] # 第2行 array([12, 10, 5, 23, 1]) a[:,2] # 取出全部行,第2列 [15,10,16] a[1:3, :] # 取出[1,3)行,全部列 a[1,1:] # array([10, 5, 23, 1])
Get the
# 需要注意的是,我们数据进行逻辑计算操作得到的仍然是一个数组 # 如果我们想要的是一个过滤后的数组,就需要将"逻辑判断"传入数组中 a = np.array([[32, 15, 6, 9, 14], [12, 10, 5, 23, 1], [2, 16, 13, 40, 37]]) a[a > 3] a[(a > 3) | (a < 2)] ------------------------------ # 结果分别是: array([32, 15, 6, 9, 14, 12, 10, 5, 23, 16, 13, 40, 37]) array([32, 15, 6, 9, 14, 12, 10, 5, 23, 1, 16, 13, 40, 37])
that satisfies the logical operation Traversal: the result is output in rows
a = np.array([[32, 15, 6, 9, 14], [12, 10, 5, 23, 1], [2, 16, 13, 40, 37]]) for x in a: print(x) -------------------- [32 15 6 9 14] [12 10 5 23 1] [ 2 16 13 40 37]
6. Copy/ Split/Merge
Copy: arr.cope()
Split:
(1) Equal parts: np.split(arr, n, axis=0 /1) (That is, only when the number of rows or columns can be divided evenly by n)
(2) Unequal division: np.array_split(arr, n) Default is divided into n parts by row
a = np.array([[32, 15, 6, 9, 14, 21], [12, 10, 5, 23, 1, 10], [2, 16, 13, 40, 37, 8]]) # 可以看到a矩阵是(3*6),所以使用np.split()只能尝试行分成3份;或者列分成2/3/6份 np.split(a,3,axis=0) np.split(a,3,axis=1) np.array_split(a,2) np.array_split(a,4,axis=1) ------------------------------------------- [array([[32, 15, 6, 9, 14, 21]]), array([[12, 10, 5, 23, 1, 10]]), array([[ 2, 16, 13, 40, 37, 8]])] [array([[32, 15], [12, 10], [ 2, 16]]), array([[ 6, 9], [ 5, 23], [13, 40]]), array([[14, 21], [ 1, 10], [37, 8]])] [array([[32, 15, 6, 9, 14, 21], [12, 10, 5, 23, 1, 10]]), array([[ 2, 16, 13, 40, 37, 8]])] [array([[32, 15], [12, 10], [ 2, 16]]), array([[ 6, 9], [ 5, 23], [13, 40]]), array([[14], [ 1], [37]]), array([[21], [10], [ 8]])]
Merge: np.concatenate((arr1, arr2, arr3), axis=0/1) Default is connected to the data
a=np.random.rand(2,3) b=np.random.randint(1,size=(2,3)) np.concatenate((a,b,a)) # 接在下面 np.concatenate((a,b,a),axis=1) # 接在后面 ------------------------ array([[0.95912866, 0.81396527, 0.809493 ], [0.4539276 , 0.24173315, 0.63931439], [0. , 0. , 0. ], [0. , 0. , 0. ], [0.95912866, 0.81396527, 0.809493 ], [0.4539276 , 0.24173315, 0.63931439]]) array([[0.95912866, 0.81396527, 0.809493 , 0. , 0. , 0. , 0.95912866, 0.81396527, 0.809493 ], [0.4539276 , 0.24173315, 0.63931439, 0. , 0. , 0. , 0.4539276 , 0.24173315, 0.63931439]])
The above is the detailed content of Detailed explanation of the operation of arrays by Python Numpy library. For more information, please follow other related articles on the PHP Chinese website!