numpy.array_split(ary, indices_or_sections, axis=0) splits an array into multiple sub-arrays along a specified axis. Unlike strict splitting functions, array_split() allows sections that do not evenly divide the array length.When the number of sections does not evenly divide the axis length, array_split() automatically adjusts sub-array sizes so that earlier sections contain one more element than later sections. For strict equal divisions, see split(). For stacking operations, see append().
| Parameter | Description |
|---|---|
ary | Input array to be split into sub-arrays |
indices_or_sections | If an integer, indicates the number of equal (or nearly equal) sections. If 1D array of integers, indicates split indices |
axis | Axis along which to split (default is 0) |
numpy.array_split(ary, indices_or_sections, axis=0)
When the number of sections evenly divides the array length, array_split() produces sub-arrays of equal size:
import numpy as np
my_ar = np.arange(9)
splits = np.array_split(my_ar, 3)
print(splits)
assert len(splits) == 3
assert np.array_equal(splits[0], np.array([0, 1, 2]))
Expected output
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
When N does not divide evenly into the requested number of sections, array_split() distributes the remainder across the initial sections:
my_ar = np.arange(9)
print(np.array_split(my_ar, 5))
print(np.array_split(my_ar, 4))
Expected output
[array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7]), array([8])]
[array([0, 1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]
By default, splitting operates along axis=0 (splitting rows):
my_data = np.array([[6, 3, 2], [7, 2, 2], [6, 2, 9]])
print(np.array_split(my_data, 3))
Expected output
[array([[6, 3, 2]]), array([[7, 2, 2]]), array([[6, 2, 9]])]
Because array_split() returns a list of sub-arrays, individual items can be accessed using standard indexing:
my_data = np.array([[6, 3, 2], [7, 2, 2], [6, 2, 9]])
my_ar = np.array_split(my_data, 3)
print(my_ar[1]) # [[7 2 2]]
print(my_ar[1][0]) # [7 2 2]
print(my_ar[1][0][2]) # 2
assert my_ar[1][0][2] == 2
Expected output
[[7 2 2]]
[7 2 2]
2
Splitting floating-point arrays preserves precision across chunks:
my_ar = np.arange(9.)
print(np.array_split(my_ar, 4))
Expected output
[array([0., 1., 2.]), array([3., 4.]), array([5., 6.]), array([7., 8.])]
Test array_split() on 1D and 2D arrays, explore unequal section distribution, and run verification test suites in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubContinue exploring related array manipulation functions: array(), zeros(), append(), insert(), and reshape().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.