import numpy as np arr=np.array([1,2,3,4,5]) arr array([1, 2, 3, 4, 5]) arr=np.
array(['a','b',3,4,5]) arr array(['a', 'b', '3', '4', '5'], dtype='<U1') list_1=
[i for i in range(100)] np.array(list_1) array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
<> Part I

<>1 Array inner function creation
#1. whole 1 array arr_1=np.ones(5) arr_1 array([1. 1. 1. 1. 1.]) #2. whole 0 array arr_0=np.
zeros(5) arr_0 array([0. 0. 0. 0. 0.]) #3. All for fill_value s Array of arr_full=np.full
(shape=[2,3],fill_value=123) arr_full array([[123, 123, 123], [123, 123, 123]])
#4. Equal search sequence （ Step jump ） arr_arange=np.arange(start=0,stop=10,step=3) arr_arange array([0,
3, 6, 9]) #5. Equipartition function arr_linspace=np.linspace(start=0,stop=9,num=11)arr_linspace
array([0. , 0.9, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9. ]) #6. Random integer
arr_random_int=np.random.randint(0,100,size=10)arr_random_int array([35, 83,
71, 28, 30, 15, 15, 56, 13, 56])
#7. Normal distribution number arr_random_θ=np.random.randn(20)arr_random_θ array([-0.26125824,
-1.10471905, 0.47196747, -0.45830774, -0.77667846, -0.19228595, -0.88741877,
0.78935227, 0.38761409, 0.11356884, -0.79957499, -0.42100158, 1.31760935,
0.83588336, 0.70025361, 0.96548828, -0.05750231, 1.00665188, -0.58256791,
-1.06221666]) #8. random number float
01 Range arr_random_float=np.random.random([2,3])arr_random_float array([[0.4602639
, 0.723614 , 0.75192543], [0.1464745 , 0.96532921, 0.86107633]]) #9. random number float
Arbitrary range arr_random_float_2=np.random.uniform(5,10,5) arr_random_float_2
array([6.41475055, 7.90246005, 6.81719872, 9.80708543, 7.85627567])
<>2. View actions
#1.2.1 Number of axes , dimension arr=np.random.randint(0,100,size=(4,2)) arr.ndim 2 #1.2.2
Shape and size arr=np.random.randint(0,100,size=(4,2,5))arr.shape (4, 2, 5) #1.2.3 Number of array elements
sizearr=np.random.randint(0,100,size=(3,4,5))arr.size 60
#1.2.4 data type arr=np.random.randint(0,100,size=(3,4,5),dtype='int64')arr.dtype
dtype('int64') #1.2.5 Size of each element in the array arr=np.random.randint(0,100,size=(3,4,5),dtype=
'int32') arr.itemsize 4
<>3. file io operation
#1.3.1 Save array x=np.random.randn(5) y=np.arange(0,10,1) z=np.linspace(0,100,80) np.
save("x_arr",x)# default .npy np.savez("all_arr",xarr=x,yarr=y,zarr=z) #1.3.2 Read save array np
.load("x_arr.npy") array([-2.48913545, 0.00585772, 2.21861779, 0.48855107,
-0.90451461]) np.load("all_arr.npz")['yarr'] array([0, 1, 2, 3, 4, 5, 6, 7, 8,
9]) #1.3.3 Read save array arr =np.random.randint(0,10,size=(3,4)) np.savetxt("arr.csv",arr
,delimiter=",")np.loadtxt("arr.csv",delimiter=",",dtype=np.int32) array([[5, 3,
0, 2], [2, 6, 0, 5], [4, 6, 7, 5]])
<> Part II data type

<>ndarray Data type for ：

* int: int8,uint8,int16,int32,int64
* float: float16,float32,float64
* str # 2.1 Create the specified data type np.array(np.arange(1,10,2),dtype='int8') array([1, 3, 5,
7, 9], dtype=int8) # 2.2 Specify the data type when converting arr
=list(range(20))np.asarray(arr,dtype='float32') array([ 0. 1. 2. 3. 4. 5.
6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.],
dtype=float32) # 2.3 Specify the data type when converting arr=np.random.randint(0,10,5,dtype='int32') arr.
dtype dtype('int32') arr=arr.astype('float64') arr.dtype dtype('float64')
<> Part III Array operation

