numpy Not just Python The most used third-party libraries in , And still SciPy,Pandas And other data science . It provides more data structure than Python
Own “ More advanced , More efficient ”, You can say so ,NumPy The data structure provided is Python Basis of data analysis .

     stay NumPy What we need to focus on in is the use of arrays
NumPy and ufunc, Because this is NumPy And standards Python The biggest difference , and ufunc It can perform function operations on each element in the array .NumPy Many ufunc
Function calculation speed is very fast , Because they all use C Language implemented .ta It is widely used to perform numerical operations on large multidimensional arrays .

Why NumPy Array structure instead of Python Own list list?

*
list The elements of are stored dispersedly in the system memory , and NumPy The array is stored in a uniform continuous memory block . In this way, the array calculation traverses all elements , Unlike a list list
You also need to find the memory address , This saves computing resources ;

*
In memory access mode , The cache will directly remove the byte block from the RAM Load into CPU Register . Because the data is continuously stored in memory ,NumPy Direct use of modern CPU
Vectorization instruction calculation , Load multiple consecutive floating-point numbers in a register ;

*
NumPy The matrix calculation in can be multi-threaded , Make full use of multi-core CPU computing resource , The computational efficiency is greatly improved ;

stay NumPy There are two extremely important objects in the :

one ,ndarray(N-dimensional array object) Used to solve the problem of multidimensional array

1, Create array
# encoding=utf-8 import numpy as np a = np.array([1, 2, 3, 4, 5]) b =
np.array(range(1, 6)) c = np.arange(1, 6) print a print b print c print type(a)
print type(b) print type(c) print a.dtype # The operation results are as follows : # [1 2 3 4 5] # [1 2 3 4 5]
# [1 2 3 4 5] # <type 'numpy.ndarray'> # <type 'numpy.ndarray'> # <type
'numpy.ndarray'> # int64 #tips: Pay attention range and np.arange Differences between # If the right is, the return value is :[1, 2, 3, 4,
5] print range(1, 6) # If the right is, the return value is :<type 'list'> print type(range(1, 6))
print('----------------') # If the right is, the return value is :[1 2 3 4 5] print np.arange(1, 6) #
If the right is, the return value is :<type 'numpy.ndarray'> print type(np.arange(1, 6))

2,numpy Common data types in :

 

3, Operation of data type :

  3.1, Specifies the data type of the created array :
a=np.array([1,0,1,0],dtype=np.bool) # The return result is shown on the right :[ True False True False] print a

  3.2, Modify the data type of the array :
a = np.array([1, 0, 1, 0], dtype=np.bool) a=a.astype(np.float) # The return result is shown on the right :[1.
0. 1. 0.] print a
  3.3, Modify the number of decimal places of floating-point type :
b = np.array([0.1214, 0.1767, 0.1999]) b = np.round(b, 2) # The return result is shown on the right : [0.12 0.18
0.2] print b
4, View the shape of the array
a=np.array([[3,4,5,6,7,8],[4,5,6,7,8,9]]) # The operation result is shown on the right :(2, 6) print a.shape

5, Modify the shape of the array :
a = np.array([[3, 4, 5, 6, 7, 8], [4, 5, 6, 7, 8, 9]]) a = a.reshape(3, 4) #
The operation result is shown on the right :(3, 4) print a.shape

6, Convert an array to one-dimensional data :
a = np.array([[3, 4, 5, 6, 7, 8], [4, 5, 6, 7, 8, 9]]) a = a.reshape(1, 12) #
The operation result is shown on the right :[3 4 5 6 7 8 4 5 6 7 8 9] print a.flatten()

7,numpy Indexing and slicing :
a = np.array([ [0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]) # Take a line ,[4 5 6 7]
print a[1] # Take a column ,[ 2 6 10] print a[:, 2] # Take multiple lines ,[[ 4 5 6 7] # [ 8 9 10 11]]
print a[1:3] # Take multiple columns ,[[ 2 3] # [ 6 7] # [10 11]] print a[:, 2:4] # Take some lines , All columns ,[[ 4
5 6 7] # [ 8 9 10 11]] print a[[1, 2], :] # Get all rows , Some columns ,[[ 2 3] # [ 6 7] # [10
11]] print a[:, [2, 3]]

