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().

Tue, 09/07/2021 - 18:47

Authored by

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