NumPy’s library provides a method called heaviside()
, which is used to calculate the heaviside step function of the input array. This is done element by element.
Note: A list of lists can be used to create a two-dimensional (2D) array in Python.
numpy.heaviside(x1,x2,out=None, where=True)
x1
: This represents the input array.x2
: This is array-like, and represents the value of the function when x1
is 0. x2
is commonly assumed to be 0.5. However, 0 and 1 are also used.out
: This parameter is optional. It specifies where the result is stored.where
: This parameter is optional. It represents the condition in which the input gets broadcasted.Note: If the shape of
x1
is not equal to the shape ofx2
, they must be broadcasted to a common shape.
The numpy.heaviside()
method returns the sign of each number of the input array.
The following code demonstrates how to use the numpy.heaviside()
method for Python two-dimensional (2D) arrays.
# import numpyimport numpy as np# create 2D array using np.arrayx1 = np.array([[-7, -3.4 , 1.2], [2, 3 , -9.5]])x2 = 0.5# Compute the heaviside step function of the array# using np.heaviside()result = np.heaviside(x1,x2)print(result)
numpy
library.x1
.x2
and assign a value to it.np.heaviside()
method to compute the heaviside step function of the input array.