Concatenating arrays is a fundamental operation in NumPy, enabling you to combine multiple arrays into a single array.
This is useful in various applications, including data manipulation, machine learning, and more.
NumPy provides several functions to concatenate arrays along different axes, allowing flexibility based on array dimensions and your specific requirements.
In this tutorial, we will cover:
- Concatenating 1D Arrays
- Concatenating 2D Arrays (Horizontally and Vertically)
- Concatenating Using stack for Higher Dimensions
- Concatenating Uneven Arrays with concatenate and array_split
- Other Useful Concatenation Functions: vstack, hstack, and dstack
Let's explore each type of concatenation with examples.
1. Concatenating 1D Arrays
The simplest way to concatenate two or more arrays in NumPy is with the np.concatenate() function.
By default, this function concatenates along the first axis (axis=0).
Example 1: Basic Concatenation of 1D Arrays
import numpy as np # Create two 1D arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Concatenate the arrays concatenated_array = np.concatenate((array1, array2)) print("Concatenated 1D Array:\n", concatenated_array)
Output:
Concatenated 1D Array: [1 2 3 4 5 6]
- Explanation: concatenate() takes a tuple of arrays and combines them into a single array.
Example 2: Concatenating Multiple 1D Arrays
# Create three 1D arrays array3 = np.array([7, 8, 9]) # Concatenate three arrays concatenated_array_multiple = np.concatenate((array1, array2, array3)) print("Concatenated Multiple 1D Arrays:\n", concatenated_array_multiple)
Output:
Concatenated Multiple 1D Arrays: [1 2 3 4 5 6 7 8 9]
2. Concatenating 2D Arrays (Horizontally and Vertically)
For 2D arrays, you can concatenate along rows (axis=0) or columns (axis=1).
Example 3: Vertical Concatenation (Row-wise) of 2D Arrays
# Create two 2D arrays array2d_1 = np.array([[1, 2], [3, 4]]) array2d_2 = np.array([[5, 6], [7, 8]]) # Concatenate along rows (axis=0) vertical_concat = np.concatenate((array2d_1, array2d_2), axis=0) print("Vertical Concatenation of 2D Arrays:\n", vertical_concat)
Output:
Vertical Concatenation of 2D Arrays: [[1 2] [3 4] [5 6] [7 8]]
- Explanation: axis=0 stacks the arrays vertically, so rows are added below each other.
Example 4: Horizontal Concatenation (Column-wise) of 2D Arrays
# Concatenate along columns (axis=1) horizontal_concat = np.concatenate((array2d_1, array2d_2), axis=1) print("Horizontal Concatenation of 2D Arrays:\n", horizontal_concat)
Output:
Horizontal Concatenation of 2D Arrays: [[1 2 5 6] [3 4 7 8]]
- Explanation: axis=1 stacks the arrays horizontally, so columns are added beside each other.
3. Concatenating Using stack for Higher Dimensions
The np.stack() function concatenates arrays along a new axis, which increases the dimensionality of the result.
Example 5: Stacking 1D Arrays Along a New Axis
# Stack 1D arrays along a new axis stacked_1d = np.stack((array1, array2), axis=0) print("Stacked 1D Arrays along a new axis (axis=0):\n", stacked_1d)
Output:
Stacked 1D Arrays along a new axis (axis=0): [[1 2 3] [4 5 6]]
- Explanation: Stacking along axis=0 creates a 2D array where array1 and array2 become rows.
Example 6: Stacking 2D Arrays Along a New Axis
# Stack 2D arrays along a new axis stacked_2d = np.stack((array2d_1, array2d_2), axis=1) print("Stacked 2D Arrays along a new axis (axis=1):\n", stacked_2d)
Output:
Stacked 2D Arrays along a new axis (axis=1): [[[1 2] [5 6]] [[3 4] [7 8]]]
- Explanation: stack creates a 3D array by adding a new axis at axis=1.
4. Concatenating Uneven Arrays with concatenate and array_split
If arrays have different shapes, you may need to split or adjust them to concatenate them effectively.
Example 7: Concatenating Uneven Arrays with array_split
# Create a 1D array with an odd length uneven_array = np.array([1, 2, 3, 4, 5, 6, 7]) # Split into three parts to match length for concatenation split_arrays = np.array_split(uneven_array, 3) print("Split Arrays:\n", split_arrays) # Concatenate the split parts concatenated_split_arrays = np.concatenate(split_arrays) print("\nConcatenated Split Arrays:\n", concatenated_split_arrays)
Output:
Split Arrays: [array([1, 2, 3]), array([4, 5]), array([6, 7])] Concatenated Split Arrays: [1 2 3 4 5 6 7]
- Explanation: Using array_split allows us to work with uneven arrays by splitting them into manageable chunks before concatenation.
5. Other Useful Concatenation Functions: vstack, hstack, and dstack
NumPy provides specialized concatenation functions for vertical, horizontal, and depth-wise stacking.
Example 8: vstack for Vertical Stacking of 1D or 2D Arrays
vstack stacks arrays vertically (equivalent to axis=0 for 2D arrays).
# Vertical stacking of 1D arrays vstacked_array = np.vstack((array1, array2)) print("Vertically Stacked 1D Arrays with vstack:\n", vstacked_array)
Output:
Vertically Stacked 1D Arrays with vstack: [[1 2 3] [4 5 6]]
- Explanation: vstack creates a 2D array by stacking array1 and array2 as rows.
Example 9: hstack for Horizontal Stacking of 1D or 2D Arrays
hstack stacks arrays horizontally (equivalent to axis=1 for 2D arrays).
# Horizontal stacking of 1D arrays hstacked_array = np.hstack((array1, array2)) print("\nHorizontally Stacked 1D Arrays with hstack:\n", hstacked_array)
Output:
Horizontally Stacked 1D Arrays with hstack: [1 2 3 4 5 6]
Example 10: dstack for Depth Stacking of 2D Arrays
dstack stacks 2D arrays along a third dimension (depth-wise).
# Depth stacking of 2D arrays dstacked_array = np.dstack((array2d_1, array2d_2)) print("\nDepth Stacked 2D Arrays with dstack:\n", dstacked_array)
Output:
Depth Stacked 2D Arrays with dstack: [[[1 5] [2 6] [3 7] [4 8]]]
- Explanation: dstack creates a 3D array by stacking the 2D arrays along the depth dimension.
Summary of Array Concatenation in NumPy
Function | Description |
---|---|
concatenate | General function to concatenate arrays along a specified axis. |
stack | Adds a new axis, creating higher-dimensional arrays. |
vstack | Stacks arrays vertically along rows (axis=0 for 2D arrays). |
hstack | Stacks arrays horizontally along columns (axis=1 for 2D arrays). |
dstack | Stacks 2D arrays along a third dimension (depth-wise). |
array_split | Allows uneven splitting for easier concatenation of arrays with different shapes. |
Conclusion
In this tutorial, we explored various ways to concatenate arrays in NumPy, including:
- Basic concatenation of 1D and 2D arrays.
- Vertical and horizontal concatenation of 2D arrays.
- Using stack to add a new dimension for higher-dimensional concatenation.
- Special functions like vstack, hstack, and dstack for targeted stacking operations.