How to Use NumPy stack() in Python

Spread the love

NumPy stack() function is used to stack or join the sequence of given arrays along a new axis. It generates a single array by taking elements from the sequence of arrays having the same shape. The returned array has 1 more dimension than the input arrays for example we are stacked two 1-D arrays using this function it will return the 2-D NumPy array.

In this article, I will explain NumPy stack() function syntax and using its parameters how we can stack the sequence of arrays along the new axis with examples.

1. Quick Examples of Python NumPy stack()

If you are in a hurry, below are some quick examples of how to use Python NumPy stack() function.


# Below are the quick examples

# Example 1 : Use stack() function get the 2-d array
arr = np.array([1, 2, 3])
arr1 = np.array([4, 5, 6])
arr2 = np.stack((arr, arr1), axis = 0)

# Example 2 : Get the 2-D stacked array
arr2 = np.stack((arr, arr1), axis = 1)

# Example 3 : Get the stacked array along = -1
arr2 = np.stack((arr, arr1), axis = -1)

# Example 4 : Get the stacked array of 3-D 
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr1 = np.array([[2, 4, 6],[5, 3, 1]])
arr2 = np.stack((arr, arr1), axis = 0)

# Example 5 : Get the stacked array of 3-D
arr2 = np.stack((arr, arr1), axis = 1)

# Example 6 : Get the stacked array of 3-D
arr2 = np.stack((arr, arr1), axis = -1)

2. Syntax of NumPy stack()

Following is the syntax of the stack() function.


# Syntax of Use stack() 
numpy.stack(arrays, axis=0, out=None)

2.1 Parameters of the stack()

Following is the parameter of the stack().

  • arr : It contains a sequence of arrays of the same shape. these arrays are to be stacked as a parameter and return a single NumPy array.
  • axis : It defines the index of the new axis in the dimensions of the result. For example, if axis=0 it will define the first dimension and if axis=-1 it will define the last dimension.

2.2 Return Value of the stack()

It returns the stacked array, where the dimensions are 1 more than the input arrays. of the given arrays.

3. Usage of the NumPy stack()

NumPy stack() function is used to stack the sequence of arrays along a new axis. In order to join two arrays, Python NumPy module provides different types of functions which are concatenate(), stack(), vstack(), and hstack().

Below I have provided an image that explains how stack() function works, I wish it will give you a better understanding.

NumPy stack
numpy.stack(x, y)

Create two 1-D NumPy arrays using numpy.array function and pass them into this function along axis = 0, it will return the stacked array of 2-D array.


import numpy as np
# Use stack() function get the 2-d array
arr = np.array([1, 2, 3])
arr1 = np.array([4, 5, 6])
arr2 = np.stack((arr, arr1), axis = 0)
print(arr2)

# Output :
# [[1 2 3]
# [4 5 6]]

This time we pass the arrays along with axis = 1 into this function, it will return the stacked array of 2-D NumPy array.


# Get the 2-D stacked array
arr2 = np.stack((arr, arr1), axis = 1)
print(arr2)

# Output :
# [[1 4]
# [2 5]
# [3 6]]
NumPy stack
numpy.stack(x, y)

Here, we have passed last axis(-1) into this function. It will return the 2-D array, this shape is same as from the above because for 1-D arrays last axis = 1.


# Get the stacked array along axis = -1
arr2 = np.stack((arr, arr1), axis = -1)
print(arr2)

# Output:
# [[1 4]
# [2 5]
# [3 6]]

4. Stack the 2-D NumPy Arrays

We can stack the 2-D arrays and get the stacked array using this function, it will return the 3-D array.


# Get the stacked array of 3-D 
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr1 = np.array([[2, 4, 6],[5, 3, 1]])
arr2 = np.stack((arr, arr1), axis = 0)
print(arr2)

# Output :
# [[[1 2 3]
#  [4 5 6]]

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

4.1 Stack the Arrays along Axis = 1

Stack the 2-D arrays along the axis = 1, it will return the stacked array of 3- The d array. in which 1st dimension has 1st-row elements and the second dimension has 2nd-row elements.


# Get the stacked array of 3-D
arr2 = np.stack((arr, arr1), axis = 1)
print(arr2)

# Output:
# [[[1 2 3]
# [2 4 6]]

# [[4 5 6]
 # [5 3 1]]]

4.2 stack the Arrays along Axis = -1

Stack the 2-D arrays along the last axis(-1), it will return the stacked array of 3-D array, in which the 1st dimension has 1st column elements and the second dimension has 2nd column elements.


# Get the stacked array of 3-D
arr2 = np.stack((arr, arr1), axis = -1)
print(arr2)

# Output:
# [[[1 2]
#  [2 4]
# [3 6]]

# [[4 5]
#  [5 3]
#  [6 1]]]

5. Conclusion

In this article, I have explained numpy.stack() and using this how we can stack the sequence of given arrays into a single array along a new axis with examples.

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing How to Use NumPy stack() in Python