Time for action – stacking arrays

First, let's set up some arrays:

In: a = arange(9).reshape(3,3)
In: a
Out:
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In: b = 2 * a
In: b
Out:
array([[ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16]])
  1. Horizontal stacking: Starting with horizontal stacking, we will form a tuple of ndarrays and give it to the hstack function. This is shown as follows:
    In: hstack((a, b))
    Out:
    array([[ 0,  1,  2,  0,  2,  4],
           [ 3,  4,  5,  6,  8, 10],
           [ 6,  7,  8, 12, 14, 16]])

    We can achieve the same with the concatenate function, which is shown as follows:

    In: 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]])
    Time for action – stacking arrays
  2. Vertical stacking: With vertical stacking, again, a tuple is formed. This time, it is given to the vstack function. This can be seen as follows:
    In: vstack((a, b))
    Out:
    array([[ 0,  1,  2],
           [ 3,  4,  5],
           [ 6,  7,  8],
           [ 0,  2,  4],
           [ 6,  8, 10],
           [12, 14, 16]])

    The concatenate function produces the same result with the axis set to 0. This is the default value for the axis argument.

    In: concatenate((a, b), axis=0)
    Out:
    array([[ 0,  1,  2],
           [ 3,  4,  5],
           [ 6,  7,  8],
           [ 0,  2,  4],
           [ 6,  8, 10],
           [12, 14, 16]])
    Time for action – stacking arrays
  3. Depth stacking: Additionally, there is the depth-wise stacking using dstack and a tuple, of course. This means stacking of a list of arrays along the third axis (depth). For instance, we could stack 2D arrays of image data on top of each other.
    In: dstack((a, b))
    Out:
    array([[[ 0,  0],
            [ 1,  2],
            [ 2,  4]],
           [[ 3,  6],
            [ 4,  8],
            [ 5, 10]],
           [[ 6, 12],
            [ 7, 14],
            [ 8, 16]]])
  4. Column stacking: The column_stack function stacks 1D arrays column-wise. It's shown as follows:
    In: oned = arange(2)
    In: oned
    Out: array([0, 1])
    In: twice_oned = 2 * oned
    In: twice_oned
    Out: array([0, 2])
    In: column_stack((oned, twice_oned))
    Out:
    array([[0, 0],
           [1, 2]])

    2D arrays are stacked the way hstack stacks them:

    In: column_stack((a, b))
    Out:
    array([[ 0,  1,  2,  0,  2,  4],
           [ 3,  4,  5,  6,  8, 10],
           [ 6,  7,  8, 12, 14, 16]])
    In: column_stack((a, b)) == hstack((a, b))
    Out:
    array([[ True,  True,  True,  True,  True,  True],
           [ True,  True,  True,  True,  True,  True],
           [ True,  True,  True,  True,  True,  True]], dtype=bool)

    Yes, you guessed it right! We compared two arrays with the == operator. Isn't it beautiful?

  5. Row stacking: NumPy, of course, also has a function that does row-wise stacking. It is called row_stack and, for 1D arrays, it just stacks the arrays in rows into a 2D array.
    In: row_stack((oned, twice_oned))
    Out:
    array([[0, 1],
           [0, 2]])

    The row_stack function results for 2D arrays are equal to, yes, exactly, the vstack function results.

    In: row_stack((a, b))
    Out:
    array([[ 0,  1,  2],
           [ 3,  4,  5],
           [ 6,  7,  8],
           [ 0,  2,  4],
           [ 6,  8, 10],
           [12, 14, 16]])
    In: row_stack((a,b)) == vstack((a, b))
    Out:
    array([[ True,  True,  True],
           [ True,  True,  True],
           [ True,  True,  True],
           [ True,  True,  True],
           [ True,  True,  True],
           [ True,  True,  True]], dtype=bool)

What just happened?

We stacked arrays horizontally, depth-wise, and vertically. We used the vstack, dstack, hstack, column_stack, row_stack, and concatenate functions.

Splitting

Arrays can be split vertically, horizontally, or depth wise. The functions involved are hsplit, vsplit, dsplit, and split. We can either split into arrays of the same shape or indicate the position after which the split should occur.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.64.47