How to Use np.hsplit and np.vsplit in NumPy for Array Splitting
Use
np.hsplit to split a NumPy array horizontally (by columns) into multiple sub-arrays, and np.vsplit to split it vertically (by rows). Both functions take the array and the number of splits or indices where to split as arguments.Syntax
np.hsplit(array, indices_or_sections) splits the array horizontally (column-wise).np.vsplit(array, indices_or_sections) splits the array vertically (row-wise).
Parameters:
array: The NumPy array to split.indices_or_sections: Either an integer number of equal splits or a list of column/row indices where to split.
python
np.hsplit(array, indices_or_sections) np.vsplit(array, indices_or_sections)
Example
This example shows how to split a 2D array into parts horizontally and vertically using np.hsplit and np.vsplit.
python
import numpy as np arr = np.arange(16).reshape(4, 4) # Horizontal split into 2 equal parts (split columns) h_splits = np.hsplit(arr, 2) # Vertical split into 2 equal parts (split rows) v_splits = np.vsplit(arr, 2) print("Original array:\n", arr) print("\nHorizontal splits:") for i, part in enumerate(h_splits): print(f"Part {i}: ", part) print("\nVertical splits:") for i, part in enumerate(v_splits): print(f"Part {i}: ", part)
Output
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Horizontal splits:
Part 0:
[[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
Part 1:
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
Vertical splits:
Part 0:
[[0 1 2 3]
[4 5 6 7]]
Part 1:
[[ 8 9 10 11]
[12 13 14 15]]
Common Pitfalls
1. Unequal splits: If you provide an integer for indices_or_sections that does not evenly divide the array's dimension, NumPy will raise an error.
2. Confusing horizontal and vertical splits: Remember hsplit splits columns, vsplit splits rows.
3. Using 1D arrays: These functions expect at least 2D arrays; using 1D arrays may cause unexpected results or errors.
python
import numpy as np arr = np.arange(10) # Wrong: trying to hsplit 1D array try: np.hsplit(arr, 2) except Exception as e: print(f"Error: {e}") # Correct: reshape to 2D before splitting arr_2d = arr.reshape(2, 5) h_splits = np.hsplit(arr_2d, 2) print("\nCorrect horizontal split:", h_splits)
Output
Error: array split does not result in an equal division
Correct horizontal split: [array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]]), array([], shape=(2, 0), dtype=int64)]
Quick Reference
np.hsplit: splits array by columns (horizontal).np.vsplit: splits array by rows (vertical).- Use integer for equal splits or list of indices for custom splits.
- Input array must be at least 2D.
Key Takeaways
Use np.hsplit to split arrays horizontally by columns and np.vsplit to split vertically by rows.
Both functions accept an integer for equal splits or a list of indices for custom splits.
Input arrays must be at least 2D; 1D arrays will cause errors or unexpected behavior.
Ensure the number of splits divides the array dimension evenly to avoid errors.
Remember hsplit splits columns, vsplit splits rows to avoid confusion.