Splitting Arrays in NumPy
Dividing an array in numPy
In NumPy, splitting arrays means dividing an array into multiple sub-arrays. This can be useful for data preprocessing, parallel processing, and more.
Here are some of the most common methods for splitting arrays:
Split into Multiple Sub-arrays
The np.split()
function divides an array into multiple sub-arrays along a specified axis.
- 1D Array Split
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
# Split into 3 sub-arrays
result = np.split(arr, 3)
print(result) # Output: [array([1, 2]), array([3, 4]), array([5, 6])]
- 2D Array Split For 2D arrays, you can split along different axes.
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Split the 2D array into 3 sub-arrays along axis=0 (rows)
result = np.split(arr, 3, axis=0)
print(result)
# Output: [array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]
Split into Equal Sub-arrays
If the array cannot be evenly divided, np.array_split()
will handle the split and distribute the remaining elements as evenly as
possible.
- 1D Array Split
arr = np.array([1, 2, 3, 4, 5, 6])
# Split into 4 sub-arrays (uneven split)
result = np.array_split(arr, 4)
print(result) # Output: [array([1, 2]), array([3, 4]), array([5]), array([6])]
Horizontal Split
np.hsplit()
is used to split an array into multiple sub-arrays horizontally (along axis=1).
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Horizontal split into 3 sub-arrays
result = np.hsplit(arr, 3)
print(result)
# Output: [array([[1], [4]]), array([[2], [5]]), array([[3], [6]])]
Vertical Split
np.vsplit()
is used to split an array into multiple sub-arrays vertically (along axis=0).
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Vertical split into 2 sub-arrays
result = np.vsplit(arr, 2)
print(result)
# Output: [array([[1, 2, 3]]), array([[4, 5, 6]])]
Split Based on Indices
You can also specify exact indices where you want the split to occur.
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# Split at indices 3 and 6
result = np.split(arr, [3, 6])
print(result)
# Output: [array([1, 2, 3]), array([4, 5, 6]), array([7, 8])]
NumPy Split Methods
Method | Purpose |
---|---|
np.split() | Split an array into multiple sub-arrays at specified indices or sections |
np.array_split() | Split an array into multiple sub-arrays (handles uneven splits) |
np.hsplit() | Split arrays horizontally (along axis=1) |
np.vsplit() | Split arrays vertically (along axis=0) |
No questions available.