"Python" NumPy stack (), Hstack (), Vstack () function

Source: Internet
Author: User
Tags function prototype

Turn from 73380803

These three functions are some similarity, are stacked array, the most difficult to understand is the stack () function, I consulted the official documents of NumPy, on the internet and read a few Daniel's blog, found that they also just numpy the contents of the document copied, after reading or can not understand, finally through my Code analysis , it is understood that the stack () function increases the meaning of the dimension. The following content I will be in easy-to-understand language interpretation, the content may be a bit more, patience to see, if not right, welcome to correct!

1. Stack () function
The function prototype is: stack (arrays, axis=0), arrays can pass arrays and lists. The meaning of axis I will explain below, we first look at an example, and then I will analyze the output results.

>>>ImportNumPy as NP>>>a=[[1,2,3],      [4,5,6]]>>>Print("List A is as follows:")>>>Print(a)>>>Print("one dimension is added and the subscript for the new dimension is 0")>>>c=np.stack (a,axis=0)>>>Print(c)>>>Print("one dimension is added and the subscript for the new dimension is 1")>>>c=np.stack (A,axis=1)>>>Print(c)

Output:

The list A is as follows: [[1, 2, 3], [4, 5, 6]] Add one dimension, the new dimension is labeled 0[[1 2 3] [4 5 6]] Add one dimension, the new dimension is labeled 1[[1 4] [2 5] [3 6]

The first thing I arrays here is a list, and now I'm going to explain the meaning of the stack () function, which is to add one dimension to each element (perhaps a list, a tuple, or an array of numpy) into an array of numpy, and then to each of the elements ( As for where the dimension is added, it is controlled by axis, and then the elements are strung together (as for the string, I will say below).

Each element in the arrays must have the same shape, for example, the two elements in List A in this example [three-way] and [4,5,6] are the same, and if [4,5,6] is replaced by [4,5], the program will report an error! and axis represents the dimension in which to add a dimension, for example, Axis=0 (which is the default) represents the increment of this dimension of the subscript is 0,axis equals how much is not random, if the parameters arrays inside each element is a 1-dimensional, then call stack () The function adds one dimension and becomes 2 dimensions, so axis can only be equal to 0 and 1 (the subscript for the dimension starts at 0), and the parameters axis=0 and Axis=1 get different results.

For example, the first element in the list of a in the above code is [axis=0], then when it is in the middle bracket, it is added with a square bracket outside of it, which becomes [[+]] (in fact, there is no comma between the two, because Stack () The function will first change each element in the parameter arrays into an array of numpy, there is no comma between the array, see the above code output to know, here we understand the line, I would like to explain, the following will also add a comma), so that the outermost layer of the brackets is the dimension of the subscript 0 of the dimension; =1, it is in the inside with the brackets, into [[1],[2],[3]], so that the added layer of the brackets in the dimension of the subscript 1 is the dimension. Similarly when Axis=0 [4,5,6] becomes [[4,5,6]], when Axis=1, it becomes [[4],[5],[6]]. Let's talk about how to string up each element after adding a dimension.

How to put the above two elements added to the results of the string up, in fact, it is very simple. Now we know that the addition of dimensions is nothing more than the meaning of the brackets, as to where to add the brackets, depending on the axis equals several. We imagined the added bracket as a box. And with the code above, when axis=0, we put the brackets ([[] the outermost bracket ") outside the [[]] box A, and the box A will be outside the [4,5,6], so we'll start with [all-in-one] and [4,5,6] Put together, become [1,2,3],[4,5,6], and then set up the box A, into [[1,2,3],[4,5,6]] This is when the Axis=0 program output results.

Now look at the time when Axis=1, for [a.], we put the box outside the 1 (that is, [[1],[2],[3] in the [[]] 1 outside of the layer of brackets) as a, set on the outside of the 2 as B, set on the 3 outside as C, the same way, box A will be set on the outside of 4, Box B will also be set on the outside of 5, Box C will also be set on the outside of 6. Then we put 1 and 4 together, 2 and 5 together, 3 and 6 put together, into [1,4, 2,5, 3,6] and then put the box a,b,c respectively in 1,4, 2,5, 3,6 outside, into [[1,4], [2,5], [3,6]] This is the program Axis=1 the time Path The output of the sequence.

We found that no, string up the time is actually the arrays in the same position of each element in the box of some small pieces (here is called small block this noun may not be consistent, but you understand the line) put together, then set the box, is the outside set of brackets, this is the stack.

Then look at the output of the code below and test what you understand.

>>>ImportNumPy as NP>>>a=[[1,2,3,4],      [5,6,7,8],      [9,10,11,12]]>>>Print("List A is as follows:")>>>Print(a)>>>Print("one dimension is added and the subscript for the new dimension is 0")>>>c=np.stack (a,axis=0)>>>Print(c)>>>Print("one dimension is added and the subscript for the new dimension is 1")>>>c=np.stack (A,axis=1)>>>Print(c)

Output:

The list A is as follows: [[1, 2, 3, 4], [5, 6, 7, 8], [9, ten,one, 1  2  3  45  6 7  8
   
    9
    1  5  
    9 2  6
    3 7 each  
    4  8 12]
   

Don't know the same as the output you think, there is another situation, first look at the following code.

>>>ImportNumPy 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("one dimension is added and the subscript for the new dimension is 0")>>>d=np.stack (a,b,c), axis=0)>>>Print(d)>>>Print("one dimension is added and the subscript for 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, Ten, one,1  2  3  45 6  7  8  91  5  92 6  (3 7 each  4  8 12]]

You will find that the output is the same as the above code, but it is the same. But when you pass the arrays, if you pass a parameter that is similar to (A,B,C), it will consider (A,B,C) as a tuple, and a,b,c are each element of the tuple. Then each element is processed separately, as I have already said, arrays arguments can be lists, tuples, or numpy arrays. So when you pass (A,B,C) and pass [a,b,c] or x=[a,b,c], the effect is the same.

The above code to deal with the arrays elements are one-dimensional variable two-dimensional situation, below we look at the two-dimensional change three-dimensional what kind of.

ImportNumPy as NPA=[[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("one dimension is added and the subscript for the new dimension is 0") d=np.stack (a,b,c), axis=0)Print(d)Print("one dimension is added and the subscript for the new dimension is 1") d=np.stack (a,b,c), Axis=1)Print(d)Print("one dimension is added and the subscript for 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]]) adds one dimension and the subscript for the new dimension is 0[[[1 2 3]  [4 5 6]] [[1 2 3]  [4 5 6]] [[1 2 3]  [4 5 6]] to add one dimension, the subscript for the new dimension is 1[[[1 2 3]  [1 2 3]  [1 2 3]] [[4 5 6]  [4 5 6]  [4 5 6]] to add one dimension, the subscript for the new dimension is 2[[[1 1 1]  [2 2 2]  [3 3 3]] [[4 4 4]  [5 5 5]  [6 6 6]]

When the axis=0, the list a,b,c the outermost need to set the box (that is, add brackets), then I put you together first, to become the following

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

And then, in the outermost case, turn into

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

When the Axis=1, the list of a,b,c in the [""] need to set the same box, the list a,b,c [4,5,6] need to set the same box, OK, I first put you into a piece to the following

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

And then start the outside of [1,2,3],[1,2,3],[1,2,3] and [4,5,6],[4,5,6],[4,5,6] outside the box, into the following

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

When axis=2, the list of a,b,c in the 1,2,3,4,5,6 need a box, I put you together to become:

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

And then in 1,1,1 ... The outside of the 6,6,6 is set into:

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

So much about the stack () function, which is part of all I understand.

2. Hstack () function
Function prototype: Hstack (TUP), the parameter tup can be a tuple, a list, or a numpy array, returning an array with the result numpy. Look at the code below to understand what it means.

Import NumPy as NPA=[1,2,3]b=[4,5,6]print(Np.hstack ((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 2] [3 3 3 3]]

It is actually horizontal (in column order) to stack up the array, and the Vstack () function is exactly the opposite of it.

3. Vstack () function
Function prototype: Vstack (TUP), the parameter tup can be a tuple, a list, or a numpy array, returning an array with the result numpy. Look at the code below to understand what it means.

Import NumPy as NPA=[1,2,3]b=[4,5,6]print(Np.vstack ((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 is the vertical (in line order) of the array to be stacked up.

"Python" NumPy stack (), Hstack (), Vstack () function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.