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. numpy.split()
2. numpy.array_split()
3. numpy.hsplit()
4. numpy.vsplit()
5. numpy.dsplit()

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

numpy.split(array, indices_or_sections, axis=0)
Parameter Description
array The input array to be split.
indices_or_sections Can be either:
  • An Integer (N): Specifies the number of equal-sized sub-arrays. The array must be divisible evenly by N, or NumPy will raise a ValueError.
  • A 1D array/list of integers: Specifies the exact indices at which to split the array.
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):

import numpy as np # Create a 3x3 array arr = np.arange(9).reshape(3, 3) # Split into 3 equal sub-arrays along columns (axis=1) split_arr = np.split(arr, 3, axis=1) print("Original Array:") print(arr) print("\nSplit into 3 equal sub-arrays along axis 1:") for sub_arr in split_arr: print(sub_arr)

Output:

Original Array: [[0 1 2] [3 4 5] [6 7 8]] Split into 3 equal sub-arrays along axis 1: [[0] [3] [6]] [[1] [4] [7]] [[2] [5] [8]]

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):

import numpy as np # 3x3 array arr = np.arange(9).reshape(3, 3) # Split array at specified row indices [1, 2] split_arr = np.split(arr, [1, 2], axis=0) print("Split at indices [1, 2] along axis 0:") for sub_arr in split_arr: print(sub_arr)

Output:

Split at indices [1, 2] along axis 0: [[0 1 2]] [[3 4 5]] [[6 7 8]]

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
Unlike split(), 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

numpy.array_split(array, indices_or_sections, axis=0)

Example: Splitting 10 Elements into 3 Unequal Sub-arrays

import numpy as np # 1D array of 10 elements arr = np.arange(10) # Split into 3 sub-arrays along axis 0 (uneven division: 4, 3, 3) split_arr = np.array_split(arr, 3) print("Original Array:") print(arr) print("\nSplit into 3 unequal sub-arrays:") for sub_arr in split_arr: print(sub_arr)

Output:

Original Array: [0 1 2 3 4 5 6 7 8 9] Split into 3 unequal sub-arrays: [0 1 2 3] [4 5 6] [7 8 9]
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

numpy.hsplit(array, indices_or_sections)

Example: Splitting Columns into 2 Equal Parts

import numpy as np # 2D array of shape (2, 4) arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) # Split into 2 equal parts along columns split_arr = np.hsplit(arr, 2) print("Original Array:\n", arr) print("\nSplit into 2 equal parts along axis 1:") for sub_arr in split_arr: print(sub_arr)

Output:

Original Array: [[1 2 3 4] [5 6 7 8]] Split into 2 equal parts along axis 1: [[1 2] [5 6]] [[3 4] [7 8]]

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

numpy.vsplit(array, indices_or_sections)

Example: Splitting Rows into 3 Equal Parts

import numpy as np # 2D array of shape (3, 3) arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Split into 3 equal parts along rows split_arr = np.vsplit(arr, 3) print("Original Array:\n", arr) print("\nSplit into 3 equal parts along axis 0:") for sub_arr in split_arr: print(sub_arr)

Output:

Original Array: [[1 2 3] [4 5 6] [7 8 9]] Split into 3 equal parts along axis 0: [[1 2 3]] [[4 5 6]] [[7 8 9]]

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

numpy.dsplit(array, indices_or_sections)

Example: Splitting a 3D Array Along Depth into 4 Parts

import numpy as np # Example 3D array of shape (2, 3, 4) arr = np.arange(24).reshape((2, 3, 4)) # Split into 4 equal parts along axis 2 (depth) split_arr = np.dsplit(arr, 4) print("Original Array:") print(arr) print("\nSplit into 4 equal parts along axis 2 (depth):") for sub_arr in split_arr: print(sub_arr) print()

Output:

Original Array: [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] Split into 4 equal parts along axis 2 (depth): [[[ 0] [ 4] [ 8]] [[12] [16] [20]]] [[[ 1] [ 5] [ 9]] [[13] [17] [21]]] [[[ 2] [ 6] [10]] [[14] [18] [22]]] [[[ 3] [ 7] [11]] [[15] [19] [23]]]

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.

1. What occurs if you run np.split(np.arange(10), 3)?
A) It splits the array into sizes 4, 3, 3
B) It raises a ValueError because 10 is not evenly divisible by 3
C) It pads the missing elements with 0
D) It truncates the 10th element
2. Which function can safely split an array of 11 elements into 3 sub-arrays without raising an error?
A) np.array_split()
B) np.split()
C) np.vsplit()
D) np.hsplit()
3. Splitting a 2D matrix of shape (4, 6) with np.hsplit(arr, 3) will produce:
A) 3 arrays of shape (4, 3)
B) 3 arrays of shape (4, 2)
C) 3 arrays of shape (1, 6)
D) 2 arrays of shape (4, 3)
4. Along which axis does np.dsplit() perform splitting?
A) axis=0 (rows)
B) axis=1 (columns)
C) axis=2 (depth)
D) A new axis
5. For np.split(arr, [2, 5], axis=0), how many sub-arrays are returned?
A) 2 sub-arrays
B) 3 sub-arrays (arr[:2], arr[2:5], arr[5:])
C) 5 sub-arrays
D) 1 sub-array