What is Array Concatenation?
By concatenating a NumPy array, we mean combining two or more arrays across different dimensions and axes to create a new array. This is helpful for combining arrays either vertically (along rows) or horizontally (along columns), depending on your data pipeline's needs.
1. The concatenate() Function
The concatenate() function in NumPy joins together arrays along an existing axis. It allows you to combine arrays along rows or columns, depending on the axis parameter provided.
Syntax
Parameters
| Parameter | Description |
|---|---|
(a1, a2, ...) |
The sequence of arrays to be joined (provided as a tuple or list). These arrays must have the exact same shape along all axes except the one specified by axis. |
axis (optional) |
Specifies the axis along which the arrays will be joined. Default is 0 (along rows). Use 1 for columns, or higher numbers for multidimensional depth. |
out (optional) |
Allows specifying an existing destination array where the output will be stored. |
2. Concatenating Arrays Along Rows (axis=0)
Concatenating arrays along rows in NumPy means stacking arrays vertically, placing one array on top of another to create a larger array. This is useful for combining datasets or expanding data vertically (adding new sample rows).
To concatenate along rows, set the axis argument to 0 (which is also the default).
Example: Joining 2D Arrays Along Rows
Output:
Visual Breakdown (axis=0)
Rule: The number of columns (3) must be identical in both arrays.
3. Concatenating Arrays Along Columns (axis=1)
We can also concatenate arrays along columns in NumPy by placing one array beside another to extend data horizontally. This is useful for combining datasets where each array represents feature columns that need to be merged.
To concatenate along columns, set the axis argument to 1.
Example: Joining 2D Arrays Along Columns
Output:
Visual Breakdown (axis=1)
Rule: The number of rows (2) must be identical in both arrays.
4. Concatenating Arrays with Mixed Dimensions
Concatenating arrays with mixed dimensions in NumPy involves combining arrays that initially have different numbers of dimensions or shapes (such as merging a 1D vector into a 2D matrix).
Dimension Matching Requirement
NumPy requires all arrays passed to concatenate() to have the same number of dimensions. If they differ, you must adjust the dimensions of the smaller array using functions like np.expand_dims(), np.reshape(), or slice notation (np.newaxis).
Example 1: Concatenate 1D Array with 2D Array
Here, the 1D array is expanded along axis 0 to become a 2D row vector (1, 3) so that it can be joined along rows with the (2, 3) array:
Output:
Example 2: Concatenate 2D Array with 3D Array
If you have arrays with higher dimensions, you can similarly expand them. For example, expanding a 2D array of shape (2, 2) to (2, 2, 1) allows combining it with a 3D array of shape (2, 2, 2) along axis=2:
Output:
5. Concatenating Arrays Along Specific Axes (Higher Dimensions)
You can concatenate arrays along axes other than 0 and 1 using the axis parameter of concatenate(). For 3D or higher arrays, specifying axis=2 combines arrays along their depth dimension.
Example: Concatenating Two 3D Arrays Along axis=2
Output:
Both input arrays have shape (2, 2, 2). Concatenating along axis=2 joins the depth elements, resulting in shape (2, 2, 4).
6. Concatenating Arrays Using the stack() Function
The NumPy stack() function can also be used to combine arrays, but there is a fundamental conceptual difference between concatenate() and stack():
Joins arrays along an EXISTING axis.
- Does not change the number of dimensions.
- If input arrays are 2D, the result is still 2D.
- Example:
(2, 2) + (2, 2) → (4, 2)along axis 0.
Joins arrays along a NEW axis.
- Increases the number of dimensions by 1.
- If input arrays are 2D, the result becomes 3D!
- Example:
(2, 2) + (2, 2) → (2, 2, 2)along axis 2.
Example: Stacking Two 2D Arrays Along a New 3rd Axis
Output:
7. Summary: concatenate() vs stack()
| Feature | np.concatenate() |
np.stack() |
|---|---|---|
| Axis Behavior | Joins along an existing axis | Creates a brand-new axis |
| Dimension Count | Preserves dimension count (N-D → N-D) |
Increases dimension count by 1 (N-D → (N+1)-D) |
| Input Shape Rule | Must match on all axes except the concatenation axis | Must have the exact same shape across all axes |
| Typical Use | Appending rows or columns to tables | Batching images or collecting time-series frames |
Concatenation Quick Quiz
Test your knowledge of array concatenation in NumPy. Click an option to see the explanation.