NumPy Array Splitting

Split NumPy arrays

Splitting is the reverse operation of joining.

Joining is the process of combining multiple arrays into one, while splitting is the process of dividing an array into multiple parts.

We use array_split() Split the array by passing the array to be split and the number of splits to it.

Example

Split the array into 3 parts:

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

Run Instance

Note:The return value is an array containing three arrays.

If the number of elements in the array is less than the required number, it will adjust from the end accordingly.

Example

Split the array into 4 parts:

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 also have split() The method is available, but when the elements in the source array are fewer than needed for splitting, it will not adjust the elements as in the example above,array_split() works normally, but split() will fail.

Split into arrays

array_split() The return value of the method is an array containing each split array.

If an array is split into 3 arrays, you can access them from the result as if they were any array elements:

Example

Access the split arrays:

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

Split two-dimensional arrays

Use the same syntax when splitting two-dimensional arrays.

Use array_split() The method takes the array to be split and the number of splits as arguments.

Example

Split this 2-D array 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 in the 2-D array contains 3 elements.

Example

Split this 2-D array 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.

In addition, 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 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.array_split(arr, 3, axis=1)
print(newarr)

Run Instance

Another solution is to use the same 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() You can use the same vstack() and dstack() Similar alternative methods.