w3resource

NumPy: numpy.stack() function

numpy.stack() function

The numpy.stack() function is used to join a sequence of arrays along a new axis.
The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

numpy.stack() is useful when working with machine learning models that require a single input array. For example, when working with image data, it is common to have multiple image files that need to be joined into a single array for processing by the machine learning model.

Syntax:

numpy.stack(arrays, axis=0, out=None)
NumPy manipulation: stack() function

Parameters:

Name Description Required /
Optional
arrays Each array must have the same shape. Required
axis The axis in the result array along which the input arrays are stacked. Optional
out If provided, the destination to place the result. The shape must be correct, matching that of what stack would have returned if no out argument were specified. Optional

Return value:

stacked : ndarray The stacked array has one more dimension than the input arrays.

Example: Stacking arrays vertically using numpy.stack() function

>>> import numpy as np
>>> arrays = [np.random.randn(2, 3)for _ in range(8)]
>>> np.stack(arrays, axis=0).shape
(8, 2, 3)

The above code imports the NumPy library and generates a list of 8 arrays using a for loop. Each of the 8 arrays has 2 rows and 3 columns of random numbers generated using the np.random.randn() function.
The np.stack() function is then used to stack these arrays vertically along the first axis, which is specified by the 'axis' parameter set to 0. The resulting stacked array has a shape of (8, 2, 3) where the first dimension represents the number of arrays stacked, and the remaining two dimensions represent the shape of each of the arrays in the list.

Example: Stacking arrays in NumPy using the np.stack() function

>>> import numpy as np
>>> np.stack(arrays, axis=1).shape
(2, 8, 3)
>>>
>>> np.stack(arrays, axis=2).shape
(2, 3, 8)
>>>
>>> x = np.array([2, 3, 4])
>>> y = np.array([3, 4, 5])
>>> np.stack((x, y))
array([[2, 3, 4],
       [3, 4, 5]])

In the above code:

  • In the first example, we stack the same list of arrays along the second axis (axis=1) to get an array of shape (2,8,3). Similarly, in the second example, we stack the arrays along the third axis (axis=2) to get an array of shape (2,3,8).
  • In the third example, we stack two 1-D arrays x and y along the first axis (axis=0) to get a 2-D array of shape (2,3).

Pictorial Presentation:

NumPy manipulation: stack() function

Example: Numpy numpy.stack() function with axis parameter

>>> import numpy as np
>>> x = np.array([2, 3, 4])
>>> y = np.array([3, 4, 5])
>>> np.stack((x, y))
array([[2, 3, 4],
       [3, 4, 5]])
>>> np.stack((x, y), axis=-1)
array([[2, 3],
       [3, 4],
       [4, 5]])

In the above code:

  • In the first example, two 1-dimensional numpy arrays x and y are stacked together using the np.stack((x, y)) function, which by default stacks them along axis=0, producing a 2x3 array.
  • In the second example, the same arrays are stacked along axis=-1 by specifying np.stack((x, y), axis=-1). This results in a 3x2 array where the two input arrays are transposed and stacked along the second axis.

Pictorial Presentation:

NumPy manipulation: stack() function

Python - NumPy Code Editor:

Previous: concatenate()
Next: column_stack()



Follow us on Facebook and Twitter for latest update.