1. Creation of numpy arrays
1.1 array function to create arrays
The simplest and most intuitive way is to create a numpy array directly using the array function.
import numpy as np ndarray = np.array([1, 2, 3, 4]) print(ndarray) # [1 2 3 4] ndarray = np.array(list('abcdefg')) print(ndarray) # ['a' 'b' 'c' 'd' 'e' 'f' 'g'] ndarray = np.array([[11, 22, 33, 44], [10, 20, 30, 40]]) print(ndarray) ''' [[11 22 33 44] [10 20 30 40]] '''
1.2 asarray function to create arrays
asarray converts the input to ndarray and does not copy if the input itself is an ndarray.
When the data source is ndarray, the difference between array and asarray is that array still copies out a copy, taking up new memory, but asarray does not.
Asrray works the same as array when the data source is of another type.
import numpy as np a=np.random.random((3,3)) b=np.array(a,dtype='float64') c=np.asarray(a,dtype='float64') a[2]=2 print(a) ''' [[0.4593994 0.94093065 0.25819247] [0.29774257 0.32810293 0.31841724] [2. 2. 2. ]] ''' print(b) ''' [[0.4593994 0.94093065 0.25819247] [0.29774257 0.32810293 0.31841724] [0.45421249 0.85030328 0.98043655]] ''' print(c) ''' [[0.4593994 0.94093065 0.25819247] [0.29774257 0.32810293 0.31841724] [2. 2. 2. ]] '''
1.3 zeros and zeros_like to create an array
- Zeros: means to create an array of all zeros directly
- zeros_like: Create an array with all elements 0 based on the shape of the incoming ndarray array
import numpy as np ndarray = np.zeros(10) print(ndarray) # [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] ndarray = np.zeros((2, 2)) print(ndarray) ''' [[0. 0.] [0. 0.]] ''' ndarray = np.array([[11, 22, 33, 44], [10, 20, 30, 40]]) ndarray1 = np.zeros_like(ndarray) print(ndarray1) ''' [[0 0 0 0] [0 0 0 0]] '''
1.4 ones and ones_like to create an array
Used to create an array with all elements 1.ones_like usage is the same as zeros_like usage
import numpy as np ndarray = np.ones(10) print(ndarray) # [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] ndarray = np.ones((2, 2)) print(ndarray) ''' [[1. 1.] [1. 1.]] ''' ndarray = np.array([[11, 22, 33, 44], [10, 20, 30, 40]]) ndarray1 = np.ones_like(ndarray) print(ndarray1) ''' [[1 1 1 1] [1 1 1 1]] '''
1.5 empty and empty_like to create an array
To create an empty array, the values in the empty data are not zero, but uninitialized random values.
import numpy as np ndarray = np.empty(3) print(ndarray) # [1.96589195e-316 0.00000000e+000 1.58101007e-322] ndarray = np.empty((2, 2)) print(ndarray) ''' [[1.9638129e-316 0.0000000e+000] [7.9050503e-323 7.9050503e-323]] ''' ndarray = np.array([[11, 22, 33, 44], [10, 20, 30, 40]]) ndarray1 = np.empty_like(ndarray) print(ndarray1) ''' [[ 0 0 139955131091248 139957403317784] [139957403322480 139955459383168 139955130963144 139955459420264]] '''
1.6 arange Create Array
The arange function is an array version of the python built-in function range function.
import numpy as np ndarray = np.arange(10) print(ndarray) # [0 1 2 3 4 5 6 7 8 9] ndarray = np.arange(10,20) print(ndarray) # [10 11 12 13 14 15 16 17 18 19] ndarray = np.arange(10,20,2) print(ndarray) # [10 12 14 16 18]
1.7 eye, identity create diagonal array
Both eye and identity can produce arrays with some elements of 1 and the rest of 0. The difference is that identity can only create square arrays with a diagonal of 1, while eye can not only create square arrays, but also specify an offset of 1. The differences are as follows.
np.identity(n, dtype=None) #k = 0 centered, 1 offset 1 up, 2 offset 2, and so on, -1 offset down np.eye(N, M=None, k=0, dtype=<type 'float'>)
Code examples:
import numpy as np ndarray = np.identity(4) print(ndarray) ''' [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] ''' ndarray = np.eye(4,k=1) #Position of 1 is offset up by 1 print(ndarray) ''' [[0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.] [0. 0. 0. 0.]] '''
2. Index and Slice
The index order and the list order are consistent, as shown in the following figure.
2.1 One-Dimensional Array
One-dimensional arrays are simple and essentially the same as lists. The difference is that the array slice is the original array view and the list is a shallow copy. (This means that if you make any changes to the array, the original will change as well), which also means that if you don't want to change the original array, we need to make an explicit copy to get a copy of it (.copy()).
import numpy as np arr = np.arange(10) # [0 1 2 3 4 5 6 7 8 9] # Index and Slice print(arr[4]) #Index; value== 4 print(arr[3:6])#Slice: =="[34 5] # copy arr_old_nocopy = arr # [0 1 2 3 4 5 6 7 8 9] arr_old_copy = arr.copy() # [0 1 2 3 4 5 6 7 8 9] arr[3:6] = 33 #Update Value print(arr) # [ 0 1 2 33 33 33 6 7 8 9] print(arr_old_copy) # [0 1 2 3 4 5 6 7 8 9] print(arr_old_nocopy) # [ 0 1 2 33 33 33 6 7 8 9]
2.2-D Array
In a two-dimensional array, if there is only one element in [], then it takes a one-dimensional array. If you want to take a value, you need to use two [] or separate rows and columns with commas.
import numpy as np arr = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]]) #Index and Slice #Index Row print(arr[0]) #Index row=="[1 2 3] print(arr[0,:]) #Index row=="[1 2 3] print(arr[0,...]) #Index row=="[1 2 3] #Index Column print(arr[:,1]) #Index Column=="[258] print(arr[... , 1]) #Index Column=="[258] #Get a two-dimensional array print(arr[0:2,1:2])# Note left closed right open ''' [[2] [5]] ''' # Set index for step size print(arr[::2,::2]) #Set step size to 2 ''' [[1 3] [7 9]] ''' #Value print(arr[1,2]) #Index Value=="6 print(arr[1][2]) #Index Value=="6
2.3 Multidimensional Array
This is the same as a two-dimensional array, except it is a multidimensional array.
import numpy as np arr = np.arange(12) arr = arr.reshape(2,2,3) #Change arr to 2 × 2 × 3 Array #print(arr) ''' [[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]]] ''' print(arr[0]) ''' Indexes==> [[0 1 2] [3 4 5]] ''' #Value print(arr[1,0,1]) #Index Value=="7 print(arr[1][0][1]) #Index Value=="7 print(arr[0,1]) # Index=="[34 5] arr[0] = 100 #assignment print(arr) ''' [[[100 100 100] [100 100 100]] [[ 6 7 8] [ 9 10 11]]] '''
2.4 Boolean Index
Let's start with a column:
import numpy as np arr = (np.arange(36)).reshape(6,6)#Generate an array of 6*6 print(arr) ''' [[ 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]] ''' x = np.array([0, 1, 2, 1, 4, 5]) arr1 = np.array(x==1) print(arr1) # [False True False True False False] print(arr[x==1])#Rows Removed as True ''' [[ 6 7 8 9 10 11] [18 19 20 21 22 23]] '''
- As a result, the Boolean index takes rows with a Boolean value of True.
- The length of the Boolean array and the number of rows (axis length) of the indexed array must be the same.
- Boolean arrays can be used with slices, integers (sequences of integers).
import numpy as np arr = (np.arange(36)).reshape(6,6)#Generate an array of 6*6 x = np.array([0, 1, 2, 1, 4, 5]) print(arr[x == 1,2:]) #Slice: Remove rows equal to 1 with columns from index 2 to the end ''' [[ 8 9 10 11] [20 21 22 23]] ''' print(arr[x == 1,-3:]) # Slice: Take out a row equal to 1 with columns from -3 to the end ''' [[ 9 10 11] [21 22 23]] ''' print(arr[x==1,3])#Slice: Take out a row equal to 1 and list it as 3 =="[9 21]
Similarly, you can index and slice using operations that are not equal to, greater than, or less than.
2.5 Fancy Index
Fancy indexing refers to indexing with an array of integers.
- Pass in a single list or array
import numpy as np arr = np.empty((8,4))# Create a new array, allocate only memory space, not fill values for i in range(8):#Starting at 0, each row increments arr[i] = i print(arr[[2,6,1]]) #Use a list to get the rows you want #This order is the list of integers or ndarray that we passed in. ''' [[2. 2. 2. 2.] [6. 6. 6. 6.] [1. 1. 1. 1.]] ''' print(arr[[-2,-6,-1]]) #Negative Index ''' [[6. 6. 6. 6.] [2. 2. 2. 2.] [7. 7. 7. 7.]] '''
- Incoming multiple index arrays
Returns a one-dimensional array with elements corresponding to each index element.
import numpy as np arr = np.arange(35).reshape(5,7)#Generate an array of 5*7 print(arr) ''' [[ 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]] ''' print(arr[[1,3,2,4],[2,0,6,5]]) # [ 9 21 20 33] # Actually it is (1,2), (3,0)...
The same is true for fancy indexes of multidimensional arrays.