<>3.1 Addition, subtraction, multiplication and division power operation
arr1=np.array(list(range(1,5))) arr2=np.array(list(range(2,6))) arr1-arr2
array([-1, -1, -1, -1]) arr1+arr2 array([3, 5, 7, 9]) arr2/arr1 array([2. , 1.5
, 1.33333333, 1.25 ]) arr2*arr1 array([ 2, 6, 12, 20]) arr2**arr1 array([ 2, 9,
64, 625], dtype=int32)
<>3.2 Logical operation > ,<,=,>=,<=
arr1<5 array([ True, True, True, True]) arr1>5 array([False, False, False,
False]) arr1==4 array([False, False, False, True]) arr1==arr2 array([False,
False, False, False])
<>3.3 Array and scalar computation
arr1=np.array([5,5,5,5]) arr2=np.array(list(range(5))) arr1+=5 arr1-=5 arr1*=5
# arr1/=5 No division, etc
<> Part IV Copy and view

<>4.1 Copy copy No replication at all

<> When manipulating arrays , Sometimes its data is copied to a new array , Sometimes not copied .

<> For beginners , This usually causes confusion . There are three situations
a=np.random.randint(0,100,[4,5])b=a a is b # return True a and b Two different names correspond to the same name ⼀ Memory objects True b[
0,0]=1024 #a And b Same location , Modified at the same time print(a[0,0],b[0,0]) 1024 1024 display(a,b)
array([[1024, 31, 42, 19, 68], [ 69, 67, 22, 7, 93], [ 77, 62, 20, 15, 25], [
4, 46, 88, 67, 80]]) array([[1024, 31, 42, 19, 68], [ 69, 67, 22, 7, 93], [ 77,
62, 20, 15, 25], [ 4, 46, 88, 67, 80]])
<>4.2 View or shallow copy
a=np.random.randint(0,100,[4,5]) b=a.view() # send ⽤a Data creation in ⼀ A new array object a is b# return False
a and b Two different names correspond to the same name ⼀ Memory objects False b.base is a # return False a and b Two different names correspond to the same name ⼀ Memory objects True b.flags
.owndata # return False b The data in is not its ⾃⼰ of False a.flags.owndata # return True a The data in is its ⾃⼰ of True a[
0,0]=1024 b[0,0] 1024 display(a,b) array([[1024, 86, 36, 96, 66], [ 17, 68, 11,
88, 26], [ 89, 39, 14, 21, 90], [ 49, 82, 46, 98, 3]]) array([[1024, 86, 36,
96, 66], [ 17, 68, 11, 88, 26], [ 89, 39, 14, 21, 90], [ 49, 82, 46, 98, 3]])
<>4.3 Deep copy
a=np.random.randint(0,100,[4,5])b=a.copy() b is a False b.base is a False b.
flags.owndata True a.flags.owndata True b[0,0]=1024 a[0,0] 52 display(a,b)
array([[52, 60, 16, 93, 64], [67, 36, 75, 82, 42], [86, 58, 17, 54, 22], [72,
14, 85, 92, 17]]) array([[1024, 60, 16, 93, 64], [ 67, 36, 75, 82, 42], [ 86,
58, 17, 54, 22], [ 72, 14, 85, 92, 17]])
*
copy The original array should no longer be needed , cut ⽚ Post tune ⽤. for example , hypothesis a yes ⼀ A giant ⼤ Intermediate results of ,⽽ final result b Include only ⼀⼩ part a, Then in b send ⽤ cut ⽚ enter ⾏ It shall be fabricated during construction ⼀ A deep copy ⻉：
a=np.arange(1e8)b=a[::1000000]del ab.shape (100,)
<> Part V Indexes section iteration

