NumPy Array Concatenation

numpy
Published

September 30, 2023

Understanding NumPy Array Concatenation

Concatenation in NumPy essentially joins arrays along an existing axis. The key is understanding the axis parameter – it determines where the arrays are joined. If axis=0 (the default), arrays are stacked vertically (row-wise). If axis=1, they are stacked horizontally (column-wise).

np.concatenate() – The Workhorse

The np.concatenate() function is the primary tool for array concatenation. It’s versatile and handles various array shapes as long as they are compatible along the concatenation axis.

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
vertical_stack = np.concatenate((arr1, arr2), axis=0)
print("Vertical Concatenation:\n", vertical_stack)

arr3 = np.array([[1, 2], [3, 4]])
arr4 = np.array([[5, 6], [7, 8]])
horizontal_stack = np.concatenate((arr3, arr4), axis=1)
print("\nHorizontal Concatenation:\n", horizontal_stack)

arr5 = np.array([1,2,3])
arr6 = np.array([[4],[5],[6]])
vertical_stack_diff = np.concatenate((arr5.reshape(1,3), arr6), axis=0)
print("\nConcatenating arrays of different shapes:\n", vertical_stack_diff)

Notice that for horizontal concatenation, the number of rows in the arrays must be the same. For vertical concatenation, the number of columns needs to match.

np.vstack() and np.hstack() – Specialized Functions

For simpler scenarios, NumPy offers np.vstack() (vertical stack) and np.hstack() (horizontal stack) which are essentially shortcuts to np.concatenate() with specific axis values.

import numpy as np

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

vertical_stack = np.vstack((arr1, arr2))
print("Vertical Stack using vstack:\n", vertical_stack)

arr3 = np.array([[1],[2],[3]])
arr4 = np.array([[4],[5],[6]])

horizontal_stack = np.hstack((arr3, arr4))
print("\nHorizontal Stack using hstack:\n", horizontal_stack)

These functions offer more readable code when you’re performing simple vertical or horizontal stacking.

np.dstack() – Stacking along the depth axis

For 3D arrays (or higher), np.dstack() allows concatenation along the depth axis (axis=2). This stacks arrays along the third dimension.

import numpy as np

arr1 = np.array([[[1,2],[3,4]]])
arr2 = np.array([[[5,6],[7,8]]])
depth_stack = np.dstack((arr1, arr2))
print("\nDepth Stack using dstack:\n", depth_stack)

Remember to ensure your arrays are compatible in terms of dimensions before attempting to concatenate them. Attempting to concatenate incompatible arrays will result in an error. Understanding the axis parameter is crucial for successfully concatenating your NumPy arrays.