numpy.stack() – The NumPy stack Python Function

Introduction to NumPy stack

To join a sequence of same-dimension arrays along a new axis, use the numpy.stack() function. The axis parameter defines the new axis index in the result’s dimensions. For example, if axis=0, the first dimension will be 0, and if axis=-1, the last dimension would be 1.

Syntax:

numpy.stack(arrays, axis)

Parameters:

  • arrays : [array_like] Sequence of arrays of the same shape.
  • axis : [int] Axis in the resultant array along which the input arrays are stacked.

Return: [stacked ndarray] The stacked array of the input arrays has one more dimension than the input arrays.

Code Examples of NumPy stack

Example 01:

# welcome to softhunt.net
# Python program explaining
# stack() function

import numpy as np

# input array
in_arr1 = np.array([1, 3, 5] )
print ("1st Input array : \n", in_arr1)

in_arr2 = np.array([2, 4, 6] )
print ("2nd Input array : \n", in_arr2)

# Stacking the two arrays along axis 0
out_arr1 = np.stack((in_arr1, in_arr2), axis = 0)
print ("Output stacked array along axis 0:\n ", out_arr1)

# Stacking the two arrays along axis 1
out_arr2 = np.stack((in_arr1, in_arr2), axis = 1)
print ("Output stacked array along axis 1:\n ", out_arr2)

Output:

1st Input array : 
 [1 3 5]
2nd Input array : 
 [2 4 6]
Output stacked array along axis 0:
  [[1 3 5]
 [2 4 6]]
Output stacked array along axis 1:
  [[1 2]
 [3 4]
 [5 6]]

Example 02:

# welcome to softhunt.net
# Python program explaining
# stack() function

import numpy as np

# input array
in_arr1 = np.array([[1, 3, 5], [-1, -3, -5]] )
print ("1st Input array : \n", in_arr1)

in_arr2 = np.array([[2, 4, 6], [-2, -4, -6]] )
print ("2nd Input array : \n", in_arr2)

# Stacking the two arrays along axis 0
out_arr1 = np.stack((in_arr1, in_arr2), axis = 0)
print ("Output stacked array along axis 0:\n ", out_arr1)

# Stacking the two arrays along axis 1
out_arr2 = np.stack((in_arr1, in_arr2), axis = 1)
print ("Output stacked array along axis 1:\n ", out_arr2)

# Stacking the two arrays along last axis
out_arr3 = np.stack((in_arr1, in_arr2), axis = -1)
print ("Output stacked array along last axis :\n ", out_arr3)

Output:

1st Input array : 
 [[ 1  3  5]
 [-1 -3 -5]]
2nd Input array : 
 [[ 2  4  6]
 [-2 -4 -6]]
Output stacked array along axis 0:
  [[[ 1  3  5]
  [-1 -3 -5]]

 [[ 2  4  6]
  [-2 -4 -6]]]
Output stacked array along axis 1:
  [[[ 1  3  5]
  [ 2  4  6]]

 [[-1 -3 -5]
  [-2 -4 -6]]]
Output stacked array along last axis :
  [[[ 1  2]
  [ 3  4]
  [ 5  6]]

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

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

How do I stack NumPy columns?

The numpy.column_stack() function is use to create a 2-D array by stacking 1-D arrays as columns. To construct a single 2-D array, it stacks a sequence of 1-D arrays as columns. 2-D arrays are stacked in the same way as the hstack function does.

How do you stack an array depth wise in Python?

The dstack() function is use to stack arrays in depth order (along third axis). After reshaping 2-D arrays of shape (M, N) to (M, N,1) and 1-D arrays of shape (N,) to (N, N,1), this is identical to concatenation along the third axis (1, N,1). Rebuilds arrays that have been split by split.

This function works well with arrays of up to three dimensions. For pixel data having a height (first axis), width (second axis), and r/g/b channels, for example (third axis). More broad stacking and concatenation operations are provided by the functions concatenate, stack, and block.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.concatenate()

Leave a Comment