![]() |
VOOZH | about |
Splitting arrays means dividing a single NumPy array into multiple smaller sub-arrays. NumPy provides several functions that make this easy by allowing you to split arrays along different directions (rows, columns, depth).
Below are some important terms to understand when splitting arrays:
Example: This example splits a 1D array into three smaller parts using np.array_split().
[array([1, 2]), array([3, 4]), array([5, 6])]
Explanation: np.array_split(arr, 3) divides the array into 3 sub-arrays, splitting elements as evenly as possible.
NumPy provides several built-in functions to split arrays into smaller parts. These methods help divide 1D, 2D, and even 3D arrays along different axes. Let's go through each method one by one with simple examples, outputs, and clear explanations.
numpy.split() is used to divide an array into equal-sized subarrays. The number of splits must perfectly divide the size of the array along the chosen axis. If equal division is not possible, NumPy will raise an error.
[array([0, 1, 2]), array([3, 4, 5])]
Explanation: np.arange(6) creates [0 1 2 3 4 5], np.split(array, 2) splits the array into 2 equal parts and result in two subarrays, each containing 3 elements
numpy.array_split() works like split(), but it allows uneven splitting. This is useful when the array size does not divide evenly by the number of splits. NumPy will distribute the extra elements automatically.
[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8, 9]), array([10, 11, 12])]
Explanation: np.arange(13) creates array of 13 elements, np.array_split(array, 4) splits into 4 unequal parts and extra elements are distributed among the first subarrays
numpy.vsplit() performs vertical splitting, meaning it divides a matrix row-wise (along axis=0). It works only on arrays with 2 or more dimensions.
[array([[1, 2, 3], [4, 5, 6]]), array([[ 7, 8, 9], [10, 11, 12]])]
Explanation: vsplit(matrix, 2) splits into 2 vertical (row-wise) partsand each part contains 2 rows
numpy.hsplit() performs horizontal splitting, which divides the array column-wise (axis=1). This is helpful when separating feature columns in datasets.
[array([[ 1, 2], [ 5, 6], [ 9, 10]]), array([[ 3, 4], [ 7, 8], [11, 12]])]
Explanation: hsplit(array, 2) splits into 2 equal column groups and each output contains 2 columns
numpy.dsplit() is used for 3D arrays. It splits the array along the third axis (axis=2). This is useful when working with stacked matrices, images, or multi-channel data.
Output
[array([[[ 0, 1],
[ 4, 5],
[ 8, 9]],
[[12, 13],
[16, 17],
[20, 21]]]), array([[[ 2, 3],
[ 6, 7],
[10, 11]],
[[14, 15],
[18, 19],
[22, 23]]])]
Explanation: Array shape is (2, 3, 4) and dsplit(..., 2) splits last axis into 2 equal parts. Each result contains half of the last dimension