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.
x1: This represents the input array.
x2: This is array-like, and represents the value of the function when
x2is 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
x1is not equal to the shape of
x2, they must be broadcasted to a common shape.
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 numpy import numpy as np # create 2D array using np.array x1 = 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)
x2and assign a value to it.
np.heaviside()method to compute the heaviside step function of the input array.
View all Courses