Before introducing the combination and segmentation of arrays, we need to understand the concepts of ndim and axis of arrays.
If the elements of an array are arrays, that is, arrays nested arrays, we call them multidimensional arrays. Several layers of nesting are called several dimensions. For example, a two-dimensional array with shape (a, b) can be regarded as two one-dimensional arrays. The first one-dimensional array contains a one-dimensional array and the second one-dimensional array contains B data.
Each one-dimensional linear array is called an axis. The first axis (axis=0) of a two-dimensional array is the array with the array as the element, and the second axis (axis=1) is the array in the array. Therefore, the direction of the first axis is along the row (vertical direction) and the direction of the second axis is along the column (horizontal direction).
From the perspective of nested arrays, a[0], a[1], a[2], a[3]... Take the first row, the second row, the third row, and the fourth row of the two-dimensional array, respectively. This is exactly how to take the elements along the first axis (elements are rows). a[0][0], a[0][1]... Take the first element and the second element of the first row (along the second axis)
That is, the axis of the array is counted from the outermost layer.
How should we understand three-dimensional array? We can think of it as a stack of two-dimensional arrays, that is, a cube. Its first axis (axis=0) is an array with two-dimensional arrays as elements. Its direction is along the stacking direction of two-dimensional arrays, that is, the height of the cube. The second axis is naturally the width of the cube, and the third axis is the length of the cube. For example, a three-dimensional array with shape (a, b, c) is a two-dimensional array with shape (b, c) nested together.
Click to view the code
a=np.arange(24).reshape(2,3,4)#Create a three-dimensional array with dimension 3 and shape (2, 3, 4) print(a)#Print print(a.sum(axis=0))#Sum along the first axis print(a.sum(axis=1))#Sum along the second axis print(a.sum(axis=2))#Sum along the third axis ''' a The shape of the is as follows: [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] Sum along the first axis: [[12 14 16 18] [20 22 24 26] [28 30 32 34]] Sum along the second axis: [[12 15 18 21] [48 51 54 57]] Sum along the third axis: [[ 6 22 38] [54 70 86]] '''
It can be seen from this example that summing along the first axis is to "flatten" the cube from above, the second axis is along the width, and the third axis is along the length. Similar to projection.
We finally understand that the parameter order of the array attribute shape and reshape function is not the length and width we take for granted; Length, width and height; Because you can't explain why the shape of 3D array is very different from what you think. Its order is the order of axes (the first axis, the second axis, the third axis...), that is, how many elements there are along this axis. The concept of axis is very important and is reflected in many functions.
More intuitively, the parameter order should be high, wide (row direction) and long (column direction).
Therefore, the dimension of the array is well understood, that is, the number of axes. When we understand multidimensional arrays, we should not assume that there will be more elements in multidimensional arrays; A multidimensional array is just that it has many nested layers. High dimensional arrays may also contain no elements.
Next, we introduce the combination of arrays.
Combination of arrays
Array combination includes horizontal combination, vertical combination, depth combination and so on. The functions to realize these combinations mainly include vstack, dstack, hstack and column_stack,row_stack, concatenate, etc.
Because our most commonly used array is only three-dimensional, we use words such as horizontal and vertical to compare the image; But we should understand that it is essentially an operation along the axis.
Array composition usually does not change the dimension of the array.
1. Horizontal combination
hstack function and concatenate function
1.1hstack function: horizontally connect multiple arrays. There is only one parameter: a sequence with an array as an element.
1.2 concatenate function: Join array sequences along existing axes.
Function format: concatenate ((A1, A2,...), axis=0, out=None)
Parameter Description: a1, a2 Is a sequence of class arrays with array elements. Where the array shape must be the same.
axis=0: the array will be combined along this axis. If the coordinate axis is None, the array will be tiled before use. int data, optional parameter, default to zero.
2. Vertical combination
vstack function and concatenate function
2.1 vstack function: vertically connect multiple arrays. The parameters are as above.
2.2concatenate function: just change the axis parameters.
Horizontal combination and vertical combination are more intuitive, because the most used arrays are one-dimensional and two-dimensional; In fact, they are combined along the second axis (horizontal) and the first axis (vertical).
Click to view the code
a=np.array([1]) a=a.reshape(1,1,1,1,1)#A five-dimensional array with only one element b=np.array([1]) b=b.reshape(1,1,1,1,1)#Exactly the same as a c=np.hstack((a,b))#Horizontal combination d=np.vstack((a,b))#Vertical combination print(c) print(d) print(c.shape) print(d.shape) ''' Horizontal combination [[[[[1]]] [[[1]]]]] Vertical combination [[[[[1]]]] [[[[1]]]]] c Shape of (1, 2, 1, 1, 1) d Shape of (2, 1, 1, 1, 1) '''
3. Row combination and column combination
3.1row_stack function: row combination
Combining one-dimensional arrays in the row direction is completely equivalent to vstack for two-dimensional arrays. For multidimensional arrays, they are actually combined along the first axis.
3.2colum_stack function: column combination
Combining one-dimensional arrays in column direction is exactly equivalent to hstack for two-dimensional arrays. For multidimensional arrays, they are actually combined along the second axis.
Click to view the code
a=np.array([0,1,2]) b=np.array([1,2,3]) c=np.row_stack((a,b)) d=np.column_stack((a,b)) print(c) print(d) ''' Row combination [[0 1 2] [1 2 3]] Column combination [[0 1] [1 2] [2 3]] ''' a=np.array([0,1,2]).reshape(1,1,1,1,3) b=np.array([1,2,3]).reshape(1,1,1,1,3) c=np.row_stack((a,b)) d=np.column_stack((a,b)) print(c) print(d) print(c.shape) print(d.shape) ''' Row combination [[[[[0 1 2]]]] [[[[1 2 3]]]]] [[[[[0 1 2]]] Column combination [[[1 2 3]]]]] c shape (2, 1, 1, 1, 3) d shape (1, 2, 1, 1, 3) '''
4. Depth combination
Combine along the third axis.
Click to view the code
a=np.array([0,1,2]) b=np.array([1,2,3]) c=np.dstack((a,b))#Depth combination print(c) print(a.shape) print(c.shape) ''' [[[0 1] [1 2] [2 3]]] (3,) (1, 3, 2) ''' a=np.array([0,1,2]).reshape(1,1,1,3) b=np.array([1,2,3]).reshape(1,1,1,3) c=np.dstack((a,b)) print(c.shape) ''' (1, 1, 2, 3) '''
When the array dimension is relatively small, such as one-dimensional and two-dimensional, if there are no second and third parameters when combining, the function will automatically fill 1 on the left side of the shape, that is, expand one layer. This is very similar to the broadcasting mechanism mentioned before.
Array segmentation
The array can be divided horizontally and vertically. Correlation functions: hsplit, vsplit, dsplit, split.
We can divide the array into sub arrays of the same size (shape) or specify the location of the division.
1. Horizontal segmentation
hsplit function and split function.
Along the horizontal direction, that is, along the column direction, along the second axis (axis=1).
1.1hsplit function
Format: hsplit(ary, indices_or_sections)
The first parameter is an array; The second parameter is an integer or list. If it is not specified, it will be divided into sub arrays of the same size.
Click to view the code
a=np.arange(16).reshape(4,4) pp.pprint(a) pp.pprint(np.hsplit(a,2))#Divided into two parts on average pp.pprint(np.hsplit(a,[2,3]))#Along the second and third columns, it is divided into three parts ''' array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) Split into two parts [array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]), array([[ 2, 3], [ 6, 7], [10, 11], [14, 15]])] Divided into three parts [array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]), array([[ 2], [ 6], [10], [14]]), array([[ 3], [ 7], [11], [15]])] '''
1.2split function
Function format: split(ary, indices_or_sections, axis=0)
First parameter: array.
The second parameter: integer or list, optional.
The third parameter: axis, optional.
Click to view the code
a=np.arange(24).reshape(4,6) print(a) pp.pprint(np.split(a,[2],axis=0)) ''' [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]] [array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]])] '''
In the above example, we selected the first axis, that is, the column direction. Then find the second line and split it in two.
Click to view the code
Click to view the code
a=np.arange(24).reshape(2,3,4) print(a) pp.pprint(np.split(a,[1],axis=0))#Along the first axis, height pp.pprint(np.split(a,[1],axis=1))#Along the second axis, width pp.pprint(np.split(a,[1],axis=2))#Along the third axis, long ''' [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] [array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]), array([[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])] [array([[[ 0, 1, 2, 3]], [[12, 13, 14, 15]]]), array([[[ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[16, 17, 18, 19], [20, 21, 22, 23]]])] [array([[[ 0], [ 4], [ 8]], [[12], [16], [20]]]), array([[[ 1, 2, 3], [ 5, 6, 7], [ 9, 10, 11]], [[13, 14, 15], [17, 18, 19], [21, 22, 23]]])] '''
The above is an example of 3D array cutting.
2. Vertical segmentation
vsplit function and split function
Along the vertical direction, that is, along the row direction, along the first axis (axis=0).
split function as above, just change an axis parameter.
3. Depth segmentation
dsplit function
It is mainly used for three-dimensional arrays. In fact, it is cutting along the third axis, just like cutting a cake from above.
Click to view the code
a=np.arange(24).reshape(2,3,4) b=np.dsplit(a,4)#Cut the cake into four pieces from the top pp.pprint(b) ''' [array([[[ 0], [ 4], [ 8]], [[12], [16], [20]]]), array([[[ 1], [ 5], [ 9]], [[13], [17], [21]]]), array([[[ 2], [ 6], [10]], [[14], [18], [22]]]), array([[[ 3], [ 7], [11]], [[15], [19], [23]]])] '''
with python tutorial Come on.