The reverse of array joining is array splitting—breaking a large array into multiple smaller sub-arrays. This is standard in machine learning pipelines when splitting feature matrices $X$ from label vectors $y$, or dividing a dataset into equal batches.
NumPy provides three intuitive splitting routines:
np.split(): General splitting along any specified axis.
np.vsplit(): Vertical splitting (row-wise / along axis 0).
np.hsplit(): Horizontal splitting (column-wise / along axis 1).
1. General Splitting with np.split()
np.split(ary, indices_or_sections, axis=0) accepts two modes for dividing an array:
1
An integer $N$: Splits the array into $N$ equal-sized sub-arrays. (The dimension size along the split axis must be evenly divisible by $N$).
2
A list of integer indices: Splits the array at specific partition boundaries.
Mode 1: Equal Sections
1import numpy as np
2
3arr = np.arange(12)# [0, 1, 2, ..., 11]
4
5# Split into 3 equal parts (12 / 3 = 4 elements each)
6parts = np.split(arr,3)
7for i, p inenumerate(parts):
8print(f"Part {i}:", p)
Output:
1Part 0: [0 1 2 3]
2Part 1: [4 5 6 7]
3Part 2: [8 9 10 11]
If the array size is not divisible by $N$, np.split() raises ValueError: array split does not result in an equal division. To allow unequal splitting, use np.array_split().
Mode 2: Explicit Index Partitions
1# Split at index 2 and index 7
2# Results in: arr[:2], arr[2:7], arr[7:]
3sections = np.split(arr,[2,7])
4print("Section 0 (0 to 2):", sections[0])
5print("Section 1 (2 to 7):", sections[1])
6print("Section 2 (7 to end):", sections[2])
2. Vertical Splitting: np.vsplit()
np.vsplit() splits a 2D array vertically (dividing rows). It is equivalent to np.split(..., axis=0):
1dataset = np.arange(24).reshape(6,4)
2print("Original 6x4 Dataset:
3", dataset)
4
5# Split 6 rows into 2 equal halves (3 rows each)
6top_half, bottom_half = np.vsplit(dataset,2)
7print("
8Top half (3 rows):
9", top_half)
10print("
11Bottom half (3 rows):
12", bottom_half)
3. Horizontal Splitting: np.hsplit()
np.hsplit() splits an array horizontally (dividing columns). It is equivalent to np.split(..., axis=1).
Real-World Machine Learning Example: Splitting Features $X$ and Target $y$
In supervised learning datasets, the target label $y$ is often the last column, while features $X$ occupy all preceding columns:
1# 5 samples with 3 features + 1 target label (5x4 matrix)
2data = np.array([
3[1.5,2.3,0.9,1],
4[3.1,1.2,4.4,0],
5[2.8,5.1,3.2,1],
6[0.9,1.8,2.1,0],
7[4.2,3.9,5.0,1]
8])
9
10# Split horizontally after index 3: columns 0-2 (features), column 3 (target)
11X, y = np.hsplit(data,[3])
12print("Features X (Shape:", X.shape, "):
13", X)
14print("
15Target y (Shape:", y.shape, "):
16", y)
4. Unequal Splitting with np.array_split()
When you need to divide a dataset of say 10 samples across 3 workers, $10$ is not divisible by $3$. np.array_split() cleanly distributes the remainder across the first sub-arrays:
1ten_items = np.arange(10)
2
3# Splits 10 items into 3 chunks: lengths 4, 3, and 3
4chunks = np.array_split(ten_items,3)
5for i, c inenumerate(chunks):
6print(f"Worker {i} chunk (size {len(c)}):", c)
Output:
1Worker 0 chunk (size 4): [0 1 2 3]
2Worker 1 chunk (size 3): [4 5 6]
3Worker 2 chunk (size 3): [7 8 9]
Interactive Quiz• Knowledge Check
Multiple Choice Questions
Question 1
What happens if you call np.split(np.arange(10), 3)?
Question 2
What slices are created by np.split(arr, [3, 8])?
Question 3
Which function is equivalent to np.split(matrix, 2, axis=0) for a 2D matrix?
Question 4
Given a matrix of shape (8, 6), what is the shape of each sub-array after np.hsplit(matrix, 3)?
Question 5
How does np.array_split(arr, N) differ from np.split(arr, N)?
Next Lesson
Vectorization: Eliminating Python Loops with SIMD
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.