Splitting Arrays into Chunks (split, hsplit, vsplit)
Splitting Arrays: split(), hsplit(), & vsplit()
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:
- 1An integer $N$: Splits the array into $N$ equal-sized sub-arrays. (The dimension size along the split axis must be evenly divisible by $N$).
- 2A list of integer indices: Splits the array at specific partition boundaries.
Mode 1: Equal Sections
Output:
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
2. Vertical Splitting: np.vsplit()
np.vsplit() splits a 2D array vertically (dividing rows). It is equivalent to np.split(..., axis=0):
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:
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:
Output:
Multiple Choice Questions
1. What happens if you call np.split(np.arange(10), 3)?
A. The last element is discarded and three arrays of 3 elements are returned B. A ValueError is raised because 10 is not evenly divisible by 3 C. Three arrays of lengths 4, 3, 3 are returned D. An array of floats is returned Answer: B Explanation: np.split strictly requires an equal division when an integer section count is provided. For unequal splits without an error, np.array_split() must be used.
2. What slices are created by np.split(arr, [3, 8])?
A. arr[:3], arr[3:8], and arr[8:] B. arr[3], arr[8] C. arr[:3] and arr[8:] D. arr[3:8] only Answer: A Explanation: Providing a 1D list of indices [i1, i2] partitions the array into three segments: [:i1], [i1:i2], and [i2:].
3. Which function is equivalent to np.split(matrix, 2, axis=0) for a 2D matrix?
A. np.hsplit(matrix, 2) B. np.vsplit(matrix, 2) C. np.dsplit(matrix, 2) D. np.squeeze(matrix) Answer: B Explanation: np.vsplit() divides arrays vertically along axis 0 (rows), making it identical to np.split(matrix, sections, axis=0).
4. Given a matrix of shape (8, 6), what is the shape of each sub-array after np.hsplit(matrix, 3)?
A. (4, 6) B. (8, 2) C. (8, 3) D. (2, 6) Answer: B Explanation: hsplit divides the 6 columns along axis 1 into 3 equal chunks: 6 / 3 = 2 columns each. The row dimension (8) remains unchanged, producing sub-arrays of shape (8, 2).
5. How does np.array_split(arr, N) differ from np.split(arr, N)?
A. np.array_split converts all elements to float B. np.array_split allows unequal division when arr.shape[axis] is not evenly divisible by N C. np.array_split returns a single concatenated array D. np.array_split only accepts 1D arrays Answer: B Explanation: np.array_split() accommodates divisions where the length is not an exact multiple of N by adjusting sub-array sizes to handle remainders gracefully without throwing an exception.
Vectorization: Eliminating Python Loops with SIMD
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Depth & Column Stacking (dstack, column_stack, stack) | Vectorization: Eliminating Python Loops with SIMD |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.