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

Corte do Array do NumPy

Splitting is the reverse operation of joining.
Joining is the process of combining multiple arrays into one, splitting is the process of splitting an array into multiple.

Basic array splitting function as follows:

FunctionArray and operation
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 an array into subarrays along a specific axis, format 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 position along the axis (left open, right closed) axis: along which dimension to split, default is 0, horizontal splitting. For1Vertical splitting when

import numpy as np
a = np.arange(15)
print('Primeiro Array: ')
print(a)
print('\n')

b = np.split(a,5)
print(b)
print('\n')
print ('Split the position of the array in a one-dimensional array:')
b = np.split(a,[4,7])
print(b)

Resultados de Saída:

Primeiro 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))3, 4, 5))6, 7, 8)) 9, 10, 11))12, 13, 14]]
Split the position of the array in a one-dimensional array:
[array([0, 1, 2, 3))4, 5, 6)) 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 the corresponding adjustment from the end.

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

Resultados de Saída:

[array([1, 2))3, 4))5))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

A função numpy.hsplit é usada para dividir arrays horizontalmente, retornando um número específico de arrays de mesma forma.

import numpy as np
harr = np.floor(10 * np.random.random(2, 8))
print('Array original: ')
print(harr)
 
print('Divisão após: ')
print(np.hsplit(harr, 4))

Resultados de Saída:

Array original:
[7. 9. 2. 6. 8. 7. 4. 5.]
 [2. 5. 3. 5. 9. 4. 1. 3.]]
Divisão após:
[array([7. 9,
       [2. 5.]]2. 6,
       [3. 5.]]8. 7,
       [9. 4.]]4. 5,
       [1. 3.]]]

numpy.vsplit

numpy.vsplit divide o array ao longo do eixo vertical, utilizando a mesma sintaxe que o hsplit.

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

Resultados de Saída:

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