numpy's knowledge is divided into the following eight parts
All code runs in jupyter
out:Following is the result of running the output
Catalog:
1. Arnge and array functions create arrays
2. The shape function returns the dimensions of the array (returned as tuples)
3. Array slices
4. Changing the dimensions of arrays
5. Combination of arrays
6. Split Array
7. Attributes of arrays
8. Array Conversion
Most importantly, import the numpy package first
import numpy as np
1. Arnge and array functions create arrays
There are two ways to create an array using numpy
(1)
numpy.arange([start, ]stop, [step, ]dtype=None)
Detailed parameters:
Start, stop ----> Array start and stop position, tuple form, follow the principle of left-closed right-open, start omit defaults to 0
Step ----> The step of an array of equal differences, defaulting to 1
Dtype ---->Specify data type
Only one-dimensional arrays can be created and their dimensions can be changed later by other means
# Do not specify start a = np.arange(5) a # out: array([0, 1, 2, 3, 4]) -------------- # Specify start a = np.arange(2, 5) a # out: array([2, 3, 4]) -------------- # Specify step size a = np.arange(2, 10, 2) # Specify with start parameter a # out: array([2, 4, 6, 8]) a = np.arange(10, step = 2) # Specify without start parameter a # out: array([0, 2, 4, 6, 8])
(2)
numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
Detailed parameters:
Dtype ---->Specify data type
You can create one or more dimensional arrays
# Create a one-dimensional array a = np.array([1, 2, 3, 4]) a # out: array([1, 2, 3, 4]) ------ # Create a two-dimensional array a = np.array([[1, 2], [3, 4]]) a ''' out: array([[1, 2], [3, 4]]) ''' ------ # Specify data type a = np.array([1, 2, 3, 4], dtype = 'str') a # out: array(['1', '2', '3', '4'], dtype='<U1') # You can see that all elements in the array change to look at the string type
2. The shape function returns the dimensions of the array (returned as tuples)
Shapes are primarily used to view the dimensions and shape s of arrays
Shapes can also change the dimension and shape of arrays
# View the dimensions and shapes of array a with shapes a = np.array([[0,1,2,3,4],[1,3,2,3,4]]) a.shape # out: (2, 5) # You can see that array A is a two-dimensional array ------ # Changing the dimensions and shape of an array with shapes a = np.arange(8) # First create a one-dimensional array a a # out: array([0, 1, 2, 3, 4, 5, 6, 7]) a.shape # Shape of the array # out: (8,) # Then use shapes to change a's shape a.shape = (2, 4) a # View array a again ''' out: array([[0, 1, 2, 3], [4, 5, 6, 7]]) ''' a.shape # View the shape of a again # out: (2, 4) # You can see that array a has changed somewhat, but it has the same number of elements and content
3. Array slices
Array slices mainly take a part of the array, following the principle of left-closed right-open
It was also found that setting the step to -1 also reverses the order of array elements, but this function is not available in the arange function to create arrays, only an empty array can be created.
And "..."Ellipsis in Multidimensional Arrays
# Slices of a one-dimensional array a = np.arange(9) a[3:7] # 3 and 7 are the index values of the array elements, which means getting the fourth to the seventh elements of the array # This operation returns a new array without changing the original array # If you need to change the original array, you can use a = a[3:7] # out: array([3, 4, 5, 6]) ------ # Slices of multidimensional arrays b = np.arange(24).reshape(2, 3, 4) # reshape is used to change the shape and dimension of an array, which will be described later b ''' out: 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]]]) ''' # Select one of the elements b[0,0,0] # out: 0 # Select a part of it b[:,0,0] # ":" denotes the 0 th element of the first element of each selected element # The element here refers not only to a number, but also to a list contained in an array. ''' out: array([ 0, 12]) ''' # Slicing a multidimensional array in a more complex way b[:,0,1:3] # Here, 1:3 means slicing the list of corresponding layers ''' out: array([[ 1, 2], [13, 14]]) ''' ------ # The Use of the'...'Ellipsis in Multidimensional Arrays b[..., 1] # Equivalent to b[:,:, 1], continuous: can be replaced by an ellipsis... ''' out: array([[ 1, 5, 9], [13, 17, 21]]) ''' b[1,...] # Equivalent to b[1,:,] ''' out: array([[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]) ''' ------ # How to Reverse Arrays # Look at the original array first b ''' out: 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]]]) ''' b[::-1] # Reverse outermost list # Note that the quantities':'here are not always well separated. The first':' represents selecting the entire array as a slice. # The second':'is used to separate the last parameter, the whole parameter is the step, and when the step is 1, it reverses the order of the entire array ''' out: array([[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]], [[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]) '''
4. Changing the dimensions of arrays
There are a number of functions that can change the dimension of an array, each with different functions
There are:
reshape function
ravel function
flatten function
shape function
transpose function
resize function
Next, use the code to see how each should work
# reshape changes the dimension of the array, but does not change the original array b = np.arange(24).reshape(2, 3, 4) # Dimensions of arrays can be set directly. # But one thing to note is that: # The product of each dimension should be the same as the minimum number of unit elements in the array, e.g. 24 = 2*3*4 b ''' out: 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]]]) ''' ------ # The ravel function flattens an array by collecting all its elements into a one-dimensional array b.ravel() # Will not change the original array ''' out: 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]) ''' ------ # Like the ravel function, it flattens an array, but ravel simply returns a view of the array, and flatten requests to allocate memory to hold the results b.flatten() # Will not change the original array ''' out: 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]) ''' ------ # Above we know that shapes are used to view the shape and dimension of arrays, but they can also change the shape and dimension of arrays b.shape = (6, 4) # Dimensions of b arrays set directly as tuples # Will modify the original array directly b ''' out: 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]]) ''' ------ # In the case where the b-array is changed in dimension by the shape, we continue to use the transpose function to manipulate the array # The transpose function is used to transpose a matrix: each column of a multidimensional array becomes each row, and each row becomes each column. # Equivalent to flipping a rectangle 90 degrees b.transpose() # Transpose the matrix without changing the original array ''' out: array([[ 0, 4, 8, 12, 16, 20], [ 1, 5, 9, 13, 17, 21], [ 2, 6, 10, 14, 18, 22], [ 3, 7, 11, 15, 19, 23]]) ''' ------ # Resize does the same thing as reshape, but resize directly modifies the array it operates on b.resize((2, 12)) b ''' out: 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]]) '''
5. Combination of arrays
NumPy arrays can be combined horizontally, vertically, and in depth.
Arrays can be combined using vstack, dstack, hstack, column_stack, row_stack, and concatenate functions
The dimension described below is actually the element of the tuple obtained by the shape function
Horizontal combination (second dimension stacked combination)
# Create two multidimensional arrays a = np.arange(9).reshape(3,3) a ''' out: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) ''' b = 2 * a # All elements in a are multiplied by (*)2 b ''' out: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) ''' # Horizontal Combination # Dimensionally speaking, a horizontal combination of two arrays can be achieved as long as the other dimensions are equal and the dimensions are equal outside the second dimension. # From the row and column perspective, arrays can be grouped as long as the number of rows corresponding to them is the same np.hstack((a, b)) ''' out: array([[ 0, 1, 2, 0, 2, 4], [ 3, 4, 5, 6, 8, 10], [ 6, 7, 8, 12, 14, 16]]) ''' # I'm trying to see if different dimensions of the second dimension can be combined horizontally # Modify b Array b = np.arange(12).reshape(3,4) b ''' out: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) ''' np.hstack((a, b)) ''' out: array([[ 0, 1, 2, 0, 1, 2, 3], [ 3, 4, 5, 4, 5, 6, 7], [ 6, 7, 8, 8, 9, 10, 11]]) ''' # Let me try again if the dimensions are different based on the previous example # Change the shape of b b = np.arange(12).reshape(2,3,2) b ''' out: array([[[ 0, 1], [ 2, 3], [ 4, 5]], [[ 6, 7], [ 8, 9], [10, 11]]]) ''' np.hstack((a, b)) ''' out: ValueError: all the input arrays must have same number of dimensions ''' # Error, Error Tip: All input arrays must have the same dimension, indicating that the horizontal combination must have the same dimension ------ # In fact, the concatenate function can do the same thing # Also create two arrays a = np.arange(9).reshape(3,3) a ''' out: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) ''' b = 2 * a # All elements in a are multiplied by (*)2 b ''' out: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) ''' # concatenate((a, b), axis = 0) achieves the same effect axis represents splicing from which dimension the first dimension is 0 np.concatenate((a, b), axis = 1) ''' out: array([[ 0, 1, 2, 0, 2, 4], [ 3, 4, 5, 6, 8, 10], [ 6, 7, 8, 12, 14, 16]]) '''
From this example, it can be concluded that the concatenate functions of hstack and axis = 1 have the same effect
We think of arrays as matrices, rows and columns as widths and heights. Look closely at the example above in hstack. In fact, the horizontal combination is to stitch the widths of two matrices together at the same height to form a new matrix
Similarly, the vertical combination below is the opposite of the horizontal combination. It joins the two matrices up and down to form a new matrix. The width of the new matrix is the same as the width of the original matrix, but the height is the sum of the original matrices.
Vertical Combination (First Dimension Stacked Combination)
# Or create two arrays first a = np.arange(9).reshape(3,3) a ''' out: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) ''' b = 2 * a # All elements in a are multiplied by (*)2 b ''' out: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) ''' # Vertical combination vstacknp. np.vstack((a, b)) ''' out: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) ''' # The same effect can be achieved with concatenate np.concatenate((a, b), axis = 0) # axis=0 represents the first dimension ''' out: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) '''
Vertical combination, as long as the first dimension is equal and the dimensions are the same, then two arrays can be combined, and the vstack effect is the same as that of the concatenate function with axis = 0
Depth combination (third dimension stacking combination)
a = np.arange(12).reshape(2,3,2) a ''' out: array([[[ 0, 1], [ 2, 3], [ 4, 5]], [[ 6, 7], [ 8, 9], [10, 11]]]) ''' c = np.arange(12).reshape(2,3,2) c ''' out: array([[[ 0, 1], [ 2, 3], [ 4, 5]], [[ 6, 7], [ 8, 9], [10, 11]]]) ''' np.dstack((a, c)) ''' out: array([[[ 0, 1, 0, 1], [ 2, 3, 2, 3], [ 4, 5, 4, 5]], [[ 6, 7, 6, 7], [ 8, 9, 8, 9], [10, 11, 10, 11]]]) ''' # Let's look at the change in dimension after depth combination np.dstack((a, c)).shape # out: (2, 3, 4) # Find that the third dimension overlaps # Using concatenate np.concatenate((a,c),axis=2) ''' out: array([[[ 0, 1, 0, 1], [ 2, 3, 2, 3], [ 4, 5, 4, 5]], [[ 6, 7, 6, 7], [ 8, 9, 8, 9], [10, 11, 10, 11]]]) # Same effect as dstack '''
Change the array to compare again
a = np.arange(18).reshape(2,9) a ''' out: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8], [ 9, 10, 11, 12, 13, 14, 15, 16, 17]]) ''' c = np.arange(18).reshape(2,9,1) c ''' out: array([[[ 0], [ 1], [ 2], [ 3], [ 4], [ 5], [ 6], [ 7], [ 8]], [[ 9], [10], [11], [12], [13], [14], [15], [16], [17]]]) ''' np.dstack((a, c)) ''' out: array([[[ 0, 0], [ 1, 1], [ 2, 2], [ 3, 3], [ 4, 4], [ 5, 5], [ 6, 6], [ 7, 7], [ 8, 8]], [[ 9, 9], [10, 10], [11, 11], [12, 12], [13, 13], [14, 14], [15, 15], [16, 16], [17, 17]]]) ''' # Using concatenate np.concatenate((a,c),axis=2) ''' out: AxisError: axis 2 is out of bounds for array of dimension 2 ''' # Error reported because there are arrays whose dimensions do not match those specified by concatenate
With some features of the dstack function in this case, their dimensions can be omitted in arrays that are combined using the dstack function, that is, the dstack function can not only combine two three-dimensional arrays, but also two two two two two two one-dimensional arrays.
You can also find the difference between dstack function and concatenate function. dstack can combine arrays with lower dimensions than it requires. As long as the two arrays are identical except for the corresponding dimension, they can be successfully combined and the dimension of rise can be different
For concatenate functions, however, all dimensions must be explicitly expressed, and the dimensions of both arrays must be the same.Besides the corresponding dimensions of the combinations can be different, the other dimensions must be the same.
There are two more functions
row_stack
column_stack
I tried it with the example above and found that column_stack and hstack both work equally well, without knowing what the difference is.
The same is true for row_stack and vstack
Continue to discuss later and update if you find a difference
6. Split Array
It is important to note that splitting an array yields a list of multiple arrays
Split hsplit horizontally
# Create a two-dimensional array a = np.arange(9) a.shape = (3, 3) a ''' out: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) ''' # Horizontal Split np.hsplit(a, 3) # Horizontal splitting is the average division of the width of a matrix array while maintaining its height, resulting in multiple new arrays of the same shape. ''' Out: [array([[0], [3], [6]]), array ([[1], [4], [7]]), array ([[2], [5], [8]])] ''' np.split(a, 3, axis = 1) # axis=1 is to cut each row, axis=0 is to cut each column. ''' out: [array([[0], [3], [6]]), array([[1], [4], [7]]), array([[2], [5], [8]])] ''' a # Do not change the original array ''' array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) '''
Split hsplit horizontally
a = np.arange(9) a.shape = (3, 3) a ''' out: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) ''' # Vertical split vsplit np.vsplit(a, 3) # Vertical splitting is the average splitting of a matrix array into multiple new arrays with the same width and shape. ''' out: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])] ''' # The same effect can be achieved by calling the split function np.split(a, 3, axis = 0) ''' out: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])] '''
Deep Split dsplit
Be careful:
dsplit only works with arrays of three or more dimensions
a = np.arange(54).reshape(3, 3, 3) a ''' out: 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]]]) ''' # Depth split, parameter array must be 3-D or more np.dsplit(a, 2) ''' out: [ array([[[ 0], [ 3], [ 6]], [[ 9], [12], [15]], [[18], [21], [24]]]), array([[[ 1], [ 4], [ 7]], [[10], [13], [16]], [[19], [22], [25]]]), array([[[ 2], [ 5], [ 8]], [[11], [14], [17]], [[20], [23], [26]]]) ] ''' # Looking at the out put, we can see that the deep division dsplit is divided by the columns of the array.
Dimensionally:
The horizontal division is divided from the second dimension, that is, the second dimension is divided equally, the vertical division is from the first dimension, and the deep division is from the third dimension.
You can see this by looking at each split array with the shape function
Take depth segmentation as an example:
a = np.arange(27).reshape(3, 3, 3) a.shape # out: (3, 3, 3) # Deep Segmentation l = np.dsplit(a, 3) l[0].shape # out: (3, 3, 1) l[1].shape # out: (3, 3, 1) l[2].shape # out: (3, 3, 1)
7. Attributes of arrays
shape, reshape displays and sets the dimensions of the array
dtype * Type used to display arrays
ndim * Dimension of output array
itemsize Gives the number of bytes in memory that elements in an array occupy
nbytes Gives the bytes in memory of all elements in the array
T) The effect is the same as transpose, but T does not change the original array, while transpose changes the original array
flat) returns a numpy.flatiter object that allows us to traverse any multidimensional array as if it were a one-dimensional array
# Shapes and re shape s ''' shape Dimensions used to display arrays, or tuples can be used to set the dimensions of arrays reshape Dimensions used to set arrays ''' # for example a = np.arange(12) a '''out: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])''' # shape View Array Dimension a.shape '''out : (12,)''' # shape sets the dimensions of the array a.shape = (3, 4) a ''' out: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) ''' a.shape '''out:(3, 4) ''' # Reset a Array a = np.arange(12) a '''out: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])''' # reshape sets the dimension of the array a.reshape(3, 4) ''' out: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) ''' a '''out: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])''' ''' //As you can see from the above two examples, shape Setting the dimension of an array directly changes the original array. //But reshape does not change the dimension of the array '''
a = np.arange(12) # dtype property, which displays the type of array a.dtype ''' out: dtype('int32') ''' # ndim, the dimension of the output array a.ndim # out: 1 # size gives the total number of array elements a.size # out:12 # itemsize, gives the number of bytes in memory that elements in an array occupy a.itemsize # out: 4 # nbytes a.nbytes # out: 48 a.size * b.itemsize # out: 48 ''' //Actually nbytes is equal to size*itemsize ''' # T property, the effect is the same as transpose, but T does not change the original array, while transpose changes the original array a = np.arange(12) a.resize(4, 3) a ''' out: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) ''' a.T # Does not change the original array ''' out: array([[ 0, 3, 6, 9], [ 1, 4, 7, 10], [ 2, 5, 8, 11]]) ''' a ''' out: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) ''' # For an array of digits, its T attribute is itself q = np.arange(5) q # out:array([0, 1, 2, 3, 4]) q.T # out:array([0, 1, 2, 3, 4]) q.transpose q # out: array([0, 1, 2, 3, 4]) '''
flat attribute
# ''' flat Property will return a numpy.flatiter Object, this is get flatiter The Only Way to Object - Me //They cannot access the flatiter's constructor.This so-called "flat iterator" can make us look like traversing one dimension //Traverse through any multidimensional array as a group ''' b = np.arange(4).reshape(2, 2) b ''' out: array([[0, 1], [2, 3]]) ''' f = b.flat # The flat property does not change the original array, resulting in an iterative object b ''' out: array([[0, 1], [2, 3]]) ''' for item in f: print(item, end=',') ''' //Knowledge expansion: print Functional end Attribute is the symbol after output, default is\n(Line break), can be modified end Property forces no line break or ends with another symbol ''' # The flat property gets part of the array # flat can also get an element of the array b.flat[[2]] # out : 2 # Or get some elements b.flat[[1, 3]] # Gets elements with subscripts 1 and 3 in the entire array iteration object, multiple subscripts need to be surrounded by [] # out: array([1, 3]) b.flat[:3] # You can also get arrays as slices # Essentially, you slice the array and pass this new array to flat to get iterative objects # out: array([0, 1, 2])
Note: Assigning a flat object directly changes the original array
# Assigning values to flat objects # Assign values to the entire array b.flat = 7 b ''' out: array([[7, 7], [7, 7]]) ''' # Assign a value to an array part b.flat[[0, 2]] = 3 b ''' out: array([[3, 7], [3, 7]]) ''' b.flat[:3] = 1 b ''' out: array([[1, 1], [1, 7]]) '''
8. Array Conversion
The tolist function numpy array is converted to a python list
c = np.arange(8).reshape(2, 2, 2) c ''' out: array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) ''' c.tolist() # out: [[[0, 1], [2, 3]], [[4, 5], [6, 7]]] # Another example d = np.arange(32).reshape(2, 2, 2, 2, 2) d ''' out: 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]]]]]) ''' d.tolist() ''' out: [[[[[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]]]]] ''' # Two examples show that the dimension of the converted list is the same as that of the original array
astype functions can specify data types when converting arrays
# 1. First create an array of floating point types e = np.array([1.12, 1.56]) e # out: array([1.12, 1.56]) # Convert array e to an integer at this time e.astype(int) # This method does not change the original array # out: array([1, 1]) can see that the decimal part has been forcibly deleted e # out: array([1.12, 1.56]) # 2. Convert e to string type e.astype(str) # out : array(['1.12', '1.56'], dtype='<U32') # Successfully converted, indicating that the numeric type can be converted to the string type, then can the string be converted to the numeric type? # 3. Convert string type to numeric type e = e.astype(str) e # out: array(['1.12', '1.56'], dtype='<U32') e.astype(float) # out: array([1.12, 1.56]) #Success means that they can be converted to each other e.astype(int) ''' out: ValueError: invalid literal for int() with base 10: '1.12' //Conversion failed, indicating that formatting is required to convert from string type to numeric type '''