Join NumPy Arrays
Joining Arrays in NumPy
In NumPy, joining arrays refers to concatenating or stacking multiple arrays along different axes. Here are some common methods to join arrays:
Concatenation
The np.concatenate()
function joins arrays along an existing axis (axis=0 by default, which is row-wise).
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Join arrays along axis=0 (default, horizontal concatenation)
result = np.concatenate((arr1, arr2))
print(result) # Output: [1 2 3 4 5 6]
For 2D arrays (row-wise):
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
result = np.concatenate((arr1, arr2), axis=0)
print(result)
Output:
[[1 2]
[3 4]
[5 6]]
For 2D arrays (column-wise):
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
result = np.concatenate((arr1, arr2), axis=1)
print(result)
Output:
[[1 2 5 6]
[3 4 7 8]]
Stacking Arrays
np.stack()
joins arrays along a new axis (creating a new dimension).
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Stack arrays along a new axis (axis=0)
result = np.stack((arr1, arr2), axis=0)
print(result)
Output:
[[1 2 3]
[4 5 6]]
For stacking along a new axis (axis=1):
result = np.stack((arr1, arr2), axis=1)
print(result)
Output:
[[1 4]
[2 5]
[3 6]]
Horizontal Stack
np.hstack()
horizontally stacks arrays (along axis=1).
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Horizontal stacking
result = np.hstack((arr1, arr2))
print(result)
# Output: [1 2 3 4 5 6]
For 2D arrays:
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
result = np.hstack((arr1, arr2))
print(result)
Output:
[[1 2 5 6]
[3 4 7 8]]
Vertical Stack
np.vstack()
vertically stacks arrays (along axis=0).
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Vertical stacking
result = np.vstack((arr1, arr2))
print(result)
Output:
[[1 2 3]
[4 5 6]]
For 2D arrays:
arr1 = np.array([[1, 2]])
arr2 = np.array([[3, 4]])
result = np.vstack((arr1, arr2))
print(result)
Output:
[[1 2]
[3 4]]
Depth Stack
np.dstack()
stacks arrays along the third axis, adding depth.
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Depth stacking
result = np.dstack((arr1, arr2))
print(result)
Output:
[[[1 4]]
[[2 5]]
[[3 6]]]
Combining Arrays with Different Dimensions
np.column_stack()
and np.row_stack()
stack 1D arrays into 2D arrays as columns or rows.
- Column Stack:
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.column_stack((arr1, arr2))
print(result)
Output:
[[1 4]
[2 5]
[3 6]]
- Row Stack:
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.row_stack((arr1, arr2))
print(result)
Output:
[[1 2 3]
[4 5 6]]
NumPy Join Methods
Method | Purpose |
---|---|
np.concatenate() | Join arrays along an existing axis |
np.stack() | Join arrays along a new axis |
np.hstack() | Stack arrays horizontally |
np.vstack() | Stack arrays vertically |
np.dstack() | Stack arrays along the third axis (depth) |
np.column_stack() | Stack 1D arrays as columns |
np.row_stack() | Stack 1D arrays as rows |