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
= np.array([[1, 2], [3, 4]])
arr1 = np.array([[5, 6], [7, 8]])
arr2 = np.concatenate((arr1, arr2), axis=0)
vertical_stack print("Vertical Concatenation:\n", vertical_stack)
= np.array([[1, 2], [3, 4]])
arr3 = np.array([[5, 6], [7, 8]])
arr4 = np.concatenate((arr3, arr4), axis=1)
horizontal_stack print("\nHorizontal Concatenation:\n", horizontal_stack)
= np.array([1,2,3])
arr5 = np.array([[4],[5],[6]])
arr6 = np.concatenate((arr5.reshape(1,3), arr6), axis=0)
vertical_stack_diff 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
= np.array([1, 2, 3])
arr1 = np.array([4, 5, 6])
arr2
= np.vstack((arr1, arr2))
vertical_stack print("Vertical Stack using vstack:\n", vertical_stack)
= np.array([[1],[2],[3]])
arr3 = np.array([[4],[5],[6]])
arr4
= np.hstack((arr3, arr4))
horizontal_stack 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
= np.array([[[1,2],[3,4]]])
arr1 = np.array([[[5,6],[7,8]]])
arr2 = np.dstack((arr1, arr2))
depth_stack 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.