NumPy - Stacking Arrays

Stacking NumPy Array

Stacking arrays in NumPy refers to combining multiple arrays along a new dimension, creating higher-dimensional arrays. This is different from concatenation, which combines arrays along an existing axis without adding new dimensions.

NumPy provides several functions to achieve stacking. They are as follows −

  • Using numpy.stack() Functiom
  • Using numpy.vstack() Function
  • Using numpy.hstack() Function
  • Using numpy.dstack() Function
  • Using numpy.column_stack() Function

Stacking Arrays Using stack() Function

We can use the stack() function in NumPy to stack a sequence of arrays along a new axis, creating a new dimension in the result.

Unlike numpy.concatenate() function, which combines arrays along an existing axis, numpy.stack() function adds a new axis at the specified position to the arrays being stacked.

Following is the syntax of the stack() function in NumPy −

np.stack(arrays, axis=0)

Where,

  • arrays − A sequence of arrays to be stacked.
  • axis − The axis along which to stack the arrays. The default is 0, which adds a new first axis.

Example: Stacking 1D Arrays

In the below example, we are stacking three 1D arrays along a new axis (axis 0) using the numpy.stack() function, resulting in a 2D array −

import numpy as np

# arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr3 = np.array([7, 8, 9])

# Stack arrays along a new axis
stacked_arr = np.stack((arr1, arr2, arr3), axis=0)
print("Stacked Array along a new axis (Axis 0):")
print(stacked_arr)

Following is the output obtained −

Stacked Array along a new axis (Axis 0):
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Example: Changing the Axis

The “axis” parameter in numpy.stack() function determines where the new axis is inserted. By changing the value of axis, you can control how the arrays are stacked −

import numpy as np

# arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr3 = np.array([7, 8, 9])

# Stack arrays along axis 1
stacked_arr = np.stack((arr1, arr2, arr3), axis=1)
print("Stacked Array along Axis 1:")
print(stacked_arr)

This will produce the following result −

Stacked Array along Axis 1:
[[1 4 7]
 [2 5 8]
 [3 6 9]]

Example: Stacking Multi-dimensional Arrays

The numpy.stack() function can also be used to stack multi-dimensional arrays. The function adds a new axis to the higher-dimensional arrays and stacks them accordingly.

In here, we are stacking two 2D arrays −

import numpy as np

# 2D arrays
arr1 = np.array([[1, 2],
                 [3, 4]])

arr2 = np.array([[5, 6],
                 [7, 8]])

# Stack arrays along a new axis
stacked_arr = np.stack((arr1, arr2), axis=0)
print("Stacked 2D Arrays along a new axis (Axis 0):")
print(stacked_arr)

Following is the output of the above code −

Stacked 2D Arrays along a new axis (Axis 0):
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

Stacking Arrays Using column_stack() Function

The numpy.column_stack() function in NumPy is used to stack 1D arrays as columns into a 2D array or to stack 2D arrays column-wise. This function provides a way to combine arrays along the second axis (axis=1), effectively increasing the number of columns in the resulting array.

Following is the syntax −

np.column_stack(tup)

Where, tup is a tuple of arrays to be stacked. The arrays can be either 1D or 2D, but they must have the same number of rows.

Example: Stacking 1D arrays as columns

In the example below, we are stacking two two 1D arrays as columns into a 2D array using the NumPy column_stack() function −

import numpy as np

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

# Column-stack 1D arrays
stacked_arr_1d = np.column_stack((arr1, arr2))

print("Stacked 1D arrays as 2D array:")
print(stacked_arr_1d)

We get the output as shown below −

Stacked 1D arrays as 2D array:
[[1 4]
 [2 5]
 [3 6]]

Example: Stacking 2D arrays column-wise

In here, we are stacking two 2D arrays column-wise using the NumPy column_stack() function −

import numpy as np
# 2D arrays
arr3 = np.array([[1, 2],
                 [3, 4]])
arr4 = np.array([[5, 6],
                 [7, 8]])

# Column-stack 2D arrays
stacked_arr_2d = np.column_stack((arr3, arr4))

print("Stacked 2D arrays column-wise:")
print(stacked_arr_2d)

Following is the output obtained −

Stacked 2D arrays column-wise:
[[1 2 5 6]
 [3 4 7 8]]

Vertical Stacking

We can also stack arrays vertically (row-wise) using the vstack() function in NumPy. It is equivalent to using numpy.concatenate() function with “axis=0”, where arrays are concatenated along the first axis.

This results in an array with an increased number of rows, combining multiple arrays row-wise. Following is the syntax −

numpy.vstack(tup)

Where, tup is a tuple of arrays to be stacked vertically. All arrays must have the same number of columns.

Example

In the example below, we are stacking two arrays vertically using the NumPy vstack() function −

import numpy as np

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

arr2 = np.array([[7, 8, 9],
                 [10, 11, 12]])

# Stack arrays vertically
stacked_arr = np.vstack((arr1, arr2))

print("Vertically Stacked Array:")
print(stacked_arr)

The output obtained is as shown below −

Vertically Stacked Array:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Horizontal Stacking

We can stack arrays horizontally (column-wise) using the hstack() function in NumPy. It is equivalent to using numpy.concatenate() function with “axis=1”, where arrays are concatenated along the second axis for 2D arrays.

This results in an array with an increased number of columns, combining multiple arrays column-wise. Following is the syntax −

numpy.hstack(tup)

Where, tup is a tuple of arrays to be stacked horizontally. All arrays must have the same number of rows.

Example

In the example below, we are stacking two arrays horizontally using the NumPy hstack() function −

import numpy as np

# arrays
arr1 = np.array([[1, 2],
                 [3, 4]])

arr2 = np.array([[5, 6],
                 [7, 8]])

# Stack arrays horizontally
stacked_arr = np.hstack((arr1, arr2))

print("Horizontally Stacked Array:")
print(stacked_arr)

After executing the above code, we get the following output −

Horizontally Stacked Array:
[[1 2 5 6]
 [3 4 7 8]]

Depth Stacking

The numpy.dstack() function is used to stack arrays along the third dimension, also known as the depth dimension. This combines arrays depth-wise, effectively creating a new dimension in the resulting array.

It is particularly useful when you want to combine multiple 2D arrays into a single 3D array. Following is the syntax −

np.dstack(tup)

Where, tup is a tuple of arrays to be stacked along the third dimension. All arrays must have the same shape in the first two dimensions.

Example

In this example, we are stacking two arrays along the third dimension using the NumPy dstack() function −

import numpy as np

# arrays
arr1 = np.array([[1, 2],
                 [3, 4]])

arr2 = np.array([[5, 6],
                 [7, 8]])

# Stack arrays along the third dimension
stacked_arr = np.dstack((arr1, arr2))

print("Depth-wise Stacked Array:")
print(stacked_arr)

The result produced is as follows −

Depth-wise Stacked Array:
[[[1 5]
  [2 6]]

 [[3 7]
  [4 8]]]
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team