<>5.1 Basic index and slice
## 5.1 Basic index and slice arr=np.arange(10)arr array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) arr[5]
5 arr[5:8] array([5, 6, 7]) arr[5::2] array([5, 7, 9]) arr[::2] array([0, 2, 4,
6, 8]) arr[:7:2] array([0, 2, 4, 6]) arr[::-2] # Reverse order array([9, 7, 5, 3, 1]) arr[
::-1] array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) arr[0:3]=999 arr array([999, 999,
999, 3, 4, 5, 6, 7, 8, 9]) temp=arr[5:8] temp[1]=1034 arr array([ 999, 999,
999, 3, 4, 5, 1034, 7, 8, 9])
<>5.2 High dimensional indexing and slicing

* about ⼆ Dimension array or ⾼ Dimension group , We can index according to the previous knowledge , Of course, it can also be transmitted ⼊⼀ Comma separated indexes
* List to select one or more elements arr2d=np.array(list(range(12)))arr2d.shape=(3,4)arr2d array([[
0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) arr2d[0,-1]# The first digit represents the row , Binary representation column 3 arr2d[
0,2] 2 arr2d[:2,-2:] array([[2, 3], [6, 7]]) arr2d[:2,:-2] array([[0, 1], [4,
5]]) arr2d[:2,2:] array([[2, 3], [6, 7]]) arr2d[:2,:2] array([[0, 1], [4, 5]])
<>5.3 Fancy indexing and indexing techniques

* Integer array forward ⾏ Index is fancy index , Its sum and tangent ⽚ no ⼀ kind , It always copies data into a new array # One dimensional index arr1=np.arange(1,11)arr1 array([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) arr2=arr1[[1,3,3,5,7,7,7]]arr2 array([2, 4, 4,
6, 8, 8, 8]) arr2[-1]=1024# Not affect arr1arr2 array([ 2, 4, 4, 6, 8, 8, 1024]) arr1
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # Two dimensional index arr2d=np.arange(20)arr2d array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) arr2d.
shape=(4,5)arr2d array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13,
14], [15, 16, 17, 18, 19]]) arr2d[[1,3]]# Index No 1 Line and page 3 that 's ok array([[ 5, 6, 7, 8, 9],
[15, 16, 17, 18, 19]]) arr2d[[0,2]]# Index No 0 Line and page 2 that 's ok array([[ 0, 1, 2, 3, 4], [10,
11, 12, 13, 14]]) arr2d[([1,3],[2,4])]# Index No 1 Line and page 3 that 's ok And The first 2 Liehedi 4 column Intersection of
element , Equivalent to coordinates arr2d[3,4] and arr2d[1,2] array([ 7, 19]) arr2d[1,2] 7 arr2d[3,4] 19 #
Select an area to output arr2d[np.ix_([1,3,3,3],[2,4,4])] array([[ 7, 9, 9], [17, 19, 19], [17,
19, 19], [17, 19, 19]]) # amount to arr2d[[1,3,3,3]][:,[2,4,4]] array([[ 7, 9, 9], [17,
19, 19], [17, 19, 19], [17, 19, 19]]) # Also equivalent to arr2d_tmp=arr2d[[1,3,3,3]]#
arr2d_tmparr2d_tmp[:,[2,4,4]] array([[ 7, 9, 9], [17, 19, 19], [17, 19, 19],
[17, 19, 19]]) # ix_() Function can ⽤ To combine different vectors # The first ⼀ A list stores the elements to be extracted ⾏ mark , The first ⼆ A list stores the columns of the elements to be extracted
<>5.4 boolean Value index （ Boolean index ）
# names = #
np.array(['softpo','Brandon','Will','Michael','Will','Ella','Daniel','softpo','
# Will','Brandon']) # cond1 = names == 'Will' # cond1 # # output array([False,
False, True, False, True, False, False, False, True, # False]) # names[cond1] #
array(['Will', 'Will', 'Will'], dtype='<U7') # arr =
np.random.randint(0,100,size = (10,8)) # 0~100 random number # cond2 = arr > 90 # #
Find all ⼤ to 90 Index of , return boolean Array of type shape(10,8),⼤ Yu return True, otherwise False # arr[cond2] #
All the returned data are ⼤ to 90 of names =np.array(['softpo','Brandon','Will','Michael','Will','Ella',
'Daniel','softpo','Will','Brandon']) names array(['softpo', 'Brandon', 'Will',
'Michael', 'Will', 'Ella', 'Daniel', 'softpo', 'Will', 'Brandon'], dtype='<U7')
cond1= names == 'Will'cond1 array([False, False, True, False, True, False,
False, False, True, False]) names[cond1]# Index is True Corresponding element of array(['Will', 'Will',
'Will'], dtype='<U7') arr=np.random.randint(0,100,size=[10,8])arr array([[81,
59, 92, 17, 87, 49, 89, 45], [ 3, 1, 12, 17, 54, 68, 11, 11], [35, 86, 20, 44,
38, 80, 39, 52], [80, 5, 50, 16, 20, 78, 94, 91], [90, 68, 25, 35, 27, 67, 43,
44], [23, 54, 45, 75, 14, 10, 21, 72], [84, 26, 63, 50, 56, 3, 67, 90], [98,
15, 25, 96, 58, 44, 9, 19], [35, 75, 46, 79, 8, 94, 83, 88], [ 8, 52, 68, 69,
72, 71, 31, 88]]) arr_bool=arr>90arr_bool array([[False, False, True, False,
False, False, False, False], [False, False, False, False, False, False, False,
False], [False, False, False, False, False, False, False, False], [False,
False, False, False, False, False, True, True], [False, False, False, False,
False, False, False, False], [False, False, False, False, False, False, False,
False], [False, False, False, False, False, False, False, False], [ True,
False, False, True, False, False, False, False], [False, False, False, False,
False, True, False, False], [False, False, False, False, False, False, False,
False]]) arr[arr_bool] array([92, 94, 91, 98, 96, 94])
<> Part VI Shape operation

<>6.1 Array deformation
arr1=np.random.randint(1,100,size=(3,4,5))arr1 array([[[68, 40, 48, 91, 49],
[96, 37, 30, 75, 78], [79, 74, 75, 26, 51], [15, 45, 20, 59, 54]], [[73, 5, 71,
88, 62], [16, 6, 36, 49, 89], [40, 7, 42, 34, 97], [62, 87, 82, 70, 96]], [[94,
85, 62, 11, 26], [ 2, 76, 43, 9, 40], [21, 23, 99, 36, 78], [69, 71, 30, 11,
98]]]) arr1.reshape(12,5) # Modify shape array([[68, 40, 48, 91, 49], [96, 37, 30, 75,
78], [79, 74, 75, 26, 51], [15, 45, 20, 59, 54], [73, 5, 71, 88, 62], [16, 6,
36, 49, 89], [40, 7, 42, 34, 97], [62, 87, 82, 70, 96], [94, 85, 62, 11, 26], [
2, 76, 43, 9, 40], [21, 23, 99, 36, 78], [69, 71, 30, 11, 98]]) arr1.reshape(-1,
5)# Values that are negative are automatically assigned the corresponding number of rows or columns array([[68, 40, 48, 91, 49], [96, 37, 30, 75, 78], [79,
74, 75, 26, 51], [15, 45, 20, 59, 54], [73, 5, 71, 88, 62], [16, 6, 36, 49,
89], [40, 7, 42, 34, 97], [62, 87, 82, 70, 96], [94, 85, 62, 11, 26], [ 2, 76,
43, 9, 40], [21, 23, 99, 36, 78], [69, 71, 30, 11, 98]]) arr1.reshape(6,-1)#
Values that are negative are automatically assigned the corresponding number of rows or columns array([[68, 40, 48, 91, 49, 96, 37, 30, 75, 78], [79, 74,
75, 26, 51, 15, 45, 20, 59, 54], [73, 5, 71, 88, 62, 16, 6, 36, 49, 89], [40,
7, 42, 34, 97, 62, 87, 82, 70, 96], [94, 85, 62, 11, 26, 2, 76, 43, 9, 40],
[21, 23, 99, 36, 78, 69, 71, 30, 11, 98]])
<>6.2 Array conversion
arr1=np.random.randint(1,100,size=(3,5))arr1 array([[15, 31, 44, 13, 74], [54,
82, 18, 33, 56], [76, 84, 71, 23, 29]]) arr1.T array([[15, 54, 76], [31, 82,
84], [44, 18, 71], [13, 33, 23], [74, 56, 29]]) arr2=np.random.randint(1,100,
size=(3,4,5,6))arr2 array([[[[89, 5, 9, 23, 15, 45], [83, 72, 90, 98, 14, 54],
[41, 16, 31, 28, 25, 28], [ 6, 48, 8, 62, 69, 4], [36, 1, 63, 52, 40, 17]],
[[22, 19, 56, 69, 75, 30], [43, 7, 3, 76, 15, 79], [13, 45, 78, 19, 81, 77],
[60, 51, 81, 40, 48, 65], [99, 79, 74, 52, 4, 27]], [[73, 12, 54, 42, 67, 43],
[81, 78, 35, 71, 80, 66], [90, 43, 2, 24, 55, 3], [98, 56, 76, 15, 65, 5], [64,
85, 92, 22, 39, 79]], [[55, 24, 54, 6, 35, 42], [88, 84, 46, 15, 30, 83], [ 9,
62, 55, 81, 65, 75], [14, 99, 34, 75, 63, 53], [19, 39, 61, 88, 95, 6]]],

[[[57, 37, 14, 32, 79, 88], [ 1, 32, 80, 8, 52, 86], [42, 38, 11, 75, 56,
22], [70, 43, 24, 14, 77, 26], [75, 68, 1, 94, 98, 88]], [[99, 73, 68, 14, 48,
5], [29, 52, 72, 99, 10, 16], [21, 86, 85, 15, 29, 43], [87, 52, 83, 6, 59,
83], [51, 61, 67, 7, 14, 50]], [[ 4, 77, 14, 45, 13, 9], [58, 6, 24, 59, 88,
92], [11, 72, 35, 60, 94, 37], [59, 94, 67, 92, 75, 84], [77, 23, 39, 77, 67,
14]], [[63, 8, 16, 69, 16, 74], [34, 61, 64, 72, 15, 15], [33, 62, 49, 37, 64,
84], [94, 22, 9, 4, 56, 48], [53, 16, 14, 61, 56, 63]]],

[[[28, 90, 96, 64, 61, 15], [41, 99, 61, 83, 37, 14], [97, 92, 99, 16, 72,
46], [42, 30, 83, 83, 15, 31], [52, 98, 16, 98, 90, 31]], [[37, 35, 37, 66, 97,
6], [18, 29, 14, 26, 96, 11], [78, 40, 75, 41, 17, 15], [77, 82, 29, 87, 65,
30], [56, 69, 40, 90, 67, 2]], [[14, 43, 51, 60, 52, 91], [66, 45, 11, 74, 11,
2], [25, 44, 72, 6, 13, 22], [63, 24, 90, 86, 43, 3], [78, 29, 36, 18, 94,
10]], [[56, 64, 86, 32, 76, 90], [61, 42, 25, 98, 3, 32], [ 6, 51, 25, 60, 36,
68], [19, 24, 51, 20, 35, 27], [22, 21, 85, 17, 82, 30]]]]) arr3=arr2.T arr3.
shape (6, 5, 4, 3) arr2.shape (3, 4, 5, 6) # transpose Change array dimension
Sort the original array shapes , Then use the index to exchange the order arr4=np.transpose(arr2,axes=(3,0,2,1)) arr4.shape (6, 3,
5, 4)
<>6.3 Array stack
arr1=np.array([range(1,4)])arr2=np.array([range(4,7)]) arr1 array([[1, 2, 3]])
np.concatenate([arr1,arr2],axis=0) # memory 0 Represents that merging below is equivalent to union,1 Represents merging on the right, equivalent to right join
array([[1, 2, 3], [4, 5, 6]]) np.concatenate([arr1,arr2],axis=1) # memory
0 Represents that merging below is equivalent to union,1 Represents merging on the right, equivalent to right join array([[1, 2, 3, 4, 5, 6]]) np.hstack((
arr1,arr2)) # Horizontal stacking array([[1, 2, 3, 4, 5, 6]]) np.vstack((arr1,arr2)) # Vertical stacking
array([[1, 2, 3], [4, 5, 6]])
<>6.4 Array splitting split
import numpy as pd arr = np.random.randint(0,10,size=(6,5))np.split(arr,
indices_or_sections=2,axis=0) [array([[6, 7, 3, 3, 2], [1, 5, 8, 5, 4], [6, 0,
7, 6, 9]]), array([[2, 1, 2, 3, 3], [0, 4, 5, 4, 7], [9, 9, 2, 1, 4]])] np.split
(arr,indices_or_sections=2,axis=0)# Vertical segmentation Ensure vertical （ Number of rows ） Even number [array([[6, 7, 3, 3, 2],
[1, 5, 8, 5, 4], [6, 0, 7, 6, 9]]), array([[2, 1, 2, 3, 3], [0, 4, 5, 4, 7],
[9, 9, 2, 1, 4]])] np.split(arr,indices_or_sections=[2,],axis=1)# Transverse cutting direction segmentation
Ensure lateral （ Number of columns ） Even number , Otherwise incoming indices_or_sections=[2,],2 Is the second column , Divide into two when left blank [array([[6, 7], [1, 5],
[6, 0], [2, 1], [0, 4], [9, 9]]), array([[3, 3, 2], [8, 5, 4], [7, 6, 9], [2,
3, 3], [5, 4, 7], [2, 1, 4]])] np.split(arr,indices_or_sections=[2,3],axis=1)#
Transverse cutting direction segmentation Ensure lateral （ Number of columns ） Even number , Otherwise, it can be passed in indices_or_sections=[2,3],2 Is the second column ,3 Is the third column ,#
So the array is divided into 1,2 column ,3 column , To last column [array([[6, 7], [1, 5], [6, 0], [2, 1], [0, 4], [9,
9]]), array([[3], [8], [7], [2], [5], [2]]), array([[3, 2], [5, 4], [6, 9], [3,
3], [4, 7], [1, 4]])] arr array([[6, 7, 3, 3, 2], [1, 5, 8, 5, 4], [6, 0, 7, 6,
9], [2, 1, 2, 3, 3], [0, 4, 5, 4, 7], [9, 9, 2, 1, 4]]) np.vsplit(arr,
indices_or_sections=3)# It is evenly divided into three parts in the vertical direction [array([[6, 7, 3, 3, 2], [1, 5, 8, 5,
4]]), array([[6, 0, 7, 6, 9], [2, 1, 2, 3, 3]]), array([[0, 4, 5, 4, 7], [9, 9,
2, 1, 4]])] np.hsplit(arr,indices_or_sections=[1,4])#
Index horizontally 1,4 Divide the breakpoint into three parts （ Index here from 1 start , Not from 0 start ） [array([[6], [1], [6], [2], [0], [9]]),
array([[7, 3, 3], [5, 8, 5], [0, 7, 6], [1, 2, 3], [4, 5, 4], [9, 2, 1]]),
array([[2], [4], [9], [3], [7], [4]])]
<> Part VII Broadcasting mechanism

