Splitting Arrays in NumPy
Examples with split(), hsplit(), vsplit(), and array_split()
Next Topic ⮕Adding and Removing Dimensions in NumPy Arrays
Introduction: Why Split Arrays in NumPy?
Splitting arrays in NumPy isn't just about breaking them into chunks — it's about control, flexibility, and clarity in data processing. Whether you're slicing large datasets into mini-batches for training or dividing results across processes, the right split function makes all the difference.
Available Functions for Splitting
NumPy offers multiple ways to split arrays depending on your needs:
np.split()
– Split evenly across a specified axisnp.array_split()
– Split unequally if needednp.hsplit()
– Horizontal split (axis=1)np.vsplit()
– Vertical split (axis=0)
1. Splitting with np.split()
This method requires that the array is divisible by the number of sections. If not, it throws an error.
import numpy as np
arr = np.arange(12)
print("Original:", arr)
split_arrays = np.split(arr, 3)
print("Split Result:", split_arrays)
Original: [ 0 1 2 3 4 5 6 7 8 9 10 11]
Split Result: [array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8, 9, 10, 11])]
Explanation:
The array is split into 3 equal parts of 4 elements each. The function respects the original ordering and dimensions.
Watch out:
- If the array cannot be split evenly (e.g., 13 elements into 3), it will raise a
ValueError
.
2. Splitting Unequally with np.array_split()
Need to split without worrying about perfect divisibility? array_split()
comes to the rescue.
arr = np.arange(13)
split_arrays = np.array_split(arr, 3)
for i, subarray in enumerate(split_arrays):
print(f"Chunk {i}:", subarray)
Chunk 0: [0 1 2 3 4]
Chunk 1: [5 6 7 8]
Chunk 2: [ 9 10 11 12]
Explanation:
Even though 13 isn’t divisible by 3, the function smartly distributes extra elements to the earlier chunks.
3. Horizontal Splitting with np.hsplit()
Use this for splitting a 2D array along the columns (axis=1).
matrix = np.arange(12).reshape(3, 4)
print("Original Matrix:\n", matrix)
hsplit_result = np.hsplit(matrix, 2)
print("HSplit Result:")
for part in hsplit_result:
print(part)
Original Matrix:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
HSplit Result:
[[0 1]
[4 5]
[8 9]]
[[ 2 3]
[ 6 7]
[10 11]]
Check:
- Ensure number of columns is divisible by the number of splits.
- Use
array_split
if unsure of exact divisibility.
4. Vertical Splitting with np.vsplit()
This splits a 2D array row-wise (axis=0).
vsplit_result = np.vsplit(matrix, 3)
for part in vsplit_result:
print(part)
[[0 1 2 3]]
[[4 5 6 7]]
[[ 8 9 10 11]]
Best Practices When Splitting Arrays
- Verify shape before splitting: Use
array.shape
to check compatibility. - Catch errors early: Wrap
np.split()
in try-except when unsure of divisibility. - Use array_split for robustness: It handles odd cases without breaking your code.
Recap
Array splitting in NumPy is both powerful and precise—once you understand the axis system and how each function behaves. Stick with split()
for strict control, and array_split()
when flexibility is key. For 2D arrays, leverage hsplit()
and vsplit()
for intuitive slicing.
What’s Next?
Coming up: “Adding/Removing Dimensions in NumPy” – where you’ll learn to reshape arrays like a pro using expand_dims
, newaxis
, and more.