Introduction and examples of stack of hstack of vstack of

  • 2020-06-23 01:06:51
  • OfStack

1. stack () function

The prototype is stack(arrays,axis=0), which can pass arrays and lists. I'll explain what axis means next, but let's look at an example first, and then I'll analyze the output.


import numpy as np
a=[[1,2,3],
 [4,5,6]]
print(" The list of a As follows: ")
print(a)

print(" increase 1 Dimension, the subscript of the new dimension is 0")
c=np.stack(a,axis=0)
print(c)

print(" increase 1 Dimension, the subscript of the new dimension is 1")
c=np.stack(a,axis=1)
print(c)

 Output: 
 The list of a As follows: 
[[1, 2, 3], [4, 5, 6]]
 increase 1 Dimension, new dimension subscript 0
[[1 2 3]
 [4 5 6]]
 increase 1 Dimension, new dimension subscript 1
[[1 4]
 [2 5]
 [3 6]]

First arrays my travels here is a list, now I started to explain this stack () function, it is the arrays inside of each element (it might be a list of tuples, or is a numpy array) into numpy array, 1 d again to each element (as for dimensions and where, is controlled by axis), and then to string together these elements (as to how to string, I would say below).

Each element in arrays must have one shape. For example, the two elements [1,2,3] and [4,5,6] in listing a in this example have one shape. If you replace [4,5,6] with [4,5], the program will report an error! And axis represents the 1 d on what dimension, such as axis = 0 (it is the default) represent is to increase the 1 d subscript to 0, axis equal how not to write, if the parameter arrays inside of each element is a 1 d, then call stack after 1 d () function will become 2 d, so axis can only equal to 0 and 1 (subscript begins with zero dimension), and parameter axis = 0 and axis = 1 the result is not 1 sample.

, for example, the code above a list the first element in [1, 2, 3], so when axis = 0 is the brackets in it add a center outside parentheses, into a [[1, 2, 3]] (in fact, there is no comma between 1, 2, 3, because stack arrays () function will be the first parameter of each element into numpy array, the array is no comma between, look at the code above output, here you know, I for your convenience, the following will add a comma). So that the outermost bracket represents the dimension with the index of zero; When axis=1, you put a bracket inside it that becomes [[1], [2], [3]], so that the bracket inside it represents the dimension with the index of 1. When axis=0 [4,5,6] becomes [4,5,6], and when axis=1 becomes [4], [5], [6]]. Now let's see how we can string each element after adding 1 dimension.

Now, how do you string together the results of adding dimensions to those two elements? Well, it's pretty straightforward. Now we know that adding dimensions is nothing more than adding brackets, depending on what axis is. Think of the added brackets as a box of one. Take the above code for example, when axis=0, we regard the bracket outside [1,2,3] as the box A, and the box A is also outside [4,5,6], so we put [1,2,3] and [4,5,6] in the first place, become [1,2,3],[4,5,6], and then put the box A in the first place. Becomes [[1,2,3],[4,5,6]] and this is the output of the program when axis=0.

Now look at when axis = 1 in [1, 2, 3], we have set on the outside of the 1 cases (that is, speak on the [[1], [2], [3]] in 1 layer outside the brackets) as A, set as B, to the outside of the 2 sets of the outside of the 3 as C, by the same token, the box A will set in the outside of the 4, box B will set outside of the five, the outside of the box C will set in 6. So we put 1 and 4, 2 and 5, 3 and 6 and 1 into [1,4, 2,5, 3,6] and then put the box A,B and C outside 1,4, 2,5, 3,6, respectively, into [[1,4], [2,5], [3,6] and this is the output of the program when axis=1.

You see, when you string it up, you put each element in arrays in the same place and put it in the same place, and then you put it in the same place, and then you put it in the same place, and then you put it in the same place, and then you put it in the same place, and then you put it in the same place, and then you put it in the same place, and then you put it in the same place, and then you put it in the same place.

Take a look at the output of the code below to test if you understand it.


import numpy as np
a=[[1,2,3,4],
 [5,6,7,8],
 [9,10,11,12]]
print(" The list of a As follows: ")
print(a)

print(" increase 1 Dimension, the subscript of the new dimension is 0")
c=np.stack(a,axis=0)
print(c)

print(" increase 1 Dimension, the subscript of the new dimension is 1")
c=np.stack(a,axis=1)
print(c)

 Output: 
 The list of a As follows: 
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
 increase 1 Dimension, the subscript of the new dimension is 0
[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]]
 increase 1 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 if it looks like the output you imagined, but there's another case, so let's look at the code below.


import numpy as np
a=[1,2,3,4]
b=[5,6,7,8]
c=[9,10,11,12]
print("a=",a)
print("b=",b)
print("c=",c)

print(" increase 1 Dimension, the subscript of the new dimension is 0")
d=np.stack((a,b,c),axis=0)
print(d)

print(" increase 1 Dimension, the subscript of the 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])
 increase 1 Dimension, the subscript of the new dimension is 0
[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]]
 increase 1 Dimension, the subscript of the new dimension is 1
[[ 1 5 9]
 [ 2 6 10]
 [ 3 7 11]
 [ 4 8 12]]

You'll see that the output looks exactly like code 1 above, which is exactly the same. It's just that when you pass arguments to arrays, if you pass arguments like (a,b,c), it sees (a,b,c) as a tuple, a,b,c are each of the elements of the tuple. Each element is then processed separately. As I said above, arrays passes in either lists, tuples, or numpy arrays. So the effect is the same for both (a,b,c) and [a,b,c] or when x=[a,b,c].

