What is Array Splitting?
Splitting arrays in NumPy is a way to divide a single array into multiple sub-arrays. This can be done along any axis, depending on how you want to partition your data.
NumPy provides 5 essential functions for splitting:
1. Splitting Arrays Using numpy.split()
The split() function divides an array into multiple sub-arrays along a specified axis based on a number of equal sections or a list of split indices.
Syntax
| Parameter | Description |
|---|---|
array |
The input array to be split. |
indices_or_sections |
Can be either:
|
axis (optional) |
The axis along which to split. Default is 0 (rows in 2D). |
Example 1: Splitting into Equal-sized Sub-arrays (axis=1)
In this example, we split a 3x3 matrix into 3 equal sub-arrays along the columns (axis=1):
Output:
Example 2: Splitting at Specific Index Positions
When passing a list of split indices like [1, 2], NumPy splits the array into [:1], [1:2], and [2:] along rows (axis=0):
Output:
2. Splitting Arrays Using numpy.array_split()
What happens if an array with 10 elements needs to be split into 3 parts? Since 10 is not evenly divisible by 3, np.split() will raise a ValueError.
Key Difference: Unequal Splits Allowed
Unlikesplit(), numpy.array_split() allows for unequal splits. Extra elements are distributed to the earlier sub-arrays so that the sizes remain as balanced as possible!
Syntax
Example: Splitting 10 Elements into 3 Unequal Sub-arrays
Output:
How NumPy Balances Unequal Splits
For N = 10 elements into sections = 3: 10 = 4 + 3 + 3. The first sub-array receives 4 items, and the remaining sub-arrays receive 3 items each.
3. Horizontal Splitting: numpy.hsplit()
The hsplit() function splits an array along the horizontal axis (which corresponds to axis=1 for 2D arrays). It effectively partitions an array by separating its columns.
Syntax
Example: Splitting Columns into 2 Equal Parts
Output:
4. Vertical Splitting: numpy.vsplit()
The vsplit() function splits an array along the vertical axis (which corresponds to axis=0 for 2D arrays). It effectively partitions an array by separating its rows.
Syntax
Example: Splitting Rows into 3 Equal Parts
Output:
5. Depth Splitting: numpy.dsplit()
The dsplit() function is used to split a 3-Dimensional array along its third dimension (commonly referred to as the depth dimension, corresponding to axis=2).
Syntax
Example: Splitting a 3D Array Along Depth into 4 Parts
Output:
6. Comparison Table: All Splitting Functions
| Function | Default / Applicable Axis | Unequal Splits Allowed? | Primary Purpose |
|---|---|---|---|
np.split() |
User-specified axis (default 0) |
No (Strict Equal) | General equal splitting along any axis. |
np.array_split() |
User-specified axis (default 0) |
Yes (Safe) | General splitting when array size may not divide evenly. |
np.hsplit() |
axis=1 (Columns in 2D) |
No (Strict Equal) | Splits horizontally across columns. |
np.vsplit() |
axis=0 (Rows in 2D) |
No (Strict Equal) | Splits vertically across rows. |
np.dsplit() |
axis=2 (Depth in 3D) |
No (Strict Equal) | Splits depth-wise across channels / layers. |
Array Splitting Quiz
Test your understanding of array splitting functions in NumPy. Click an option to see instant feedback.