* When the shapes of two arrays are different , We can add, subtract and multiply by extending the array （ except ） Other operations , This mechanism is called broadcast mechanism
<>7.1 Sorting rules np.sort(array,[axis=1,0])
import numpy as np arr1=np.sort(np.array([1,2,3,4]*3)).reshape(4,3) arr2=np.
sort(np.array([1,2,3,4]*3)).reshape(4,3) arr2 array([[1, 1, 1], [2, 2, 2], [3,
3, 3], [4, 4, 4]]) #2 np.sort(arr1,axis=0) array([[1, 1, 1], [2, 2, 2], [3, 3,
3], [4, 4, 4]]) arr1 array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) arr3=np
.sort(arr1,axis=1) arr3 array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) np.
sort(arr3,axis=0) array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) arr1
array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) ## default axis = 1 Sort by row
Arrange horizontally from small to large , arr2=np.sort(np.random.randint(1,10,(4,3))) arr2 ## Arranged vertically from small to large , np.
sort(arr2,axis=0) ## Arrange horizontally from small to large , np.sort(arr2,axis=1) array([[1, 5, 8], [3, 8,
9], [6, 8, 9], [1, 1, 2]])
<>7.2 One dimensional array broadcasting
arr1=np.sort(np.array([1,2,3,4]*3)).reshape(4,3) arr2=np.array([1,2,3]) arr3=
arr1+arr3 arr3 array([[2, 2, 2], [4, 4, 4], [6, 6, 6], [8, 8, 8]])

