Combining arrays

Arrays can be combined in various ways. This process in NumPy is referred to as stacking. Stacking can take various forms, including horizontal, vertical, and depth-wise stacking. To demonstrate this, we will use the following two arrays (a and b):

In [59]:
   # creating two arrays for examples
   a = np.arange(9).reshape(3, 3)
   b = (a + 1) * 10
   a

Out[59]:
   array([[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]])

In [60]:
   b

Out[60]:
   array([[10, 20, 30],
          [40, 50, 60],
          [70, 80, 90]])

Horizontal stacking combines two arrays in a manner where the columns of the second array are placed to the right of those in the first array. The function actually stacks the two items provided in a two-element tuple. The result is a new array with data copied from the two that are specified:

In [61]:
   # horizontally stack the two arrays
   # b becomes columns of a to the right of a's columns
   np.hstack((a, b))

Out[61]:
   array([[ 0,  1,  2, 10, 20, 30],
          [ 3,  4,  5, 40, 50, 60],
          [ 6,  7,  8, 70, 80, 90]])

This functionally is equivalent to using the np.concatenate() function while specifying axis = 1:

In [62]:
   # identical to concatenate along axis = 1
   np.concatenate((a, b), axis = 1)

Out[62]:
   array([[ 0,  1,  2, 10, 20, 30],
          [ 3,  4,  5, 40, 50, 60],
          [ 6,  7,  8, 70, 80, 90]])

Vertical stacking returns a new array with the contents of the second array as appended rows of the first array:

In [63]:
# vertical stack, adding b as rows after a's rows
np.vstack((a, b))

Out[63]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 20, 30],
       [40, 50, 60],
       [70, 80, 90]])

Like np.hstack(), this is equivalent to using the concatenate function, except specifying axis=0:

In [64]:
   # concatenate along axis=0 is the same as vstack
   np.concatenate((a, b), axis = 0)

Out[64]:
   array([[ 0,  1,  2],
          [ 3,  4,  5],
          [ 6,  7,  8],
          [10, 20, 30],
          [40, 50, 60],
          [70, 80, 90]])

Depth stacking takes a list of arrays and arranges them in order along an additional axis referred to as the depth:

In [65]:
   # dstack stacks each independent column of a and b
   np.dstack((a, b))

Out[65]:
   rray([[[ 0, 10],
           [ 1, 20],
           [ 2, 30]],

          [[ 3, 40],
           [ 4, 50],
           [ 5, 60]],

          [[ 6, 70],
           [ 7, 80],
           [ 8, 90]]])

Column stacking performs a horizontal stack of two one-dimensional arrays, making each array a column in the resulting array:

In [66]:
   # set up 1-d array
   one_d_a = np.arange(5)
   one_d_a

Out[66]:
   array([0, 1, 2, 3, 4])

In [67]:
   # another 1-d array
   one_d_b = (one_d_a + 1) * 10
   one_d_b

Out[67]:
   array([10, 20, 30, 40, 50])

In [68]:
   # stack the two columns
   np.column_stack((one_d_a, one_d_b))

Out[68]:
   array([[ 0, 10],
          [ 1, 20],
          [ 2, 30],
          [ 3, 40],
          [ 4, 50]])

Row stacking returns a new array where each one-dimensional array forms one of the rows of the new array:

In [69]:
   # stack along rows
   np.row_stack((one_d_a, one_d_b))

Out[69]:
   array([[ 0,  1,  2,  3,  4],
          [10, 20, 30, 40, 50]])
..................Content has been hidden....................

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