Passing an integer N to split() divides the array into N equal parts along the given axis, raising a ValueError if the array's size along that axis doesn't divide evenly by N. Passing a list of index positions instead splits the array at exactly those boundaries, producing sub-arrays of potentially different sizes — for example, splitting at positions [3, 7] on a 10-element array produces three pieces: elements 0-2, 3-6, and 7-9.
1Understanding np.split()
Passing an integer N to split() divides the array into N equal parts along the given axis, raising a ValueError if the array's size along that axis doesn't divide evenly by N. Passing a list of index positions instead splits the array at exactly those boundaries, producing sub-arrays of potentially different sizes — for example, splitting at positions [3, 7] on a 10-element array produces three pieces: elements 0-2, 3-6, and 7-9.
If split() raises a ValueError about an array that cannot be split into equal parts, either use np.array_split() instead, which allows uneven splits, or pass explicit index positions instead of a plain integer count.
import numpy as np
arr = np.arange(9)
parts = np.split(arr, 3)
print(parts)2Practical Example
Here is a real-world application of np.split() showing how it is used in production NumPy code.
import numpy as np
arr = np.arange(10)
parts = np.split(arr, [3, 7])
print(parts)3Best Practices
Follow these guidelines when working with np.split():
1. Use np.array_split() instead of np.split() when the array's size might not divide evenly into the number of pieces you want
2. Pass explicit index positions to split() when you need specific, uneven boundaries rather than N equal pieces
3. Double check the axis parameter matches the dimension you actually intend to split along, especially for multi-dimensional arrays
Tip: If split() raises a ValueError about an array that cannot be split into equal parts, either use np.array_split() instead, which allows uneven splits, or pass explicit index positions instead of a plain integer count.
import numpy as np
arr = np.arange(9)
parts = np.split(arr, 3)
print(parts)