Numpy stack – Python NumPy stack() Function

Numpy Module:

Numpy stack: NumPy is a Python module that is used to work with arrays.

It also has functions for working with linear algebra, the Fourier transform, and matrices.

Travis Oliphant designed NumPy in 2005. It is an open-source project that you are free to use.

NumPy is an abbreviation for Numerical Python.

Uses of NumPy Module:

Lists in Python serve the same purpose as arrays, although they are slower to process.

NumPy’s goal is to provide array objects that are up to 50 times faster than ordinary Python lists.

The array object in NumPy is named ndarray, and it comes with a slew of helper methods that make working with ndarray a breeze.

Arrays are often utilized in data science, where speed and resources are critical.

NumPy stack() Function

To combine or join a sequence of arrays along a new axis, use the stack() function.
The axis argument defines the index of the new axis in the dimensions of the result. For example, if axis=0, it is the first dimension, and if axis=-1, it is the last dimension.

Syntax:

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

Parameters

arrays: This is required. The shape of each array must be the same.

axis: This is optional. The axis of the result array that the input arrays are stacked along.

out: This is optional. If specified, the location where the result should be placed. It specifies the output of the result. The shape must be the same as what stack would have returned if no out parameter was given.

Return Value:

The stacked array is returned. It is one dimension larger than the input arrays.

NumPy stack() Function in Python

Example1

For multi-dimensional Arrays

Approach:

  • Import numpy module using the import keyword.
  • Give the random multi-dimensional array as static input using the array() function and store it in a variable.
  • Give the other random multi-dimensional array as static input using the array() function and store it in another variable.
  • Stack the above given two arrays along the axis=0(first dimension) by passing them as arguments to the stack() function and store it in a variable.
  • Stack the above given two arrays along the axis=1(last dimension) by passing them as arguments to the stack() function and store it in another variable.
  • Print the given first array.
  • Print the given second array.
  • Print the stacked array along axis=0(first dimension).
  • Print the stacked array along axis=1(last dimension).
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the random multi-dimensional array as static input
# using the array() function and store it in a variable.
gvn_arry1 = np.array([[5, 7], [1, 8]])
# Give the other random multi-dimensional array as static input
# using the array() function and store it in another variable.
gvn_arry2 = np.array([[4, 15], [3, 12]])

# Stack the above given two arrays along the axis=0(first dimension) by passing
# them as arguments to the stack() function and store it in a variable.
rslt_stackarr1 = np.stack((gvn_arry1, gvn_arry2), axis=0)
# Stack the above given two arrays along the axis=1(last dimension) by passing
# them as arguments to the stack() function and store it in another variable.
rslt_stackarr2 = np.stack((gvn_arry1, gvn_arry2), axis=1)

# Print the given first array
print("The given first array:\n", gvn_arry1)
# Print the given second array
print("The given second array:\n", gvn_arry2)
# Print the stacked array along axis=0(first dimension)
print("The stacked array along axis=0(first dimension):\n", rslt_stackarr1)
# Print the stacked array along axis=1(last dimension)
print("The stacked array along axis=1(last dimension):\n", rslt_stackarr2)

Output:

The given first array:
[[5 7]
 [1 8]]
The given second array:
[[ 4 15]
 [ 3 12]]
The stacked array along axis=0(first dimension):
[[[ 5 7]
 [ 1 8]]

[[ 4 15]
 [ 3 12]]]
The stacked array along axis=1(last dimension):
[[[ 5 7]
 [ 4 15]]

[[ 1 8]
 [ 3 12]]]

Example2

For 1-Dimensional Arrays

Approach:

  • Import numpy module using the import keyword.
  • Give the random one-dimensional array as static input using the array() function and store it in a variable.
  • Give the other random one-dimensional array as static input using the array() function and store it in another variable.
  • Stack the above given two arrays along the axis=0(first dimension) by passing them as arguments to the stack() function and store it in a variable.
  • Stack the above given two arrays along the axis=1(last dimension) by passing them as arguments to the stack() function and store it in another variable.
  • Print the given first array.
  • Print the given second array.
  • Print the stacked array along axis=0(first dimension).
  • Print the stacked array along axis=1(last dimension).
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the one-dimensional array as static input
# using the array() function and store it in a variable.
gvn_arry1 = np.array([10, 20, 30, 40])
# Give the other random one-dimensional array as static input
# using the array() function and store it in another variable.
gvn_arry2 = np.array([50, 60, 70, 80])

# Stack the above given two arrays along the axis=0(first dimension) by passing
# them as arguments to the stack() function and store it in a variable.
rslt_stackarr1 = np.stack((gvn_arry1, gvn_arry2), axis=0)
# Stack the above given two arrays along the axis=1(last dimension) by passing
# them as arguments to the stack() function and store it in another variable.
rslt_stackarr2 = np.stack((gvn_arry1, gvn_arry2), axis=1)

# Print the given first array
print("The given first array:\n", gvn_arry1)
# Print the given second array
print("The given second array:\n", gvn_arry2)
# Print the stacked array along axis=0(first dimension)
print("The stacked array along axis=0(first dimension):\n", rslt_stackarr1)
# Print the stacked array along axis=1(last dimension)
print("The stacked array along axis=1(last dimension):\n", rslt_stackarr2)

Output:

The given first array:
[10 20 30 40]
The given second array:
[50 60 70 80]
The stacked array along axis=0(first dimension):
[[10 20 30 40]
 [50 60 70 80]]
The stacked array along axis=1(last dimension):
[[10 50]
 [20 60]
 [30 70]
 [40 80]]