Python Numpy array
Data processing and calculation, transformation
Import numpy: import numpy as np
Note: The types of elements in the array in numpy must be consistent
Numpy: ndarray (data processing of multidimensional arrays)
1. array
Convert a list to an array with the array function
a1=np.arrary([1,2,3,4,5,6])
a1
a2=np.array([[1,2,3],[4,5,6]])
#array([10, 78, 34, 89, 23])
2.arange
Use the arange function to create an array of numbers in a specified range (arange usage is similar to the range function)
a3=np.arange(60,100,5)
a3
#array([60, 65, 70, 75, 80, 85, 90, 95])
3. linspace
Create an arithmetic sequence with the linspace function
a4=np.linspace(1,100,5)
a4
#array([ 1. , 25.75, 50.5 , 75.25, 100. ])
4.np.random
Generate an array of numbers using the random function in numpy's random module
np.random, random((m,n)): Generate a random array of m rows and n columns
np.array([random.random() for i in range(n)]): Randomly generate an array containing n elements.
np.random.randint(0,z,n): Randomly generate a one-dimensional array containing n elements with values between 0 and z.
np.random.randint(0,z,(m,n)): Generate a random array with m rows and n columns between 0 and z.
a5=np.random.randint(1,101,10)
#array([17, 73, 56, 21, 23, 83, 94, 14, 29, 61])
a6=np.random.rand(1,100,(3,4))
#array([[63, 68, 83, 93],
# [81, 79, 60, 6],
# [28, 81, 57, 87]])
# Randomly generate an array containing ten elements
a6=np.array([random.random() for i in range(10)])
a6
#array([0.9495929 , 0.07423479, 0.19000517, 0.64636538, 0.66012647,
# 0.91676274, 0.27137253, 0.07187407, 0.73341749, 0.17518598])
insert image description here
5. Special functions
np.eye(n): Create an identity matrix with n rows and n columns
np.zeros((m,n)): generate an array with m rows and n columns all 0
np.ones( (m,n) ): generate m
Array np.full( (m,n ) , y) with rows and n columns of all 1s : generate an array with m rows and n columns whose elements are all y
a7=np.eye(4)
#array([[1., 0., 0., 0.],
# [0., 1., 0., 0.],
# [0., 0., 1., 0.],
# [0., 0., 0., 1.]])