# NumPy Stack

In this tutorial you will learn:

• What is NumPy Stack function and its uses?
• Results of hstack on 1D, 2D and 3D arrays
• Results of vstack on 1D, 2D and 3D arrays
• Results of dstack on 1D, 2D and 3D arrays

NumPy Stack Function

Just like concatenate function, stack function is another function for joining or concatenation of the arrays but with a main difference between them is that the stacking is always done along a new axis. If the concatenation has to be done along the same axis, it is better to use concatenate function. There are 4 types of stack functions

• Plain Stack (Stack)
• H stack
• V stack
• D stack

If we want to use stack function to stack over the values of one array on another along the same axis, then we can use the simple stack function. Just like concatenate function stack function takes in two mandatory parameters, which includes two arrays being concatenated while “axis” is an optional parameter, which if not passed explicitly is taken as 0.

In this example I will stack two 2D array along the same axis, here you can observe that the result of concatenate and stack is same as we have declared axis as 1.

`import numpy as npfirst_arr = np.array(['s','o','u'])print('First 2D array being concatenated:', first_arr)second_arr = np.array(['r','c','e'])print('Second 2D array being concatenated:\n', first_arr)array1 = np.stack((first_arr, second_arr), axis=1)array2 = np.stack((first_arr, second_arr), axis=1)#concatenating arraysprint('Result of Concatenate() function:\n', array1)#stacking arraysprint('Result of Stack() function:\n', array2)`

H Stack

For stacking along the rows, hstack() function is used. This function takes in the first two arguments same as stack() while third argument “axis” is not required

In this example we will stack two 1D arrays (3,) and we can observe that the dimension of the stacked array is (1,6)

`import numpy as npfirst_arr = np.array(['s','o','u'])print('First 1D array being stacked ', first_arr)second_arr = np.array(['r','c','e'])print('Second 1D array being stacked:', second_arr)#row stackingarray = np.hstack((first_arr, second_arr))print('Result of row stacking function:\n', array)`

In this example we will row stack two 2D arrays, the array being stacked are (2,6) while result after stacking is (2,12)

`import numpy as npfirst_arr = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])print('First 2D array being stacked:', first_arr)second_arr = np.array([[12,13,14,15,16,17],[18,19,20,21,22,23]])print('Second 2D array being stacked:', second_arr)#row stackingarray_res = np.hstack((first_arr, second_arr))print('Result of row stacking function:\n', array_res)`

In this example I am row stacking 2 3D array and you can clearly observe the changes in result dimensions.

`import numpy as npfirst_arr = np.array([[[1,2,3,4,5,6],[7,8,9,10,11,12],[24,25,26,27,28,29]]])print('First 3D array being stacked:', first_arr)second_arr = np.array([[[12,13,14,15,16,17],[18,19,20,21,22,23],[30,31,32,33,34,35]]])print('Second 3D array being stacked:', second_arr)#row stackingarray_res = np.hstack((first_arr, second_arr))print('Result of row stacking function:\n', array_res)`

V Stack

For stacking along the columns, vstack() function is used. This function takes in the first two arguments same as stack() while third argument “axis” is not required.

In this example we will be column stacking two 1D arrays (3,) resulting into an array(2,3). Here you can observe that the columns are stacked over one another.

`import numpy as npfirst_arr = np.array(['s','o','u'])print('First 1D array being stacked:', first_arr)second_arr = np.array(['r','c','e'])print('Second 1D array being stacked:', second_arr)#column stackingarray = np.vstack((first_arr, second_arr))print('Result of column stacking function:\n', array)`

In this example we will be column stacking two 2D arrays (2,6) which will be resulting into (4,6). Here you can observe that after column stacking the number of rows are doubled.

`import numpy as npfirst_arr = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])print('First 2D array being stacked:', first_arr)second_arr = np.array([[12,13,14,15,16,17],[18,19,20,21,22,23]])print('Second 2D array being stacked:', second_arr)#column stackingarray_res = np.vstack((first_arr, second_arr))print('Result of column stacking function:\n', array_res)`

In this example I will be column stacking two 3D arrays and the changes in dimensions of resultant array can be easily observed.

`import numpy as npfirst_arr = np.array([[[1,2,3,4,5,6],[7,8,9,10,11,12],[24,25,26,27,28,29]]])print('First 3D array being stacked:', first_arr)second_arr = np.array([[[12,13,14,15,16,17],[18,19,20,21,22,23],[30,31,32,33,34,35]]])print('Second 3D array being stacked:', second_arr)#column stackingarray_res = np.vstack((first_arr, second_arr))print('Result of column stacking function:\n', array_res)`

D Stack

For stacking along the height/ depth, dstack() function is used. This function takes in the first two arguments same as stack() while third argument “axis” is not required.

In this example we will be height/ depth stacking two 1D arrays (3,) resulting into an array(1,3,2). Here you can observe that the stacking is carried out in depth and array has been reshaped from 2D to 3D array.

`import numpy as npfirst_arr = np.array(['s','o','u'])print('First 1D array being stacked:', first_arr)second_arr = np.array(['r','c','e'])print('Second 1D array being stacked:',second_arr)#height stackingarray = np.dstack((first_arr, second_arr))print('Result of height stacking function:\n', array`

In this example we will be height stacking two 2D arrays (2,6) which will be resulting into a 3D array (2,6,2).

`import numpy as npfirst_arr = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])print('First 2D array being stacked:', first_arr)second_arr = np.array([[12,13,14,15,16,17],[18,19,20,21,22,23]])print('Second 2D array being stacked:', second_arr)#height stackingarray_res = np.dstack((first_arr, second_arr))print('Result of height stacking function:\n', array_res)`

In this example we will be height stacking two 3D array(1,3,6) and their stacking will result into array with dimensions(1,3,12)

`import numpy as npfirst_arr = np.array([[[1,2,3,4,5,6],[7,8,9,10,11,12],[24,25,26,27,28,29]]])print('First 3D array being stacked:', first_arr)second_arr = np.array([[[12,13,14,15,16,17],[18,19,20,21,22,23],[30,31,32,33,34,35]]])print('Second 3D array being stacked:', second_arr)#height stackingarray_res = np.dstack((first_arr, second_arr))print('Result of height stacking function:\n', array_res)`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.