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.

  1. import numpy as np
  2. first_arr = np.array(['s','o','u'])
  3. print('First 2D array being concatenated:', first_arr)
  4. second_arr = np.array(['r','c','e'])
  5. print('Second 2D array being concatenated:\n', first_arr)
  6. array1 = np.stack((first_arr, second_arr), axis=1)
  7. array2 = np.stack((first_arr, second_arr), axis=1)
  8. #concatenating arrays
  9. print('Result of Concatenate() function:\n', array1)
  10. #stacking arrays
  11. print('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)

  1. import numpy as np
  2. first_arr = np.array(['s','o','u'])
  3. print('First 1D array being stacked ', first_arr)
  4. second_arr = np.array(['r','c','e'])
  5. print('Second 1D array being stacked:', second_arr)
  6. #row stacking
  7. array = np.hstack((first_arr, second_arr))
  8. 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)

  1. import numpy as np
  2. first_arr = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
  3. print('First 2D array being stacked:', first_arr)
  4. second_arr = np.array([[12,13,14,15,16,17],[18,19,20,21,22,23]])
  5. print('Second 2D array being stacked:', second_arr)
  6. #row stacking
  7. array_res = np.hstack((first_arr, second_arr))
  8. 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.

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

  1. import numpy as np
  2. first_arr = np.array(['s','o','u'])
  3. print('First 1D array being stacked:', first_arr)
  4. second_arr = np.array(['r','c','e'])
  5. print('Second 1D array being stacked:', second_arr)
  6. #column stacking
  7. array = np.vstack((first_arr, second_arr))
  8. 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.

  1. import numpy as np
  2. first_arr = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
  3. print('First 2D array being stacked:', first_arr)
  4. second_arr = np.array([[12,13,14,15,16,17],[18,19,20,21,22,23]])
  5. print('Second 2D array being stacked:', second_arr)
  6. #column stacking
  7. array_res = np.vstack((first_arr, second_arr))
  8. 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.

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

  1. import numpy as np
  2. first_arr = np.array(['s','o','u'])
  3. print('First 1D array being stacked:', first_arr)
  4. second_arr = np.array(['r','c','e'])
  5. print('Second 1D array being stacked:',second_arr)
  6. #height stacking
  7. array = np.dstack((first_arr, second_arr))
  8. 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).

  1. import numpy as np
  2. first_arr = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
  3. print('First 2D array being stacked:', first_arr)
  4. second_arr = np.array([[12,13,14,15,16,17],[18,19,20,21,22,23]])
  5. print('Second 2D array being stacked:', second_arr)
  6. #height stacking
  7. array_res = np.dstack((first_arr, second_arr))
  8. 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)

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

Add new comment