English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NumPy Array Splitting

Splitting is the reverse operation of joining.
Joining is to combine multiple arrays into one, splitting is to split an array into multiple.

Basic array splitting function is as follows:

FunctionArray and operations
splitSplit an array into multiple subarrays
hsplitSplit an array horizontally into multiple subarrays (by column)
vsplitSplit an array vertically into multiple subarrays (by row)

numpy.split

The numpy.split function splits the array along a specific axis into subarrays, the format is as follows:

numpy.split(ary, indices_or_sections, axis)

Parameter description:

ary: the array to be splitindices_or_sections: if it is an integer, it is split evenly using that number, if it is an array, it is the positions along the axis to split (left open, right closed) axis: along which dimension to cut, default is 0, horizontal cutting. For1When, longitudinal cutting

import numpy as np
a = np.arange(15)
print('First Array:')
print(a)
print('\n')
print('Split the array into three subarrays of equal size:')
b = np.split(a,5)
print(b)
print('\n')
print('Split the array at the indicated position in a one-dimensional array:')
b = np.split(a,4,7])
print(b)

Output Result:

First Array:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]]
Split the array into three subarrays of equal size:
[array([0, 1, 2)) array([3, 4, 5)) array([6, 7, 8)) array([ 9, 10, 11)) array([12, 13, 14]]
Split the array at the indicated position in a one-dimensional array:
[array([0, 1, 2, 3)) array([4, 5, 6)) array([ 7, 8, 9, 10, 11, 12, 13, 14]]

When the number of elements in the array is less than the required quantity, it is necessary to usearray_split functionIt will make corresponding adjustments from the end.

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 4)
print(newarr)

Output Result:

[array([1, 2)) array([3, 4)) array([5)) array([6]]
Note:Using the split() method, when the elements in the source array are few and not suitable for splitting, it will not adjust the elements, as shown in the example above, array_split() works normally, but split() will fail.

numpy.hsplit

The numpy.hsplit function is used to split an array horizontally, splitting the original array into the number of arrays specified with the same shape.

import numpy as np
harr = np.floor(10 * np.random.random(2, 8))
print('Original array:')
print(harr)
 
print('Split After:')
print(np.hsplit(harr, 4))

Output Result:

Original array:
[7. 9. 2. 6. 8. 7. 4. 5.]
 [2. 5. 3. 5. 9. 4. 1. 3.]]
Split After:
[array([7. 9.],
       [2. 5.]]2. 6.],
       [3. 5.]]8. 7.],
       [9. 4.]]4. 5.],
       [1. 3.

numpy.vsplit

numpy.vsplit splits along the vertical axis, similar to the usage of hsplit.

import numpy as np
a = np.arange(16).reshape(4,4)
print('First Array:')
print(a)
print('\n')
print('Vertical Split:')
b = np.vsplit(a,2)
print(b)

Output Result:

First Array:
[[ 0 1 2 3]]
 [ 4 5 6 7]]
 [ 8 9 10 11]]
 [12 13 14 15]]
Vertical Split:
[array([[ 1, 2, 3],
       [4, 5, 6, 7]]] 8, 9, 10, 11],
       [12, 13, 14, 15]]]