numpy.hsplit() function
×


numpy.hsplit() function

1417

Understanding the numpy.hsplit() Function in Python

Introduction

In numerical computing with Python, efficiently manipulating arrays is crucial. The numpy.hsplit() function provides a straightforward way to split arrays horizontally (column-wise), enabling flexible data manipulation and analysis.

What is numpy.hsplit()?

The numpy.hsplit() function is used to split a NumPy array into multiple sub-arrays horizontally along the second axis (columns). This operation is particularly useful when you need to partition data based on columns for further processing or analysis.

Syntax

numpy.hsplit(ary, indices_or_sections)

Parameters:

  • ary: The input array to be split.
  • indices_or_sections: If this is an integer, it specifies the number of equal-sized sub-arrays to split the array into. If it is an array of indices, it specifies the indices at which to split the array.

Returns: A list of sub-arrays obtained by splitting the input array.

Example 1: Splitting a 2D Array into Equal Parts

import numpy as np

arr = np.arange(16).reshape(4, 4)
print("Original Array:")
print(arr)

result = np.hsplit(arr, 2)
print("\nResult after np.hsplit():")
for sub_arr in result:
    print(sub_arr)

Output:

Original Array:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Result after np.hsplit():
[[[ 0  1]
  [ 4  5]
  [ 8  9]
  [12 13]]

 [[ 2  3]
  [ 6  7]
  [10 11]
  [14 15]]]

In this example, the 4x4 array is split into two 4x2 sub-arrays along the horizontal axis.

Example 2: Splitting a 2D Array at Specific Indices

indices = np.array([2, 6])
result = np.hsplit(arr, indices)
print("\nResult after np.hsplit() with indices:")
for sub_arr in result:
    print(sub_arr)

Output:

Result after np.hsplit() with indices:
[[[ 0  1  2]
  [ 4  5  6]
  [ 8  9 10]
  [12 13 14]]

 [[ 3]
  [ 7]
  [11]
  [15]]

 []]

Here, the array is split at columns 2 and 6. The last sub-array is empty because there are no columns between indices 6 and the end of the array.

Use Cases

The numpy.hsplit() function is particularly useful in scenarios such as:

  • Dividing large datasets into smaller batches for processing.
  • Splitting data into features and labels in machine learning workflows.
  • Segmenting multi-dimensional arrays for parallel processing.

Conclusion

The numpy.hsplit() function is a powerful tool for horizontally splitting arrays in Python. By understanding its syntax and applications, you can efficiently manipulate and analyze multi-dimensional data, making it an essential function in the NumPy library.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat