What is the stack() Function in NumPy?

Learn via video courses
Topics Covered

Overview

A versatile Python library called NumPy offers a wide range of functions for manipulating arrays. It greatly simplifies our jobs as developers. Just like NumPy, data structures are equally important for a Software Developer.

Data structures are a particular way of arranging information on a computer in a specialized style so that it may be processed, stored, and retrieved efficiently. They are a method of handling data that makes it simple to use. In this article, we will look at stack, a very important and popular data structure used in real-world problems using NumPy.

Introduction

As discussed earlier, a stack is a linear data structure that helps developers perform operations in order. This order is the Last In First Out order. To sum it up, the element that enters the stack in the beginning, is the last one to be popped out. Conversely, the element that is inserted into the stack recently is the first one to be popped out.

A stack in the actual world would be a collection of books. The first book stacked will be the last to be removed from the stack. The first book to be taken off the stack would be the one that has been there most recently.

numpy.stack() in Python

The numpy stack() function is used to combine a series of arrays along a new axis. The numPy concatenate() function is also used to combine two or more arrays, but by using numPy stack(), we can combine arrays along a new axis.

  • Syntax

The syntax for numpy stack() function is:

numpy.stack(arr, axis = 0, out = None)

  • Parameters

    The parameters that the NumPy stack() function takes in are:

    • arr: This mandatory parameter represents a singular or a sequence of arrays to be combined.
    • axis: This mandatory parameter represents the axis along which the input arrays will be stacked.
    • out: This optional parameter represents the location of the output array.
  • Return Value

The numPy stack() function returns a NumPy array.

  • Pictorial Representation

    In this section, we will look at the working of the NumPy stack() function for two one-dimensional NumPy arrays. Let's take two arrays; a = [5, 6, 7] and b = [6, 7, 8]. When we use the numpy stack() function, we will stack the two arrays, and a two-dimensional array will be returned.

    Output

  • Examples

    • Stacking Two 1-D Arrays

      We can use the NumPy stack() function to combine two one-dimensional NumPy arrays. If we want our arrays to be combined row-wise, we need to set the axis as 0, and for column-wise stacking, we need to set the axis as 1.

      Output

    • Stacking Two 2-D Arrays

      Just as we stacked two one-dimensional arrays, we can combine two two-dimensional arrays using the numpy stack() function.

      Output

    • Stacking more than two 2-D Arrays

      Using the numpy stack() function, we can combine more than two 2-D NumPy arrays row or column-wise.

      Output

    • Stacking two 3-D Arrays

      Just as we are stacking two 2-D NumPy arrays using numpy stack(), we can concatenate two three-dimensional NumPy arrays using numpy.stack() too.

      Output

Since NumPy stack() allows us to stack multiple arrays in any orientation (horizontal and vertical) using the axis parameter, NumPy also allows us to create specific stacks for horizontal and vertical stacking.

numpy.hstack() in Python

The NumPy hstack() function allows us to merge two or more than two NumPy arrays in a horizontal manner (column-wise) and return it as a NumPy array. When we use the NumPy hstack() function, we don't necessarily need to specify the axis parameter.

  • Syntax

The syntax for NumPy hstack() function is:

numpy.hstack(arr)

  • Parameters

    • arr: This mandatory parameter represents the specific array/tuple that is to be stacked horizontally for using NumPy hstack()`.
  • Return Value

The numpy hstack() function return a NumPy array.

  • Examples

    • Stacking two 1-D NumPy Arrays

    Output

    • Stacking 2-D NumPy Arrays

    Output

numpy.vstack() in Python

The numpy vstack() function allows us to merge two or more than two NumPy arrays in a vertical manner (row-wise) and return it as a NumPy array. Just as we didn't use the axis parameter in numpy hstack(), we don't have to use it here too.

  • Syntax

The syntax for numpy vstack() function is:

numpy.vstack(arr)

  • Parameters

    • arr: This mandatory parameter represents the specific array/tuple that is to be stacked vertically.
  • Return Value

The numpy vstack() function return a NumPy array.

  • Examples

    • Stacking two 1-D NumPy Arrays

    Output

    • Stacking 2-D NumPy Arrays

    Output

More Examples

  • Varying the Axis Parameter of the numpy.stack() function by 1, 0, and -1

    The axis parameter in numpy.stack() helps us to combine elements in a horizontal or vertical manner. In this example, we'll experiment with different values(0, 1, and -1) to see how our NumPy arrays stack up.

    Output

  • Verifying constraints of the axis parameter

    In this example, we will verify the constraints of the axis parameter using values such as 0, 1, and 2.

    In NumPy, arrays have 3 axes; 0, 1, and -1. If we pass 2 as a value, we will get a numpy.AxisError exception.

    Output

Conclusion

  • In this article, we learned about numpy.stack(), a function used to combine two or more two arrays along its axes using the axis parameter.
  • Using numpy.stack() function, we concatenated multiple 2-D and 3-D NumPy Arrays.
  • For stacking arrays in a specific manner (horizontal or vertical), we went through an in-depth understanding of the numpy.hstack() and numpy.vstack() functions.