Splitting Arrays in NumPy
Just like joining arrays, NumPy makes it simple to split arrays into multiple sub-arrays. These operations are helpful when you want to divide data for batching, preprocessing, or analysis.
NumPy provides flexible functions like np.split(), np.array_split(), np.hsplit(), and np.vsplit() to divide arrays along specified axes.
- Split equally: Use np.split() when the array can be divided into equal parts.
- Split flexibly: Use np.array_split() when equal division isn’t possible.
- Axis-based splitting: Use np.hsplit() and np.vsplit() for 2D horizontal and vertical splits.
Splitting arrays is especially useful when building training and test sets, distributing data across processes, or working with structured array layouts.
Splitting Arrays Equally with np.split()
The np.split() function is used to divide an array into multiple equal-sized parts. This only works if the array can be split evenly — otherwise, NumPy will raise an error.
Here’s an example of splitting a 1D array into 3 equal parts:
import numpy as np
# Create a 1D array with 6 elements
arr = np.array([10, 20, 30, 40, 50, 60])
# Split it into 3 equal parts
result = np.split(arr, 3)
print(result)
import numpy as np
# Create a 1D array with 6 elements
arr = np.array([10, 20, 30, 40, 50, 60])
# Split it into 3 equal parts
result = np.split(arr, 3)
print(result)
How It Works:
- The array has 6 elements and is split into 3 parts → each part has 2 elements.
- np.split(arr, 3) means “split into 3 equal sub-arrays”.
- If the array size wasn’t divisible by 3, this would raise a ValueError.
- You can also use keyword arguments like np.split(arr, indices_or_sections=3) for clarity.
Output
[array([10, 20]), array([30, 40]), array([50, 60])]
[array([10, 20]), array([30, 40]), array([50, 60])]
💡 Tip: Use np.split() only when you're sure the array can be divided evenly. Otherwise, use np.array_split() to avoid errors.
Splitting Arrays Flexibly with np.array_split()
The np.array_split() function works like np.split(), but it allows you to split arrays even when they can’t be divided evenly.
If the array size doesn't divide perfectly, NumPy will create sub-arrays of slightly different sizes — no error is raised.
Here’s an example of splitting a 1D array with 5 elements into 3 parts:
import numpy as np
# Create a 1D array with 5 elements
arr = np.array([10, 20, 30, 40, 50])
# Split into 3 parts (not evenly divisible)
result = np.array_split(arr, 3)
print(result)
import numpy as np
# Create a 1D array with 5 elements
arr = np.array([10, 20, 30, 40, 50])
# Split into 3 parts (not evenly divisible)
result = np.array_split(arr, 3)
print(result)
How It Works:
- The array has 5 elements, but we ask for 3 splits.
- NumPy creates 3 sub-arrays: two with 2 elements, one with 1 element.
- This function avoids errors and distributes the elements as evenly as possible.
Output
[array([10, 20]), array([30, 40]), array([50])]
[array([10, 20]), array([30, 40]), array([50])]
The result is a list of NumPy arrays. Each sub-array contains a chunk of the original data.
💡 Tip: Use array_split() when you're not sure the array can be divided evenly. It's more flexible than split() and won’t raise an error.
Axis-Based Splitting in NumPy
When working with 2D arrays (like tables or matrices), NumPy offers specific functions to split along rows or columns:
- np.hsplit() – splits horizontally (by columns)
- np.vsplit() – splits vertically (by rows)
Here’s an example using a 2D array with shape (2, 4):
import numpy as np
# Create a 2D array (2 rows, 4 columns)
arr = np.array([[10, 20, 30, 40],
[50, 60, 70, 80]])
# Split into 2 parts along columns (horizontal split)
h_result = np.hsplit(arr, 2)
# Split into 2 parts along rows (vertical split)
v_result = np.vsplit(arr, 2)
print("Horizontal split:")
print(h_result)
print("Vertical split:")
print(v_result)
import numpy as np
# Create a 2D array (2 rows, 4 columns)
arr = np.array([[10, 20, 30, 40],
[50, 60, 70, 80]])
# Split into 2 parts along columns (horizontal split)
h_result = np.hsplit(arr, 2)
# Split into 2 parts along rows (vertical split)
v_result = np.vsplit(arr, 2)
print("Horizontal split:")
print(h_result)
print("Vertical split:")
print(v_result)
How It Works:
- hsplit() splits along axis 1 (columns), dividing each row into chunks.
- vsplit() splits along axis 0 (rows), dividing the array into separate rows.
- These functions are shorthand versions of np.split() with a fixed axis.
Output
Horizontal split:
[array([[10, 20],
[50, 60]]),
array([[30, 40],
[70, 80]])]
Vertical split:
[array([[10, 20, 30, 40]]),
array([[50, 60, 70, 80]])]
Horizontal split:
[array([[10, 20],
[50, 60]]),
array([[30, 40],
[70, 80]])]
Vertical split:
[array([[10, 20, 30, 40]]),
array([[50, 60, 70, 80]])]
💡 Tip: Use hsplit() and vsplit() when working with 2D arrays and want quick column-wise or row-wise splitting without manually specifying the axis.
Frequently Asked Questions
How do I split an array in NumPy?
How do I split an array in NumPy?
You can use functions like np.split, np.array_split, np.hsplit, and np.vsplit to divide arrays into multiple sub-arrays.
What is the difference between np.split and np.array_split?
What is the difference between np.split and np.array_split?
np.split requires equal-sized splits and will raise an error otherwise, whereas np.array_split can split arrays unevenly.
Can I split a 2D array horizontally and vertically?
Can I split a 2D array horizontally and vertically?
Yes! Use np.hsplit for horizontal (column-wise) splits and np.vsplit for vertical (row-wise) splits.
What happens if the indices for splitting are out of range?
What happens if the indices for splitting are out of range?
NumPy will raise an IndexError if you provide indices outside the valid range of the array's shape. Always ensure indices are valid.
Are the split arrays views or copies?
Are the split arrays views or copies?
Split arrays are typically views of the original array, so changes to them can affect the original array.
What's Next?
Coming up next, we’ll explore Broadcasting in NumPy — a powerful feature that allows arrays of different shapes to be combined in arithmetic operations.