NumPy Array Split

Splits NumPy-array

Splitsen is het omgekeerde van koppelen.

Koppeling (Joining) is het samenvoegen van meerdere arrays in één, splitsen (Spliting) is het splitsen van een array in meerdere.

We gebruiken array_split() Splits het array, geef het te splitsen array en het aantal splitsen door aan de methode.

Example

Splits het array in 3 delen:

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

Run Instance

Opmerking:De retourwaarde is een array die drie arrays bevat.

Als er minder elementen in het array zijn dan het vereiste aantal, zal het van het einde aanpassen.

Example

Splits het array in 4 delen:

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

Run Instance

Tip:We hebben ook split() De methode is beschikbaar, maar wanneer er minder elementen in de bronarray zijn dan nodig voor het splitsen, zal het geen elementen aanpassen zoals in het voorbeeld hierboven,array_split() Werkt normaal, maar split() zal falen.

Splits in arrays

array_split() De retourwaarde van de methode is een array die elk gesplitst array bevat.

Als een array wordt gesplitst in 3 arrays, kunnen ze net als elk arrayelement worden benaderd uit het resultaat:

Example

Toegang tot het gesplitste array:

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

Run Instance

Splits een tweedimensionaal array

Gebruik dezelfde syntaxis om een tweedimensionaal array te splitsen.

Gebruik array_split() Method, voer het te splitsen array en het aantal te splitsen in. In.

Example

Split this 2-D into three 2-D arrays.

import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
newarr = np.array_split(arr, 3)
print(newarr)

Run Instance

The above example returns three 2-D arrays.

Let's look at another example, this time each element of the 2-D array contains 3 elements.

Example

Split this 2-D into three 2-D arrays.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3)
print(newarr)

Run Instance

The above example returns three 2-D arrays.

Additionally, you can specify the axis to be split.

The following example also returns three 2-D arrays, but they are split along the row (axis=1).

Example

Split this 2-D into three 2-D arrays along the row.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3, axis=1)
print(newarr)

Run Instance

Another solution is to use with hstack() opposite hsplit().

Example

Use the hsplit() method to split a 2-D array into three 2-D arrays along the row.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.hsplit(arr, 3)
print(newarr)

Run Instance

Tip:vsplit() and dsplit() Can be used with vstack() and dstack() Similar alternative methods.