Skip to main content

Introduction to Python NumPy Splitting Array

Python NumPy Splitting Array

In NumPy, you can split an array into multiple smaller arrays along a specified axis using different functions.

Here are some common functions for splitting arrays:

Using np.split()

The np.split() function splits an array into multiple sub-arrays along a specified axis. It takes three arguments: the array to be split, the number of equally-sized sub-arrays to create, and the axis along which to split the array.

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

# Split the array into three sub-arrays along the first axis (axis=0)
result = np.split(arr, 3)
print(result)

Output:

[array([1, 2]), array([3, 4]), array([5, 6])]

Using np.array_split()

The np.array_split() function is similar to np.split(), but it allows for unequal division of the array.

You can specify the number of sub-arrays to create, and it will try to evenly distribute the elements among the sub-arrays.

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

# Split the array into four sub-arrays using np.array_split()
result = np.array_split(arr, 4)
print(result)

Output:

[array([1, 2]), array([3, 4]), array([5, 6]), array([7])]

Using np.hsplit() and np.vsplit()

The np.hsplit() function splits an array horizontally along the second axis (columns), while np.vsplit() splits an array vertically along the first axis (rows).

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Split the array horizontally (columns) using np.hsplit()
hsplit_result = np.hsplit(arr, 3)
print(hsplit_result)

# Split the array vertically (rows) using np.vsplit()
vsplit_result = np.vsplit(arr, 3)
print(vsplit_result)

Output:

[array([[1],
[4],
[7]]), array([[2],
[5],
[8]]), array([[3],
[6],
[9]])]
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]

Splitting 2-D Arrays

When splitting a 2D NumPy array, you can use the functions np.split(), np.hsplit(), and np.vsplit() to split the array into smaller sub-arrays along different axes.

Here are some examples:

Using np.split()

The np.split() function can be used to split a 2D array into multiple sub-arrays along a specified axis.

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

# Split the array into two sub-arrays along the second axis (columns)
result = np.split(arr, 2, axis=1)
print(result)

Output:

[array([[1, 2],
[5, 6],
[9, 10]]), array([[3, 4],
[7, 8],
[11, 12]])]

Using np.hsplit()

The np.hsplit() function splits a 2D array horizontally along the second axis (columns) into multiple sub-arrays.

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

# Split the array horizontally (columns) into two sub-arrays
hsplit_result = np.hsplit(arr, 2)
print(hsplit_result)

Output:

[array([[1, 2],
[5, 6],
[9, 10]]), array([[3, 4],
[7, 8],
[11, 12]])]

Using np.vsplit()

The np.vsplit() function splits a 2D array vertically along the first axis (rows) into multiple sub-arrays.

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

# Split the array vertically (rows) into two sub-arrays
vsplit_result = np.vsplit(arr, 2)
print(vsplit_result)

Output:

[array([[1, 2, 3, 4],
[5, 6, 7, 8]]), array([[ 9, 10, 11, 12]])]