Numpy stack (), hstack (), vstack () function usage and examples, numpyhstack
1. stack () function
The function prototype is stack (arrays, axis = 0). arrays can be used to upload arrays and lists. I will explain the meaning of axis below. Let's take a look at the example and then I will analyze the output result.
Import numpy as npa = [[1, 2, 3], [4, 5, 6] print ("list a is as follows:") print (a) print ("add one dimension, the subscript of the new dimension is 0 ") c = np. stack (a, axis = 0) print (c) print ("add one dimension, the subscript of the new dimension is 1") c = np. stack (a, axis = 1) print (c) Output: List a is as follows: [[1, 2, 3], [4, 5, 6] add one dimension, the subscript of the new dimension is 0 [[1 2 3] [4 5 6], and the subscript of the new dimension is 1 [[1 4] [2 5] [3 6].
First, arrays is a list. Now I will explain the meaning of this stack () function, which is for each element in arrays (may be a list, tuples, or a numpy array) is converted to a numpy array, and then a one-dimensional value is added to each element (where the dimension is added, it is controlled by axis ), then, concatenate these elements (as for how to string them, I will talk about them below ).
Each element in arrays must have the same shape. For example, the two elements in list a in this example are in the same shape as [, 3] and [, 6, if [, 6] is changed to [], an error is reported! Axis indicates the dimension on which one dimension is added. For example, axis = 0 (which is the default value) indicates that the subscript of the added one dimension is 0, the value equal to axis is not randomly written. If each element in the arrays parameter is a one-dimensional element, calling the stack () function to add one dimension will become a two-dimensional component, therefore, axis can only be equal to 0 and 1 (the subscript of the dimension starts from 0), and the results of the parameters axis = 0 and axis = 1 are different.
For example, if the first element in the list of a in the code above is [1, 2, 3], then when axis is 0, an angle bracket is added out of its brackets, [[, 3] (in fact, there is no comma between and 3, because the stack () function will first convert each element in the arrays parameter into an array of numpy, there is no comma between arrays. You can see the code output above. Here you can understand it. For convenience, I will add a comma below ), in this way, the parentheses in the outermost layer represent the dimension whose subscript is 0. When axis = 1, a bracket is added to it, which becomes [[1], [2], [3]. In this way, the brackets in the layer added inside represent the dimension whose subscript is 1. Similarly, when axis is 0, [, 6] is changed to [[, 5]. When axis is 1, it is changed to [[4], [5], [6]. Next we will explain how to concatenate each element after adding a dimension.
How to concatenate the results of the preceding two elements after adding dimensions is actually very simple. Now we know that adding a dimension is nothing more than adding brackets. as to where to add brackets, it depends on the number of equals to axis. We think of the brackets added as boxes. Taking the above Code as an example, when axis is 0, we place brackets ([, 3]) outside [, 3], that is, the middle bracket of the outermost layer) as A box A, this box A will also be placed outside [, 6], so we should first put [, 3] and [, 6] together, change to [1, 2, 3], [, 6], and then attach A to [1, 2, 3], [, 5, 6] This is the output result of the program when axis = 0.
Now let's take a look at axis = 1. For [1, 2, 3], we put the box on the outside of 1 (that is, [1], [2], [3] in the middle of the 1 outer layer of the brackets) as A, set outside 2 as B, set outside 3 as C, similarly, box A will also be set outside 4, Box B will also be set outside 5, and box C will also be set outside 6. Then we put 1 and 4 together, 2 and 5 together, 3 and 6 together, into [,], and then put Box A, B, C is set to [[], [], [], and [] respectively. This is the output result of the program when axis is 1 in the program.
No, when we get together, we actually put each element in arrays in some small pieces of the box in the same position (this is called a small piece, which may not be the same, but you can understand it) put together, then set the box, that is, the outer set of brackets, This is the stack.
Let's look at the output of the following code and test what you understand.
Import numpy as npa = [[1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] print ("list a is as follows:") print () print ("add one dimension, the subscript of the new dimension is 0") c = np. stack (a, axis = 0) print (c) print ("add one dimension, the subscript of the new dimension is 1") c = np. stack (a, axis = 1) print (c) Output: List a is as follows: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] adding one dimension, the subscript of the new dimension is 0 [[1 2 3 4] [5 6 7 8] [9 10 11 12] to add one dimension, the subscript of the new dimension is 1 [[1 5 9] [2 6 10] [3 7 11] [4 8 12]
I don't know how different the output is from your imagination. In another case, let's take a look at the following code.
Import numpy as npa = [1, 3, 4] B = [5, 6, 7, 8] c = [9, 10, 11, 12] print ("a =", a) print ("B = ", b) print ("c =", c) print ("add one dimension, the subscript of the new dimension is 0") d = np. stack (a, B, c), axis = 0) print (d) print ("add one dimension, subscript of new dimension is 1") d = np. stack (a, B, c), axis = 1) print (d) Output :( 'a = ', [1, 2, 3, 4]) ('B =', [5, 6, 7, 8]) ('C = ', [9, 10, 11, 12]) add one dimension, the subscript of the new dimension is 0 [[1 2 3 4] [5 6 7 8] [9 10 11 12] to add one dimension, the subscript of the new dimension is 1 [[1 5 9] [2 6 10] [3 7 11] [4 8 12]
You will find that the output result is the same as the above Code. In fact, they are the same. However, when you pass a parameter to arrays, if the parameter you pass is similar to (a, B, c) as a tuples, a, B, and c are each element of the tuples. Then process each element separately. As mentioned above, the parameters transmitted by arrays can be list, tuples, or numpy arrays. Therefore, the effects of transmitting (a, B, c) and [a, B, c] or when x = [a, B, c] are the same.
The arrays elements processed by the code above are changed from one dimension to two dimensions. Let's take a look at what the two-dimensional transformation is like.
Import numpy as npa = [[, 3], [, 6] B = [[, 3], [, 6] c =, 3], [4, 5, 6] print ("a =", a) print ("B =", B) print ("c =", c) print ("add one dimension, the subscript of the new dimension is 0") d = np. stack (a, B, c), axis = 0) print (d) print ("add one dimension, subscript of new dimension is 1") d = np. stack (a, B, c), axis = 1) print (d) print ("add one dimension, subscript of new dimension is 2") d = np. stack (a, B, c), axis = 2) print (d) Output :( 'a = ', [1, 2, 3], [4, 5, 6]) ('B =', [[1, 2, 3], [4, 5, 6]) ('C = ', [1, 2, 3], [4, 5, 6]) add one dimension, the subscript of the new dimension is 0 [[[1 2 3] [4 5 6] [[1 2 3] [4 5 6] [[1 2 3] [4 5 6] add one dimension, the subscript of the new dimension is 1 [[[1 2 3] [1 2 3] [1 2 3] [[4 5 6] [4 5 6] [4 5 6]] add one dimension, the subscript of the new dimension is 2 [[[1 1 1] [2 2 2 2] [3 3] [[4 4 4] [5 5 5] [6 6 6]]
When axis is set to 0, the outermost surface of list a, B, and c needs a box (that is, brackets), so I will put you together first and change it to the following:
[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]
In the outermost box
[[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]]
When axis is 1, [, 3] in list a, B, c needs to set the same box. in list a, B, c, [, 5, 6] You need to set the same box. Well, let me put one of you into the following.
[ [1,2,3],[1,2,3],[1,2,3] , [4,5,6],[4,5,6],[4,5,6]]
And then start to [, 3], [, 3], [, 3] and [, 6], [, 6, 6] The outer box is changed to the following:
[ [[1,2,3],[1,2,3],[1,2,3]] , [[4,5,6],[4,5,6],[4,5,6]]]
When axis is 2, boxes 1, 2, 3, 4, 5, and 6 in list a, B, and c need to be set up. I will put you together first to become:
[ [1,1,1 , 2,2,2 , 3,3,3], [4,4,4 , 5,5,5 , 6,6,6]]
Then at, 1 ......... The boxes on the outside of 6, 6 are changed:
[ [[1,1,1] , [2,2,2] , [3,3,3]], [[4,4,4] , [5,5,5] , [6,6,6]]]
There are so many stack () functions that I fully understand.
2. hstack () function
Function prototype: hstack (tup). The tup parameter can be a tuples, list, or numpy array, and the returned result is an array of numpy. See the following code to understand its meaning.
Import numpy as npa = [1, 2, 3] B = [4, 5, 6] print (np. hstack (a, B) Output: [1 2 3 4 5 6]
Import numpy as npa = [[1], [2], [3] B = [[1], [2], [3] c = [1], [2], [3] d = [[1], [2], [3] print (np. hstack (a, B, c, d) Output: [1 1 1 1] [2 2 2] [3 3 3 3]
It is actually to stack the array horizontally (In column order), and The vstack () function is just opposite to it.
3. vstack () function
Function prototype: vstack (tup). The tup parameter can be a tuples, list, or numpy array, and the returned result is an array of numpy. See the following code to understand its meaning.
Import numpy as npa = [1, 2, 3] B = [4, 5, 6] print (np. vstack (a, B) Output: [[1 2 3] [4 5 6]
Import numpy as npa = [[1], [2], [3] B = [[1], [2], [3] c = [1], [2], [3] d = [[1], [2], [3] print (np. vstack (a, B, c, d) output: [[1] [2] [3] [1] [2] [3] [1] [2] [3] [1] [2] [3]
It stacks arrays vertically (in the order of rows.
Summary
The above section describes the usage of the stack (), hstack (), and vstack () functions in Numpy and all the content of the instance. I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!