What is the numpy.hsplit() function in NumPy?

Overview

The hsplit() function in NumPy is used to split an input array into multiple subarrays horizontally.

Syntax

numpy.hsplit(array, indices_or_sections, axis=0)
Syntax for the hsplit() function in NumPy

Parameter value

The hsplit() function takes the following parameter values:

  • array: This is the input array to be split. This is a required parameter.
  • indices_or_sections: This is an integer representation of the number of sections of the array to be split. An error is raised if the number of splits specified is not possible.
  • axis: This is the axis along which the split is done.

Return value

The hsplit() function returns a list of subarrays of the input array.

Example

import numpy as np
# creating the input arrray
first_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# splitting the input array into 3 sub-arrays
my_array = np.hsplit(first_array, 5)
# printing the split array
print(my_array)

Explanation

  • Line 1: We import the numpy module.
  • Line 3: We create an array, first_array, using the array() function.
  • Line 6: We split the input array into 5 subarrays using the hsplit() function. We assign the result to a variable my_array.
  • Line 9: We print the new split array my_array.

Free Resources