vsplit() requires the array to have at least 2 dimensions, and divides it along its first axis, rows, either into N equal groups of rows, an integer argument, or at specific row-index boundaries, a list argument. It's the row-wise counterpart to np.hsplit(), and its name communicates the specific, common 'split by rows' intent more directly than the more general np.split(arr, ..., axis=0) call it's equivalent to.
1Understanding np.vsplit()
vsplit() requires the array to have at least 2 dimensions, and divides it along its first axis, rows, either into N equal groups of rows, an integer argument, or at specific row-index boundaries, a list argument. It's the row-wise counterpart to np.hsplit(), and its name communicates the specific, common 'split by rows' intent more directly than the more general np.split(arr, ..., axis=0) call it's equivalent to.
vsplit() requires the array to already have at least 2 dimensions — calling it on a plain 1D array raises an error, since there's no concept of 'rows' to split by for a single-dimensional array.
import numpy as np
matrix = np.arange(16).reshape(4, 4)
top, bottom = np.vsplit(matrix, 2)
print(top)2Practical Example
Here is a real-world application of np.vsplit() showing how it is used in production NumPy code.
import numpy as np
matrix = np.arange(16).reshape(4, 4)
parts = np.vsplit(matrix, [1, 3])
print(len(parts))
print(parts[1])3Best Practices
Follow these guidelines when working with np.vsplit():
1. Use vsplit() for the specific, common case of splitting a 2D array by rows, instead of the more generic np.split(arr, ..., axis=0)
2. Verify the array is at least 2D before calling vsplit(), since it doesn't apply to plain 1D arrays
3. Use explicit index positions when the rows need to be split unevenly, rather than an integer count that requires an even division
Tip: vsplit() requires the array to already have at least 2 dimensions — calling it on a plain 1D array raises an error, since there's no concept of 'rows' to split by for a single-dimensional array.
import numpy as np
matrix = np.arange(16).reshape(4, 4)
top, bottom = np.vsplit(matrix, 2)
print(top)