We use np.split() to break a big array into smaller parts. This helps us work with pieces of data separately.
np.split() for dividing arrays in NumPy
import numpy as np # Split array into multiple sub-arrays np.split(array, indices_or_sections, axis=0)
array is the original array you want to split.
indices_or_sections can be an integer (number of equal parts) or a list of indices where to split.
import numpy as np array = np.array([1, 2, 3, 4, 5, 6]) # Split into 3 equal parts parts = np.split(array, 3) print(parts)
import numpy as np array = np.array([1, 2, 3, 4, 5, 6]) # Split at indices 2 and 4 parts = np.split(array, [2, 4]) print(parts)
import numpy as np array = np.array([]) # Split empty array into 2 parts parts = np.array_split(array, 2) print(parts)
import numpy as np array = np.array([10]) # Split single element array into 1 part parts = np.split(array, 1) print(parts)
This program creates an array of 12 numbers and splits it into 4 equal parts. It prints the original array and each part after splitting.
import numpy as np # Create an array of 12 numbers numbers = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]) print("Original array:", numbers) # Split into 4 equal parts split_parts = np.split(numbers, 4) print("\nArray split into 4 parts:") for index, part in enumerate(split_parts, start=1): print(f"Part {index}:", part)
Time complexity is O(n) because the array elements are copied into new arrays.
Space complexity is O(n) since new arrays are created for each part.
A common mistake is trying to split into parts that do not evenly divide the array length, which causes an error.
Use np.array_split() if you want to split into unequal parts without error.
np.split() breaks an array into smaller arrays at specified points or into equal parts.
It requires the array to be divisible evenly if using an integer number of parts.
Useful for dividing data into chunks for separate analysis or processing.