Installation and creation of numpy

install

1. Open cmd as an administrator

2. Enter the command to install numpy

pip insatll numpy

3. Use the following command to check whether the installation is successful

pip list

Create numpy

Data creation

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

parameterParameters and description
ObjectAny sequence type
dtypeRequired data type of array, optional.
copyOptional. The default value is true. Whether the object is copied.
orderC (by row), F (by column), or A (any, default).
subokBy default, the returned array is forced to be a base class array. If true, the subclass is returned.
ndiminSpecifies 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)


"""
explain np02 yes np01 The two are different objects
 therefore np02 and np01 Different data
"""
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 np02 and np01 Is 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
"""
range(start,stop,step)  Function to create a list of integers
1.Don't write start,The default starts at 0
2.Left open right close
3.step Step size, not written, default is 1
"""
for i in range(10):
    print(i)
"""
arange(start,stop,step,dtype) 
1.Don't write start,The default starts at 0
2.Left open right close
3.step Step size, not written, default 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

functiondescribe
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() generates random numbers between 0 and 1  
#Create a one-dimensional array size to generate 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 open right close
2.Don't write low The default 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)Generate 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) Generate uniformly distributed random numbers
low: Do not write. The default is 0,
high: ending,
: length
1.Left open right close
"""
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)Random disorder order
"""
np01 = np.array([2,5,6,7,3])
print(np01)
np.random.shuffle(np01)
print(np01)
"""
np.random.seed()Set 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 it every time before generating a random number seed()
"""
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)  Generate 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(shaps,dtype=float,order="C") #Creates an array of the specified size, which is filled with 0
"""
numpy.zeros(shaps,dtype=float,order="C") #Creates an array of the specified size, which is filled with 0
shaps:dimension
dtype: data type
order: By row by 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

functiondescribe
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)

parameterdescribe
startStarting value of the sequence
stopThe termination value of the sequence. If endpoint=True, it proves that the array is contained in the sequence
numThe number of generated samples is 50 by default
endpointIf true, stop is included, otherwise it is not included
retstepIf retstep=Ture, the generated array will display the spacing, otherwise it will not be displayed
dtypedata 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)

parameterdescribe
startStarting value of the sequence
stopThe termination value of the sequence. If endpoint=True, it proves that the array is contained in the sequence
numThe number of generated samples is 50 by default
endpointIf true, stop is included, otherwise it is not included
baseThe base of log. The default value is 10.0
dtypedata type

Keywords: Python

Added by sashi34u on Tue, 23 Nov 2021 14:21:23 +0200