numpy of Python module

Posted by legomez2000 on Sat, 25 Dec 2021 12:56:35 +0100

Briefly write the function definitions of the modules commonly used in Python to facilitate subsequent search (excerpt from Python):

Numpy

Create a numpy array (array a will be used for demonstration later):

a = np.array([[1,2,3],[4,5,6]])

The following statements are usually used to import numpy:

import numpy as np

Then follow up with NP XXX uses the function of numpy module;

Dimension of numpy:

a.ndim

Number of rows and columns:

a.shape

Number of elements:

a.size

Data type to create the array:

a = np.array([[1,2,3],[4,5,6]], dtype=np.int/np.int32/np.float/np.float32)

Select one of the following data types according to your needs;

Create the specified array:

Create an all zero array of 3 rows and 4 columns

a = np.zeros((3,4))

Create a full 1 array of 3 rows and 4 columns:

a = np.ones((3,4))

Create an empty array with 3 rows and 4 columns, but the data created is very close to 0, not 0:

a = np.empty((3.4))

Create a continuous array with range, with data of 1-9 and step size of 2:

a = np.arange(1,10,2)

Create line segment data, starting from 1 and ending at 10. It is divided into 18 data between 1 and 10:

a = np.linspace(1,10,18)

Reorganize the array shape. Change the shape of array a to 4 rows and 3 columns. Note that the number of elements of array A should be 12:

a = np.reshape(A, (4,3))

If a and b are arrays with the same number of rows and columns, make a difference:

c = a - b

The same is true for addition;

Multiplication, corresponding multiplication of elements in the array:

c = a * b

The elements in the array correspond to the power, such as the quadratic power:

c = b ** 2   #Use two*

Each element in the array calculates its sine value:

c = np.sin(a)   #a is also an array

For array multiplication, the matrix multiplication is not the corresponding multiplication of elements. Note that the dimensions of the two arrays should meet the conditions of matrix cross multiplication:

c = np.dot(a, b)   # Or c = a.dot(b)

Sum all elements of the array to find the minimum and maximum values:

c = np.sum(a)
c = np.max(a)
c = np.min(a)

Sum by row, minimum by column and maximum by row. Note whether axis is 0 or 1, 0 is column and 1 is row:

c = np.sum(a, axis = 1)
c = np.min(a, axis = 0)
c = np.max(a, axis = 1)

Index the smallest and largest elements in the array:

c = np.argmin(a)
c = np.argmax(a)

Find the average of the whole array:

c = np.mean(a)   # Or C = NP Average (a) or c = a.mean()

Find the median of the array:

c = np.median(a)     # Or C = a.medium ()

Accumulation of array elements, such as 1, 2, 3, to achieve 1, 3, 6:

c = np.cumsum(a)

Cumulative difference, for example, the calculated 3-row and 4-column array yields an array of 3 rows and 3 columns:

c = np.diff(a)

nonzero() function: get the row and column coordinates of all non-zero elements, and the row and column coordinates are separated by two arrays:

c = np.nonzero(a)

Sort by row from small to large:

c = np.sort(a)

Transpose:

c = np.transpose(a)    #  Or c = a.T

clip(array, min, max) function: unify the array data between min and max. if the element value in the array is less than min, it becomes min; if it is greater than max, it becomes max. the rest remains unchanged:

c = np.clip(a, min, max)    #  min and max values can be set at will

If it is a one-dimensional array, its index:

c = a[2]     # The index is the third element in array a, starting from 0

If it is a two-dimensional array, its index:

c = a[2]       # The index is the third row in array a, starting at 0
c = a[2][2]   # The index is the element of the third row and third column in array a, or C = a [2,2]
# Or c = a[2:3,2:3], using python's left closed right open feature

Array merging, if a and b are two arrays with the same number of rows and columns:

c = np.vstack((a, b))  # For example, both a and b are an array with one row and three columns, then c is an array with two rows and three columns, and b is the second row
# That is, top-down consolidation

Merge left and right, with the same conditions as above:

c = np.hstack((a,b))   # c is an array of six columns in one row

Extended array dimension:

c = a[np.newaxis, :]    # If a = [1,1,1], then c = [[1,1,1]]
c = a[: ,np.newaxis]    # If a = [1,1,1], then c = [[1]
                                   #              [1]
                                   #              [1]]

Merge arrays by row or column:

c = np.concatenate((a,b,b,a)), axis = 0)   # If a = b = [1,1,1], then c is a 4-row 3-column array
c = np.concatenate((a,b,b,a)), axis = 1)   # If a = b = [1,1,1], then c is a 1-row 12 column array

If a is a 3-row and 4-column array, the vertical split axis = 1 and the horizontal split axis = 0:

c = np.split(a, 2, axis = 1)    # c outputs two arrays of 3 rows and 2 columns
c = np.split(a, 3, axis = 0)    # c outputs three arrays of 1 row and 4 columns

The above is equal division, and unequal division is:

c = np.array_split(a ,3, axis = 1)     # c outputs an array with 3 rows and 2 columns and two arrays with 3 rows and 1 column

Other partitions include NP VSplit (a, 3, axis = 0) and NP Hsplit (a, 2, axis = 1)), which is also equal segmentation;

For deep copy, by using the equal sign "c = a", it is similar to giving the address of array a to c, so if the elements in array c change, the elements in a also change accordingly;

For shallow copy, only the value is passed in the form of "c = a.copy()", but the addresses of c and a are different. The elements in c change but will not change the elements in a.

Topics: Python numpy