Working with array data using NumPy

NumPy is one of the core packages for numerical computing in Python, which provides a large number of efficient array manipulation functions and mathematical functions. It supports multidimensional array and matrix operations, and can integrate C/C++ and Fortran codes, so it can process large amounts of data very efficiently. Following are some of the main functions and uses of NumPy:

  1. Multidimensional array: The core of NumPy is the ndarray (N-dimensional array) object, which can be used to store elements of the same type. These arrays can be one-, two-, or higher-dimensional. They provide convenient array indexing and slicing, and many basic operations and calculations (such as addition, subtraction, multiplication, division, exponentiation, etc.).

  2. Array operation: NumPy provides a large number of array operation functions, including mathematical functions (such as trigonometric functions, exponential functions, logarithmic functions, etc.), logical functions (such as Boolean operations, comparison operations, logical operations, etc.), sorting functions, statistical functions, etc. .

  3. Matrix operation: NumPy provides matrix operation functions, such as matrix addition, subtraction, multiplication, transposition, inversion, etc., which can easily perform linear algebra calculations.

  4. Random number generation: NumPy can generate various random numbers, such as normal distribution, uniform distribution, Poisson distribution, Bernoulli distribution, etc., as well as random permutation and random selection.

  5. File IO: NumPy can read and write various file formats, including text files, binary files, and matlab files, to facilitate data storage and transmission.

  6. Integration with other Python libraries: NumPy can be easily integrated with other Python libraries (such as Pandas, SciPy, matplotlib, etc.) for data analysis, scientific computing, and visualization.

import numpy as np

Ndarray

1.ndarray operation

Generate list data into array()

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

Confirm data type

print(a.dtype) # int32

If the floating point number is substituted into the integer type array, the data will automatically become an integer type (automatically discard below the decimal point)

a[1] = -3.6
print(a) # [1 -3 3 4 5]

Transforming data types

a2 = a.astype(np.float32)
print(a2, a2. dtype) # [1. -3. 3. 4. 5.] float32

Two-dimensional array

b = np.array([[1, 2, 3],
              [3.2, 5.3, 6.6]])
print('b=', b) # b= [[1. 2. 3. ][3.2 5.3 6.6]]
print('b[1,2]=', b[1,2]) # b[1,2] = 6.6

Parameters of 2.ndarray

  • Dimensions of ndarry.ndim array
  • The number of rows and columns of ndarry.shape array
  • ndarry.size The number of elements
  • category of ndarry.dtype data
print('ndim =', a.ndim, b.ndim)
print('shape =', a. shape, b. shape)
print('size =', a. size, b. size)
print('dtype =', a.dtype, b.dtype)

#ndim = 1 2
# shape = (5,) (2, 3)
# size = 5 6
# dtype = float32 float64

reshape performs array reorganization (the number of elements remains unchanged)

print(b.reshape(6)) # convert to 1D array [ 1. 2. -1.1 3.2 5.3 6.6]
print(b.reshape(3,2)) # Convert to an array of 3 rows and 2 columns [[ 1. 2. ][-1.1 3.2][ 5.3 6.6]]
print(b.T) # Transpose of matrix [[ 1. 3.2][ 2. 5.3][-1.1 6.6]]

Matrix calculation

In the four arithmetic operations of matrices and numbers, each value is operated

print(b + 2) #[[3. 4. 0.9][5.2 7.3 8.6]]
print(b-2) #[[-1. 0. -3.1][ 1.2 3.3 4.6]]
print(b*2) #[[ 2. 4. -2.2][ 6.4 10.6 13.2]]
print(b/2) #[[ 0.5 1. -0.55][ 1.6 2.65 3.3 ]]
print(b**3) # Power of 3 [[ 1. 8. -1.331][ 32.768 148.877 287.496]]
print(b//1) #Use this method to discard decimals [[ 1. 2. -2.][ 3. 5. 6.]]

When calculating a matrix of the same dimension, the value at the same position is operated (the matrix dimension is different and an error is reported)

c = b/2
print(b + c) # [[ 1.5 3. -1.65][ 4.8 7.95 9.9 ]]
print(b-c) # [[ 0.5 1. -0.55][ 1.6 2.65 3.3 ]]
print(b*c) # [[ 0.5 2. 0.605][ 5.12 14.045 21.78 ]]
print(b/c) # [[2. 2. 2.][2. 2. 2.]]

ranks の product は「@」

A row b column × b row c column = a row c column の row and column

A = np.arange(6).reshape(3,2)
B = np.arange(8).reshape(2,4)
print(A) #[[0 1][2 3][4 5]]
print(B) #[[0 1 2 3][4 5 6 7]]
print(A@B) #[[ 4 5 6 7][12 17 22 27][20 29 38 47]]

Matrix generation

Generation of 1D matrix (initial value, end value, condition)

  • The arange condition is the specified step size, the total is automatically determined, excluding the termination value
  • The linspace condition is the total, and the step size is automatically determined, including the termination value
np.arange(0,10,0.1)
# array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8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.],
# [0., 0.],
# [0., 0.]])

np.ones((5,2,3), dtype=np.int16)

# array([[[1, 1, 1],
# [1, 1, 1]],
#
# [[1, 1, 1],
# [1, 1, 1]],
#
# [[1, 1, 1],
# [1, 1, 1]],
#
# [[1, 1, 1],
# [1, 1, 1]],
#
# [[1, 1, 1],
# [1, 1, 1]]], dtype=int16)

print(np.ones((5,2,2))*128)

[[[128.128.]
# [128. 128.]]
#
# [[128.128.]
# [128. 128.]]
#
# [[128.128.]
# [128. 128.]]
#
# [[128.128.]
# [128. 128.]]
#
# [[128.128.]
# [128. 128.]]]

3. Example

Generate random 2D array

rnd = np.random.random((5,5))
print(rnd)

# [[0.61467866 0.38383428 0.4604147 0.41355961 0.22680966]
# [0.83895625 0.49135984 0.21811832 0.91433166 0.18616649]
# [0.80176894 0.23622139 0.87041535 0.59623534 0.93986178]
# [0.48324671 0.62398314 0.82435621 0.92421743 0.84660406]
# [0.63578052 0.99794079 0.46970418 0.85743179 0.11774799]]

generate image

plt.imshow(rnd, cmap='gray')
plt.colorbar() #0 is black, 1 is white

plt.imshow(rnd>0.5, cmap='gray')
plt. colorbar()

color_img = np.array([
    [[255,0,0],
     [0,255,0],
     [0,0,255]],
    [[255,255,0],
     [0,255,255],
     [255,0,255]],
    [[255,255,255],
     [128,128,128],
     [0,0,0]],
])
plt.imshow(color_img)