split()
split() cuts one array into several smaller ones.
In this page:
Syntax
np.split(arr, sections)
np.split(arr, [index1, index2])
split()
np.split divides an array into equal parts, or at specific index positions if you pass a list. It returns a list of arrays. np.array_split allows unequal parts when the size does not divide evenly.
Note:
np.split raises an error for uneven divisions; use array_split instead.
Example: split()
import numpy as np
a = np.arange(9)
print(np.split(a, 3))
print(np.split(a, [2, 5]))
print(np.array_split(np.arange(7), 3))
# Output:
# [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
# [array([0, 1]), array([2, 3, 4]), array([5, 6, 7, 8])]
# [array([0, 1, 2]), array([3, 4]), array([5, 6])]
Related Topics
Common Mistakes
- Splitting into a count that does not divide evenly
- Forgetting the result is a list
- Confusing split points with sizes
Chapter Summary
- split returns a list of arrays
- An int means equal parts
- A list means split indices
- array_split allows uneven sizes
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: