In NumPy, the heaviside()
function is used to compute the Heaviside step function of an input array passed to it.
The mathematical representation of the Heavyside step function, along with the rules for calculating it, is defined as follows:
Note: Thenotation is used here to represent the Heavyside step function. There are also other notations like , , and more.
numpy.heaviside(x1, x2, /, out=None, *, where=True)
This function takes the following parameter values:
x1
: This is the input array of values. This parameter is mandatory.x2
: This is the input value of the function when the value of x1
is 0
. This parameter is mandatory.out
: This is the location where the result is stored. This parameter is optional. where
: This is the condition over which the input is being broadcast. At a given location where this condition is True
, the resulting array will be set to the ufunc
result. Otherwise, the resulting array will retain its original value. This parameter is optional.**kwargs
: These are other keyword arguments. This is an optional parameter.It returns an array containing the Heaviside step function on the input array.
import numpy as np# creating the input arrayx1 = np.array([-1.5, 0, 2.0])x2 = 0.5# implementing the heaviside() functionmyarray = np.heaviside(x1, x2)print("Input array =", x1)print("Input value =", x2)print("Output array =", myarray)
numpy
module.x1
, and assign it some values.x2
, and assign it a value of 0.5
.heaviside()
function on the arrays, x1
and x2
, and assign the result to a variable myarray
.x1
and x2
. We also print the output array, myarray
, to the console.