8,numpy Medium Boolean index :
t = np.arange(24).reshape((4, 6)) # take t Median less than 10 The modification of is 0 t[t < 10] = 0 print t #
The operation results are as follows : # [[0 0 0 0 0 0] # [0 0 0 0 10 11] # [12 13 14 15 16 17] # [18 19 20 21
22 23]]

9,numpy Ternary operator in :
t = np.arange(24).reshape((4, 6)) # take t Median less than 10 The modification of is 0, Otherwise, change to 10,numpy Ternary operator of t =
np.where(t < 10, 0, 10) print t # The operation results are as follows : ''' [[ 0 0 0 0 0 0] [ 0 0 0 0 10 10]
[10 10 10 10 10 10] [10 10 10 10 10 10]] '''

10,numpy Medium clip( Cutting ):
t = np.array([[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16,
17], [18, 19, 20, np.nan, np.nan, np.nan]]) # take t Median less than 10 The modification of is 10, Greater than or equal to 18 The modification of is 18 t =
np.clip(t, 10, 18) print t # The operation results are as follows : ''' [[10. 10. 10. 10. 10. 10.] [10. 10.
10. 10. 10. 11.] [12. 13. 14. 15. 16. 17.] [18. 18. 18. nan nan nan]] '''

11,np.nan and np.inf

nan:not a
number Indicates that it is not a number , When we read the local file as float When , If missing , Will appear . Or as an inappropriate calculation ( Infinity minus infinity ) The same is true ;

inf Indicates positive infinity ,-inf Represents negative infinity ;

a=np.inf #1, The type of infinity is :<type 'float'> print type(a) #2, nan The type of is :<type 'float'>
b=np.nan print type(b) #3, Two nan Are not equal ,False print np.nan == np.nan
#4, Take advantage of the above features , It can be judged that nan Number of ,1 t=np.array([1.2.np.nan]) print np.count_nonzero(t
!= t) # 5, If the data contains nan, Replace it with 0,[1. 2. 0.] t[np.isnan(t)]=0 print t

two ,ufunc(universal function object) It is a general term for functions that process arrays

1,numpy Statistical functions commonly used in

*
Sum :t.sum(axis=None)

*
mean value :t.mean(a,axis=None)

*
median :np.median(t,axis=None)

*
Maximum :t.max(axis=None)

*
minimum value :t.min(axis=None)

*
extremum :np.ptp(t,axis=None)

*
standard deviation :t.std(axis=None)

2, Other methods commonly used :

*
Gets the location of the maximum and minimum values :np.argmax(t,axis=0)  ;np.argmin(t,axis=1)

*
Create an all 0 Array of :np.zeros((3,4))

*
Create an all 1 Array of :np.ones((3,4))

*
Create a diagonal for 1 Array of :np.eye(3))

 

3, Functions of some random numbers

 
# np.random.rand() The argument to this function is the dimension of the array , The value range is [0,1] # 0.761440624168 print
np.random.rand() # [0.91673038] print np.random.rand(1) # [[0.3286804
0.94399189] # [0.99691882 0.51908634]] print np.random.rand(2, 2)
np.random.randn() np.random.randn(1) np.random.randn(2, 2) np.random.randint(1)
np.random.randint(1, 5) np.random.randint(1, 12, size=(3, 4))
np.random.uniform(1,12,size=(3,4)) np.random.normal(0,1,size=(3,4))

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage Python Study notes ( one )evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possibleRISC-V_GD32VF103-TIMER0 timer interrupt java Array subscript variable _Java Basic grammar : array be based on stm32 Control four-wheel trolley motor drive ( one ) be based on redis Design of liking function Software engineering career planning mysql Query random data by conditions _MySQL Random query of several qualified records centos7 install RabbitMq