[ External chain picture transfer failed , The origin station may have anti-theft chain mechanism , It is recommended to save the picture and upload it directly (img-A96rZFV5-1638201973086)(attachment:image.png)]

<>7.3 Two dimensional array broadcasting
arr1=np.sort(np.array([0,1,2,3,]*3)).reshape(4,3) arr2=np.array([[1],[2],[3],[4
]]) arr3=arr1+arr2 arr3 array([[1, 1, 1], [3, 3, 3], [5, 5, 5], [7, 7, 7]])

[ External chain picture transfer failed , The origin station may have anti-theft chain mechanism , It is recommended to save the picture and upload it directly (img-OVQABGWd-1638201973089)(attachment:image.png)]

<>7.4 3D array ⼴ Broadcast
import numpy as np arr1 = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2)
#shape(3,4,2) arr2 = np.array([0,1,2,3,4,5,6,7]).reshape(4,2) #shape(4,2) arr3 =
arr1+ arr2 # arr2 Array in 0 Dimensional replication 3 share shape(3,4,2) arr3 array([[[ 0, 2], [ 4, 6], [ 8,
10], [12, 14]], [[ 0, 2], [ 4, 6], [ 8, 10], [12, 14]], [[ 0, 2], [ 4, 6], [ 8,
10], [12, 14]]]) arr1 array([[[0, 1], [2, 3], [4, 5], [6, 7]], [[0, 1], [2, 3],
[4, 5], [6, 7]], [[0, 1], [2, 3], [4, 5], [6, 7]]]) arr2 array([[0, 1], [2, 3],
[4, 5], [6, 7]])

[ External chain picture transfer failed , The origin station may have anti-theft chain mechanism , It is recommended to save the picture and upload it directly (img-Gt2kj621-1638201973092)(attachment:image.png)]

[ External chain picture transfer failed , The origin station may have anti-theft chain mechanism , It is recommended to save the picture and upload it directly (img-EYQjkrZn-1638201973094)(attachment:image.png)]
# Dimensions can only be from z Axis direction operation broadcast import numpy as np arr1 = np.array([0,1,2,3,4,5,6,7]*3).reshape
(3,4,2) #shape(3,4,2) arr2 = np.arange(0,12).reshape(3,4) #shape(,2) # arr3 =
arr1 + arr2 # arr2 Array in 0 Dimensional replication 3 share shape(3,4,2) # arr3
<> Part VIII General function

<>8.1 General function ： Element level numeric function

*
abs,sqrt,square,exp,log,sin,cos,tan,maxinmum,minimum,all,any,inner,clip,round,trace,ceil,floor
import numpy as np arr=np.arange(1,10) np.sqrt(arr)# Open square np.square(arr)# square np.
clip(arr,2,9)## Capping method x=np.array([1,5,2,9,3,6,8]) y=np.array([2,4,3,7,1,9,0]) np.
maximum(x,y)# Returns the maximum value at the same location of two sets of data arr2=np.arange(0,25).reshape(5,5) np.inner(arr2[0],
arr2)# Returns the inner product of a one-dimensional array vector print(" Array maximum ：%s"%(np.max(arr))) print(" Array minimum ：%s"%(np.min(arr))
) print(" with e Find for cardinality e Array power of %s"%np.exp(arr)) print(" with 10 Logarithm array for low %s"%np.log(arr)) print(
" Prescription %s"%np.sqrt(arr)) print(" average value %s"%np.mean(arr)) print(" standard deviation %s"%np.std(arr))
print(" Minimum %s"%np.argmin(arr)) print(" maximum value %s"%np.argmax(arr)) print(" Judgment limit value ：%s"%np.
isinf(arr)) Array maximum ：9 Array minimum ：1 with e Find for cardinality e Array power of [2.71828183e+00 7.38905610e+00
2.00855369e+01 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03
2.98095799e+03 8.10308393e+03] with 10 Logarithm array for low [0. 0.69314718 1.09861229 1.38629436
1.60943791 1.79175947 1.94591015 2.07944154 2.19722458] Prescription [1. 1.41421356
1.73205081 2. 2.23606798 2.44948974 2.64575131 2.82842712 3. ] average value 5.0
standard deviation 2.581988897471611 Minimum 0 maximum value 8 Judgment limit value ：[False False False False False False False
False False]
<>8.2 where function
import numpy as np arr1=np.array([1,2,5,7,9]) arr2 = np.array([2,4,6,8,10])
bool_v=np.array([True,False,True,True,True]) np.where(bool_v,arr1,arr2) np.where
(arr1<5,arr1,-16) array([ 1, 2, -16, -16, -16])
<>8.3 Sorting method

