Skip to main content

NumPy Horizontal Splitting: numpy.hsplit() function

It is used to split arrays into multiple sub-arrays column-wise (horizontally).

Syntax:

numpy.hsplit(array, sections)

Example:

For 1-D Array:

import numpy as np
a=np.arange(6)
b=np.hsplit(a,2)
print(b)

Output:

[array([0, 1, 2]), array([3, 4, 5])]

For 2-D Array:

import numpy as np
a=np.array([[1,2],[3,4],[5,6],[7,8]])
b=np.hsplit(a,2)
print(b)

Output:

[array([[1],
       [3],
       [5],
       [7]]), array([[2],
       [4],
       [6],
       [8]])]

Note: hsplit() is alternate to hstack().

Submitted by devanshi.srivastava on September 7, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.