Last chance! 50% off DataCamp Premium
Sale ends in
The `split()` function in NumPy is used to divide an array into multiple sub-arrays. It is particularly useful for partitioning data for analysis or processing in smaller, more manageable segments.
The `split()` function is typically used when you need to divide an array into equally sized sub-arrays. It requires specifying the number of splits or the indices at which to split the array.
numpy.split(array, indices_or_sections, axis=0)
In this syntax, `array` is the input array to be split, `indices_or_sections` specifies the number of equal splits or the indices where the splits occur, and `axis` defines the axis along which to split.
When the array cannot be split evenly as per the specified `indices_or_sections`, a `ValueError` will be raised. Handle this exception to ensure robust code.
Before using any examples, ensure you have imported NumPy:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
result = np.split(arr, 3)
This example splits the 1D array `arr` into three equal sub-arrays, each containing two elements: `[1, 2]`, `[3, 4]`, and `[5, 6]`.
arr = np.array([10, 20, 30, 40, 50])
result = np.split(arr, [1, 3])
Here, the array `arr` is split at indices 1 and 3, resulting in sub-arrays: `[10]`, `[20, 30]`, and `[40, 50]`.
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
result = np.split(arr, 2, axis=0)
This example splits a 2D array `arr` along the first axis into two sub-arrays: `[[1, 2], [3, 4]]` and `[[5, 6], [7, 8]]`.
try:
arr = np.array([1, 2, 3, 4, 5])
result = np.split(arr, 3)
except ValueError as e:
print("Error:", e)
In this example, attempting to split an array of size 5 into 3 equal parts raises a `ValueError`. The error is caught and handled gracefully.