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

numpy.concatenate((a1, a2, ...), axis=0, out=None)

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

import numpy as np arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([[7, 8, 9], [10, 11, 12]]) # Concatenate along rows (axis=0) concatenated_arr = np.concatenate((arr1, arr2), axis=0) print("Concatenated Array along rows:\n", concatenated_arr) print("Result Shape:", concatenated_arr.shape)

Output:

Concatenated Array along rows: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] Result Shape: (4, 3)
Visual Breakdown (axis=0)
arr1 (2x3): [[ 1, 2, 3], [ 4, 5, 6]] + (stacked vertically) arr2 (2x3): [[ 7, 8, 9], [10, 11, 12]] = Result (4x3): [[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]]

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

import numpy as np # Create two arrays arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) # Concatenate along columns (axis=1) concatenated_arr = np.concatenate((arr1, arr2), axis=1) print("Concatenated Array along columns:\n", concatenated_arr) print("Result Shape:", concatenated_arr.shape)

Output:

Concatenated Array along columns: [[1 2 5 6] [3 4 7 8]] Result Shape: (2, 4)
Visual Breakdown (axis=1)
arr1 (2x2): arr2 (2x2): [[1, 2], + [[5, 6], [3, 4]] [7, 8]] = Result (2x4, joined horizontally): [[1, 2, 5, 6], [3, 4, 7, 8]]

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:

import numpy as np # Create a 1D array of shape (3,) arr1 = np.array([1, 2, 3]) # Create a 2D array of shape (2, 3) arr2 = np.array([[4, 5, 6], [7, 8, 9]]) # Expand dimensions of 1D array to shape (1, 3) expanded_arr1 = np.expand_dims(arr1, axis=0) # Concatenate along rows (axis=0) concatenated_arr = np.concatenate((expanded_arr1, arr2), axis=0) print("Concatenated Array with Mixed Dimensions along rows:\n", concatenated_arr) print("Result Shape:", concatenated_arr.shape)

Output:

Concatenated Array with Mixed Dimensions along rows: [[1 2 3] [4 5 6] [7 8 9]] Result Shape: (3, 3)

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:

import numpy as np # Create a 2D array of shape (2, 2) arr1 = np.array([[1, 2], [3, 4]]) # Create a 3D array of shape (2, 2, 2) arr2 = np.array([[[5, 6], [7, 8]], [[9, 10], [11, 12]]]) # Expand dimensions of the 2D array to (2, 2, 1) for concatenation along axis=2 expanded_arr1 = np.expand_dims(arr1, axis=2) # Concatenate along the third dimension (axis=2) concatenated_arr = np.concatenate((expanded_arr1, arr2), axis=2) print("Concatenated Array with Mixed Dimensions along axis=2:\n", concatenated_arr) print("Result Shape:", concatenated_arr.shape)

Output:

Concatenated Array with Mixed Dimensions along axis=2: [[[ 1 5 6] [ 2 7 8]] [[ 3 9 10] [ 4 11 12]]] Result Shape: (2, 2, 3)

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

import numpy as np # 3D arrays of shape (2, 2, 2) arr1 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) arr2 = np.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]]) # Concatenate along the third dimension (axis=2) result = np.concatenate((arr1, arr2), axis=2) print("Concatenated along third dimension:\n", result) print("Result Shape:", result.shape)

Output:

Concatenated along third dimension: [[[ 1 2 9 10] [ 3 4 11 12]] [[ 5 6 13 14] [ 7 8 15 16]]] Result Shape: (2, 2, 4)

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():

numpy.concatenate()

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.
numpy.stack()

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

import numpy as np # Example 2D arrays of shape (2, 2) arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) # Stack along a new third axis (axis=2) result = np.stack((arr1, arr2), axis=2) print("Stacked along a new axis:\n", result) print("Result Shape:", result.shape)

Output:

Stacked along a new axis: [[[1 5] [2 6]] [[3 7] [4 8]]] Result Shape: (2, 2, 2)

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.

1. By default, along which axis does np.concatenate() join arrays?
A) axis=0 (along rows)
B) axis=1 (along columns)
C) axis=-1 (last axis)
D) A new axis
2. What is the output shape when concatenating two arrays of shapes (3, 4) and (5, 4) with axis=0?
A) (8, 8)
B) (8, 4)
C) (3, 8)
D) ValueError
3. Can you directly concatenate a 1D array of shape (3,) with a 2D array of shape (2, 3) using np.concatenate() without adjusting dimensions?
A) Yes, NumPy broadcasts automatically
B) No, it raises a ValueError because both arrays must have the same number of dimensions
C) Yes, but only with axis=1
D) Yes, it automatically returns a 1D array
4. What happens when you combine two 2D arrays of shape (3, 3) using np.stack() instead of np.concatenate()?
A) The result is a 2D array of shape (6, 3)
B) The result is a 1D array of shape (18,)
C) The result is a 3D array of shape (2, 3, 3) (or similar depending on axis)
D) It raises a ValueError
5. What function can you use to add an axis to a 1D array before concatenating it with a 2D array?
A) np.pad()
B) np.ravel()
C) np.expand_dims()
D) np.flatten()