NumPy split and array_split Python Function

In this article, we will cover NumPy’s two functions which are NumPy split and array_split.

Introduction to NumPy split and array_split

NumPy split

numpy.split() function used to Split an array into multiple sub-arrays as views into ary.

Syntax:

numpy.split(ary, indices_or_sections, axis=0)

Parameters:

  • ary: [ndarray] Array to be divided into sub-arrays.
  • indices_or_sections: [int or 1-D array] If indices_or_sections is an integer, N, the array will be divided into N equal arrays along an axis. If such a split is not possible, an error is raise. If indices_or_sections is a 1-D array of sorted integers, the entries indicate where along the axis the array is split. When if an index exceeds the dimension of the array along an axis, an empty sub-array is return correspondingly.
  • axis: [int, optional] The axis along which to split, default is 0.

Return: sub-arrays [list of ndarrays]

Raises: It raises a value error

NumPy array_split

The numpy.array_split() function may be use to get a split array with different dimensions.

Syntax:

numpy.array_split()

Return: Return the split array of one dimension.

Code Examples of NumPy split and array_split

NumPy split

Example 01:

# welcome to softhunt.net
# import numpy
import numpy as np

array = np.arange(16)

# using numpy.split() method
softhunt = np.split(array, 6)

print(softhunt)

Output:

Traceback (most recent call last):
  File "/tmp/sessions/36bb07e65b743769/main.py", line 8, in <module>
    softhunt = np.split(array, 6)
  File "<__array_function__ internals>", line 180, in split
  File "/usr/local/lib/python3.9/dist-packages/numpy/lib/shape_base.py", line 872, in split
    raise ValueError(
ValueError: array split does not result in an equal division

Example 02:

# welcome to softhunt.net
# import numpy
import numpy as np

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

# using numpy.split() method
softhunt = np.split(array, [3, 5, 6, 10])

print(softhunt)

Output:

[array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]]), array([], shape=(0, 3), dtype=int64), array([], shape=(0, 3), dtype=int64), array([], shape=(0, 3), dtype=int64), array([], shape=(0, 3), dtype=int64)]

NumPy array_split

Example 01:

# welcome to softhunt.net
# import numpy
import numpy as np

array = np.arange(16)

# using numpy.array_split() method
softhunt = np.array_split(array, 6)

print(softhunt)

Output:

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

Example 02:

# welcome to softhunt.net
# import numpy
import numpy as np

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

# using numpy.array_split() method
softhunt = np.array_split(array, 3)

print(softhunt)

Output:

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

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

How do you random shuffle an array in Python?

There will be different methods. these are init(), reset(), shuffle().

What is NumPy Python use for?

NumPy is a Python module that allows you to interact with arrays. It also provides functions for working with matrices, Fourier transforms, and linear algebra. Travis Oliphant invented NumPy in 2005. It is an open-source project that you are free to use.

split and array_split: Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. NumPy column_stack and row_stack Python Function

Leave a Comment