Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
numpy
python
hsplit
communitycreator

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

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)
Implementing the hsplit() function

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.

RELATED TAGS

function
numpy
python
hsplit
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring