Installation and creation of Numpy

"""
np.random.shuffle(ArrayLike)Random disorder order
"""
np01 = np.array([2,5,6,7,3])
print(np01)
np.random.shuffle(np01)
print(np01)

install

1. Open cmd as an administrator

 

2. Enter the command to install numpy plug-in

​
pip install numpy

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

 

pip list

 

Create using array

1 import numpy

import numpy as np

  2 create array

1. Create a one-dimensional array

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

  2. Create a two-dimensional array

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

be careful:
If the passed in list contains different types, they will be unified, STR > float > int

  Create with orange

         One dimensional array

import numpy as np
ndarray01 = np.arange(10)                  #Generate 10 elements from 0 to 9
ndarray02 = np.arange(10, 20)              #Generate 10 elements from 10-19
ndarray03 = np.arange(10, 20, 2)           #Generate 10 12 14 16 18, 2 is step, and the interval is 2

         Create a 2D array

ndarray01= np.arange(12).reshape(3, 4)  #Remember that the previous 12 must meet 3 * 4
print(ndarray01)    
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
'''

Creating arrays using random

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
ndarray01= 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
ndarray01= np.random.random((3,4))
#Create two three-dimensional arrays, three rows and four columns
ndarray01= np.random.random((2,3,4))
print(ndarray01) 

"""
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
ndarray02= np.random.randint(1,11,10)#1-10
#Create a 2D array
ndarray02= np.random.randint(1,11,(3,4))#1-10
#Create 3D array
ndarray02= np.random.randint(1,11,(2,3,4))#1-10
print(ndarray02)
#Create a standard normal distribution
#One dimensional array
ndarray01=np.random.randn(4)
#Two dimensional array
ndarray01=np.random.randn(2,4)
#3D array
ndarray01=np.random.randn(3,2,4)
print(ndarray01)
"""
np.random.normal(loc,scale,size)Generate normal distribution(Specify expectation and variance)
loc: Expected, default 0
scale: Variance, default 1.0
size: length
"""
ndarray01= np.random.normal(size=5)
ndarray01= np.random.normal(loc= 2,scale=3,size=5)
ndarray01= np.random.normal(size=(2,3,5))
print(ndarray01)
"""
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

Tags: Python Pycharm IDE

Posted on Tue, 23 Nov 2021 10:13:28 -0500 by TheUkSniper