* arr.sort(),np.sort(),arr.argsort() import numpy as np arr =np.array([9,3,11,
6,17,5,4,15,1]) arr.sort()# Directly change the original array np.sort(arr)# Return deep copy results array([ 1, 3, 4, 5,
6, 9, 11, 15, 17]) arr =np.array([9,3,11,6,17,5,4,15,1]) arr.argsort()#
Returns the sort index from small to large array([8, 1, 6, 5, 3, 0, 2, 7, 4], dtype=int64)
<>8.4 Set operation function
a=np.arange(2,10,2) b=np.arange(3,7) np.intersect1d(a,b)# intersection np.union1d(a,b)# Union
np.setdiff1d(a,b)# Difference set a have b Element without array([2, 8])
<>8.5 Set operation function
import numpy as np arr1=np.arange(0,50) arr1.min() arr1.max() np.argmax(arr1)
# Returns the maximum index np.argwhere(arr1>20)# The return value meets the conditions （ greater than 20） Index of np.cumsum(arr1)# Cumulative calculation arr2=np.
arange(0,50).reshape(5,10) arr2.mean(axis=0)# Calculate the average of the columns arr2.mean(axis=1)# Calculate the average of rows
np.cov(arr2,rowvar=True)# covariance matrix np.corrcoef(arr2,rowvar=True)# correlation coefficient array([[1.
1. 1. 1. 1.], [1. 1. 1. 1. 1.], [1. 1. 1. 1. 1.], [1. 1. 1. 1.
1.], [1. 1. 1. 1. 1.]]) arr2.mean(axis=0) array([20. 21. 22. 23. 24.
25. 26. 27. 28. 29.]) np.cov(arr2,rowvar=True)# array([[9.16666667,
9.16666667, 9.16666667, 9.16666667, 9.16666667], [9.16666667, 9.16666667,
9.16666667, 9.16666667, 9.16666667], [9.16666667, 9.16666667, 9.16666667,
9.16666667, 9.16666667], [9.16666667, 9.16666667, 9.16666667, 9.16666667,
9.16666667], [9.16666667, 9.16666667, 9.16666667, 9.16666667, 9.16666667]])
<>9 linear algebra
a=np.array([[4,2,3],[1,3,1]]) b=np.array([[2,7],[-5,-7],[9,3]]) a.dot(b)# Matrix operation
a The last dimensional requirements and b The first dimension of is equal np.dot(a,b)# a@b # abbreviation array([[ 25, 23], [ -4, -11]]) # Other operations of matrix
from numpy.linalg import inv,det,eig,qr,svd a=np.array([[1,2,3], [2,3,4], [4,5,8
] ]) b=inv(a) a.dot(b) array([[ 1.00000000e+00, 2.22044605e-16,
-5.55111512e-17], [ 4.44089210e-16, 1.00000000e+00, 2.22044605e-16], [
0.00000000e+00, -1.77635684e-15, 1.00000000e+00]])

Technology
Daily Recommendation
views 1