
When working with arrays in Python, one of the most common tasks is splitting them into smaller parts. Whether you’re dealing with matrices for machine learning or simply trying to break down large datasets, numpy.hsplit()
is a valuable function for horizontally splitting arrays into multiple sub-arrays. In this article, I’ll dive deep into how numpy.hsplit()
works and provide the best examples.
Understanding numpy.hsplit()
The numpy.hsplit()
function is a convenient way to split an array along the second axis. This means that when working with a 2D array (which can be thought of as a matrix), it will split that matrix into parts along its columns.
Here’s the official function signature:
numpy.hsplit(ary, indices_or_sections)
- ary: The input array that needs to be split.
- indices_or_sections: This determines how the array will be split.
The second argument can either be an integer, specifying the number of equal sub-arrays to create, or a list of indices that define the exact split points.
Basic Example of numpy.hsplit()
Let’s start with a simple example to understand how this function works:
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12]])
# Splitting into 3 equal parts
result = np.hsplit(arr, 3)
# Displaying results
for i, sub_array in enumerate(result):
print(f"Sub-array {i}:\n{sub_array}\n")
Here’s what happens:
Original Array | Split Result |
---|---|
|
|
The function divides the array into 3 equal horizontal parts.
Using Custom Split Indices
Instead of specifying equal sections, we can manually provide the columns at which to split:
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12]])
# Splitting at specific indices
result = np.hsplit(arr, [2, 4])
# Displaying results
for i, sub_array in enumerate(result):
print(f"Sub-array {i}:\n{sub_array}\n")
In this case, the array is split at columns 2 and 4, resulting in uneven splits.
What Happens If The Number of Columns Doesn’t Divide Evenly?
If you try to divide an array into an unequal number of sections using an integer, NumPy will throw an error:
import numpy as np
arr = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10]])
# Attempting an uneven split
result = np.hsplit(arr, 3) # This will raise an error
You must ensure that your chosen number of sections divides evenly along the second axis, or instead provide explicit indices.
Practical Use Cases of numpy.hsplit()
The numpy.hsplit()
function is widely used in real-world applications:
- Processing Image Data – When working with image processing libraries, large image matrices are often divided into smaller segments using NumPy.
- Machine Learning – When splitting a dataset into features and labels,
hsplit()
is a convenient way to separate different input variables. - Time Series Data – If you have time-series data stored in a 2D array, splitting the feature columns is often necessary before feeding them into models.
Alternatives to numpy.hsplit()
There are a few other similar functions in NumPy for splitting arrays:
numpy.vsplit()
– Splits an array vertically (row-wise).numpy.array_split()
– A more flexible method that allows uneven splits.numpy.split()
– A general split function where you specify the axis to split on.
Conclusion
Now that you’ve seen how numpy.hsplit()
works in Python with the best examples, you can utilize it to efficiently split arrays in your projects. Whether you’re working with datasets, images, or machine learning models, understanding how to manipulate NumPy arrays correctly makes a huge difference in performance and functionality.