Skip to content

NumPy Stack: Join NumPy Arrays Along Different Axes

NumPy Stack Join NumPy Arrays Along Different Axes Cover Image

In this tutorial, you’ll learn how to use the NumPy stack() function to join NumPy arrays along various axes. NumPy is an essential Python library for anyone working with data in Python. The NumPy stack() function allows you to combine NumPy arrays in different ways, along NumPy axes.

By the end of this tutorial, you’ll have learned:

  • How the NumPy stack() function works
  • How to stack NumPy arrays row-wise
  • How to stack NumPy arrays column-wise

Understanding the NumPy stack Function

Before diving into how to use the NumPy stack() function, you’ll learn about the different parameters the function has to offer. We’ll take a look at what the function looks like and how to use the different parameters to customize the behavior of the function.

# Understanding the np.stack() Function
np.stack(
   arrays=,
   axis=0,
   out=None
)

From the code block above, we can see that the function takes three parameters – only one of which is required. Let’s explore these parameters in a bit more detail:

  • arrays= accepts a sequence of array-like structures that are all of the same size
  • axis=0 accepts an optional integer. 0 indicates the first axis, while -1 indicates the last dimension
  • out=None provides the destination to place the resulting array, where the shape must match

Now that you have an understanding of how the function works, let’s take a look at some examples of how to use the function.

Stacking Arrays Row-Wise in NumPy

In this section, you’ll learn how to stack NumPy arrays row-wise. This means that you’re essentially concatenating arrays, array by array. By this, we stack our arrays along the 0th axis, which is the default parameter of the np.stack() function.

Let’s see what this looks like by looking at an example:

# Stacking NumPy Arrays Row-Wise
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

stacked = np.stack(arrays=[arr1, arr2])
print(stacked)

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

Let’s break down what our code above is doing:

  1. We created two arrays, arr1 and arr2 which contained the numbers 1-3 and 4-6 respectively
  2. We then created a new arr, stacked, which was the result of using the np.stack() function for the two arrays

By default, NumPy stacks the two arrays on the 0th axis. In this case, this means that we’re joining the two arrays in a row-wise manner. In the following section, you’ll learn how to modify the behavior of this and learn how to stack arrays along different axes.

Stacking Arrays Column-Wise in NumPy

In this section, you’ll learn how to stack arrays along different axes. We can think of this as stacking the arrays column-wise. This is better explained with an example. Let’s look at stacking our two arrays along the 1st axis:

# Stacking Arrays Column-Wise with np.stack()
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

stacked = np.stack(arrays=[arr1, arr2], axis=1)

print(stacked)

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

We can see that the arrays are stacked by each element, with resulting arrays containing the values from each “column” of the original arrays.

One of the great features of the np.stack() function that we can use is to stack the values along the last axis by passing in -1. In this case, our last axis is 1, but to be safe we can simply pass in -1. Let’s see what this looks like in practice:

# Stacking Arrays Along the Last Axis
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

stacked = np.stack(arrays=[arr1, arr2], axis=-1)

print(stacked)

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

In this case, this returns the same array. This allows us to execute our code more safely by ensuring that we use an axis that actually exists!

Conclusion

In this tutorial, you learned how to use the NumPy stack function, which allows you to join arrays along different axes. First, you learned how the syntax of the function works. Then, you learned how to use the function to join arrays along different axes. Finally, you learned how to safely join arrays by using the negative indexing, -1.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *