install
1. Open cmd as an administrator
2. Enter the command to install numpy plug-in
pip install numpy
- Enter the above command in cmd interface
3. After the installation is successful, enter the pip list command to check whether the installation is successful
pip list
establish
Create using array
array syntax
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
import numpy as np
#Create a one-dimensional array using array
list01 = [1,2,3,4] np01 = np.array(list01) print(np01) print(type(np01))
#Creating a two-dimensional array using array
list02 = [[1,2,3,4],[5,6,7,8]] np02 = np.array(list02) print(np02) print(type(np02))
#Create a 3D array using array
list03 = [[[1,2,3,4],[5,6,7,8],[9,10,11,12]]] np03 = np.array(list03) print(np03) print(type(np03))
Understanding of dimension
Use of array parameters
parameter | Parameters and description |
---|---|
Object | Any sequence type |
dtype | Required data type of array, optional. |
copy | Optional. The default value is true. Whether the object is copied. |
order | C (by row), F (by column), or A (any, default). |
subok | By default, the returned array is forced to be a base class array. If true, the subclass is returned. |
ndimin | Specifies the minimum dimension of the returned array. |
#Object any sequence type
tuple01 = (1,2,3) list01= [1,2,3] set01 = {5,6,7} dict01 ={"Zhang San":10,"Li Si":20} np01= np.array(dict01) print(np01)
#dtype Required data type of array, optional.
list01 = [1,2,3] np01= np.array(list01,dtype=float) print(np01)
""" Note np02 is a copy of np01, and the two are different objects Therefore, the data of np02 and np01 are different """
list01 = [1,2,3] np01= np.array(list01) np02=np.array(np01,copy=True) np02[0]=10 print(np01) print(np02)
""" You can see that the data is the same, indicating that np02 and np01 are the same object """
list01 = [1,2,3] np01= np.array(list01) np02=np.array(np01,copy=False) np02[0]=10 print(np01) print(np02)
#Since the order effect is not obvious and is not commonly used, it is used here as an understanding #subok By default, the returned array is forced to be a base class array. If true, the subclass is returned. #Matrix is a matrix. I'll explain it in detail later. I'll reuse it first
np01 = np.matrix('1 2 7; 3 4 8; 5 6 9') print(type(np01)) print(np01) np02 = np.array(np01, subok=True) np03 = np.array(np01, subok=False) print(type(np02)) #< class' numpy. Matrix '> if true, returns the matrix print(type(np03)) #< class' numpy. Ndarray '> if it is False, it is forced to change to array
#ndimin Specifies the minimum dimension of the returned array.
list01 = [1,2,3] np01= np.array(list01,dtype=float,ndmin=3) print(np01)
Create with orange
Let's review the following range function first """ The range(start,stop,step) function creates a list of integers 1. Do not write start, which starts from 0 by default 2. Left opening right closing 3.step step size. If not written, the default value is 1 """
for i in range(10): print(i)
""" arange(start,stop,step,dtype) 1. Do not write start, which starts from 0 by default 2. Left opening right closing 3.step step size. If not written, the default value is 1 """ #One dimensional array
a = np.arange(10) #[0 1 2 3 4 5 6 7 8 9] a = np.arange(2,10) #[2 3 4 5 6 7 8 9] a = np.arange(1,10,2) #[1 3 5 7 9] a = np.arange(1,10,2,dtype=float) print(a)
#Two dimensional array #Remember that the previous 12 must meet 3 * 4
np01 = np.arange(12).reshape(3, 4) print(np01)
Creating arrays using random
Common random functions
function | describe |
---|---|
np.random.random(size) | Generate random numbers between 0 and 1 |
np.random.randint(low,high=None,size=None,dtype="1") | Generate random integers |
np.random.randn(d0,d1,d2,d3.........) | Generate a standard normal random number (expected 0, variance 1) |
np.random.normal(loc,scale,size) | Generate normal distribution (specify expectation and variance) |
np.random.uniform(low,high,size) | Generate uniformly distributed random numbers |
np.random.shuffle() | Random disorder order |
np.random.seed() | Set random number seed |
np.random.sample(size) | Generate random floating point numbers |
# np.random.random() Generate random numbers between 0 and 1 #Create a one-dimensional array size generates several data, which can be written directly to 4
np01= np.random.random(size=4) #[0.13475357 0.8088961 0.52055803 0.49706622]
#Create a two-dimensional array size=(3,4) 3 rows and 4 columns available () and [], the effect is the same
np01= np.random.random((3,4))
#Create two three-dimensional arrays, three rows and four columns
np01= np.random.random((2,3,4)) print(np01)
""" np.random.randint(low,high=None,size=None,dtype="1") Generate random integers low: start high=None: end size: length Dtype data type. The default is int32 no01.dtype attribute 1. Left opening right closing 2. Do not write low. The default value is 0 """ #Create a one-dimensional array
np01= np.random.randint(1,11,10)#1-10
#Create a 2D array
np01= np.random.randint(1,11,(3,4))#1-10
#Create 3D array
np01= np.random.randint(1,11,(2,3,4))#1-10 print(np01)
#Create a standard normal distribution #One dimensional array
np01=np.random.randn(4)
#Two dimensional array
np01=np.random.randn(2,4)
#3D array
np01=np.random.randn(3,2,4) print(np01)
""" np.random.normal(loc,scale,size) generates a normal distribution (specify expectation and variance) loc: expected, default 0 scale: variance, default 1.0 size: length """
np01= np.random.normal(size=5) np01= np.random.normal(loc= 2,scale=3,size=5) np01= np.random.normal(size=(2,3,5)) print(np01)
""" np.random.uniform(low,high,size) generates uniformly distributed random numbers low: do not write. The default is 0, high: end, : length 1. Left opening right closing """
np01= np.random.uniform(size= 4)#The four data hardly differ much and are relatively uniform np01= np.random.uniform(size= (2,4)) np01= np.random.uniform(high=3) print(np01)
""" np.random.shuffle(ArrayLike) randomly scrambles the order """
np01 = np.array([2,5,6,7,3]) print(np01) np.random.shuffle(np01) print(np01)
""" np.random.seed() sets the random number seed The number selected from each pile of seeds will not change. Selecting random seeds from different piles is different every time. If you want to get the same random number every time, you need to call seed() every time before generating a random number """
np.random.seed(1) np01= np.random.randint(1,10,size= 5) np.random.seed(1) np02= np.random.randint(1,10,size = 5) print(np01) print(np02)
""" np.random.sample(size) generates random floating-point numbers """
np01= np.random.sample(size=2) print(np01) np02= np.random.sample(size=(2,3)) print(np02)
Creating arrays using zeros
Numpy. Zeros (shapes, dtype = float, order = "C") # creates an array of the specified size, and the array is filled with 0
""" Numpy. Zeros (shapes, dtype = float, order = "C") # creates an array of the specified size, and the array is filled with 0 Shapes: Dimensions dtype: data type order: by row and column """
np01= np.zeros(5) #[0. 0. 0. 0. 0.] np01= np.zeros(5,dtype="int32") #[0 0 0 0 0] np01= np.zeros((2,5),dtype="int32")
""" [[0 0 0 0 0] [0 0 0 0 0]] """ print(np01)
Creates a multidimensional array of specific shapes
function | describe |
---|---|
np.zeros((3, 4)) | Create 3 × An array whose elements of 4 are all 0 |
np.ones((3, 4)) | Create 3 × An array whose elements of 4 are all 1 |
np.empty((2, 3)) | Create 2 × 3. The value in the empty data is not 0, but an uninitialized garbage value |
np.zeros_like(ndarr) | Create an array with all 0 elements in the same dimension of ndarr |
np.ones_like(ndarr) | Create an array with all 1 elements in the same dimension of ndarr |
np.empty_like(ndarr) | Create an empty array with ndarr the same dimension |
np.eye(5) | This function is used to create a 5 × 5 matrix, diagonal is 1, and the rest is 0 |
np.full((3,5), 10) | Create 3 × The elements of 5 are all arrays of 10, and 10 is the specified value |
np01= np.ones((2,5),dtype="int32") np01= np.empty((2,5),dtype="int32") print(np01)
list01= [ [1,2,3], [4,5,6] ] np01= np.array(list01) print(np01.shape) np02= np.zeros_like(np01,dtype=float) print(np02) print(np02.shape)
# np.eye(5) This function is used to create a 5 × 5 matrix, diagonal is 1, and the rest is 0
np01= np.eye(5) print(np01)
# np.full((3,5), 10) Create 3 × The elements of 5 are all arrays of 10, and 10 is the specified value #10 is specified. You can specify any value np01= np.full((3,5),5) print(np01)
Creating arrays using linspace
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
parameter | describe |
---|---|
start | Starting value of the sequence |
stop | The termination value of the sequence. If endpoint=True, it proves that the array is contained in the sequence |
num | The number of generated samples is 50 by default |
endpoint | If true, stop is included, otherwise it is not included |
retstep | If retstep=Ture, the generated array will display the spacing, otherwise it will not be displayed |
dtype | data type |
""" np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) """
np01= np.linspace(1,10,5)#From 1-10, 5 data are generated np01= np.linspace(1,10,5,endpoint=True)#Include 10 np01= np.linspace(1,10,5,endpoint=False)#Exclude 10 np01= np.linspace(1,10,5,retstep=True)#Display spacing np01= np.linspace(1,10,5,dtype="int32") print(np01)
Create an array using logspace
np.logspace(start,stop,num=50,endpoint=Ture,base=10.0,dtype=None)
parameter | describe |
---|---|
start | Starting value of the sequence |
stop | The termination value of the sequence. If endpoint=True, it proves that the array is contained in the sequence |
num | The number of generated samples is 50 by default |
endpoint | If true, stop is included, otherwise it is not included |
base | The base of log. The default value is 10.0 |
dtype | data type |
practice
Create a one-dimensional all 0 Darry object with a length of 10, and then make the fifth element equal to Create a array object with elements from 10 to 49. Create a 4 * 4 two-dimensional array and output the array element type. Create an array that can completely transpose coordinate positions from (0, 1, 3) to (3, 0, 1). Convert the data type in question 4 to float64.
catalogue
1. Open cmd as an administrator
2. Enter the command to install numpy plug-in
Creates a multidimensional array of specific shapes
Creating arrays using linspace