The above code deals with arrays elements that go from one dimension to two, so let's look at what goes from two to three.


import numpy as np
a=[[1,2,3],
 [4,5,6]]
b=[[1,2,3],
 [4,5,6]]
c=[[1,2,3],
 [4,5,6]]
print("a=",a)
print("b=",b)
print("c=",c)

print(" increase 1 Dimension, the subscript of the new dimension is 0")
d=np.stack((a,b,c),axis=0)
print(d)

print(" increase 1 Dimension, the subscript of the new dimension is 1")
d=np.stack((a,b,c),axis=1)
print(d)
print(" increase 1 Dimension, the subscript of the 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]])
 increase 1 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]]]
 increase 1 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]]]
 increase 1 Dimension, the subscript of the new dimension is 2
[[[1 1 1]
 [2 2 2]
 [3 3 3]]

 [[4 4 4]
 [5 5 5]
 [6 6 6]]]

When axis=0, the list a, b and c all need boxes on the outside (i.e., brackets), so I'll put you in the first one, like this


[[1,2,3],[4,5,6]],
[[1,2,3],[4,5,6]],
[[1,2,3],[4,5,6]]

And then you put the box on the outside, and it becomes


[
[[1,2,3],[4,5,6]],
[[1,2,3],[4,5,6]],
[[1,2,3],[4,5,6]]
]

When axis=1, the [1,2,3] in list a, b, c need the same box. The [4,5,6] in list a, b, c need the same box. Ok, I'll put you in a box like this


[
 [1,2,3],[1,2,3],[1,2,3]
 ,
 [4,5,6],[4,5,6],[4,5,6]

]

And then start in [1, 2, 3], [1, 2, 3], [1, 2, 3] outside and [4 and 6], [4 and 6], [4 and 6] outside of the box, become like this


[
 [[1,2,3],[1,2,3],[1,2,3]]
 ,
 [[4,5,6],[4,5,6],[4,5,6]]

]

When axis=2, list a, b, c 1, 2, 3, 4, 5, 6 all need boxes. I'll put you 1 first to read:


[
 [1,1,1 , 2,2,2 , 3,3,3],
 [4,4,4 , 5,5,5 , 6,6,6]
]

Then at 1,1,1... The outer boxes of 6,6 and 6 shall be changed into:


[
 [[1,1,1] , [2,2,2] , [3,3,3]],
 [[4,4,4] , [5,5,5] , [6,6,6]]
]

So much for the stack() function, which is all I understand.

2. hstack () function

Function prototype: hstack(tup). The argument tup can be a tuple, a list, or an numpy array that returns an array of numpy. Look at the code below to see what it means


import numpy as np
a=[[1,2,3,4],
 [5,6,7,8],
 [9,10,11,12]]
print(" The list of a As follows: ")
print(a)

print(" increase 1 Dimension, the subscript of the new dimension is 0")
c=np.stack(a,axis=0)
print(c)

print(" increase 1 Dimension, the subscript of the new dimension is 1")
c=np.stack(a,axis=1)
print(c)

 Output: 
 The list of a As follows: 
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
 increase 1 Dimension, the subscript of the new dimension is 0
[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]]
 increase 1 Dimension, the subscript of the new dimension is 1
[[ 1 5 9]
 [ 2 6 10]
 [ 3 7 11]
 [ 4 8 12]]
0

import numpy as np
a=[[1,2,3,4],
 [5,6,7,8],
 [9,10,11,12]]
print(" The list of a As follows: ")
print(a)

print(" increase 1 Dimension, the subscript of the new dimension is 0")
c=np.stack(a,axis=0)
print(c)

print(" increase 1 Dimension, the subscript of the new dimension is 1")
c=np.stack(a,axis=1)
print(c)

 Output: 
 The list of a As follows: 
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
 increase 1 Dimension, the subscript of the new dimension is 0
[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]]
 increase 1 Dimension, the subscript of the new dimension is 1
[[ 1 5 9]
 [ 2 6 10]
 [ 3 7 11]
 [ 4 8 12]]
1

The vstack() function is the opposite of stacking arrays horizontally (in column order).

3. vstack () function

Function prototype: vstack(tup). The argument tup can be a tuple, a list, or an numpy array, which returns an array of numpy. Look at the code below to see what it means


import numpy as np
a=[[1,2,3,4],
 [5,6,7,8],
 [9,10,11,12]]
print(" The list of a As follows: ")
print(a)

print(" increase 1 Dimension, the subscript of the new dimension is 0")
c=np.stack(a,axis=0)
print(c)

print(" increase 1 Dimension, the subscript of the new dimension is 1")
c=np.stack(a,axis=1)
print(c)

 Output: 
 The list of a As follows: 
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
 increase 1 Dimension, the subscript of the new dimension is 0
[[ 1 2 3 4]
 [ 5 6 7 8]
 [ 9 10 11 12]]
 increase 1 Dimension, the subscript of the new dimension is 1
[[ 1 5 9]
 [ 2 6 10]
 [ 3 7 11]
 [ 4 8 12]]
2

import numpy as np
a=[[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 the arrays vertically (in row order).

conclusion

This is the introduction and examples of stack(),hstack(),vstack() functions in Numpy. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: