What is the numpy.heaviside() function in NumPy?
Overview
In NumPy, the heaviside() function is used to compute the Heaviside step function of an input array passed to it.
Mathematical representation and rules
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.
Syntax
numpy.heaviside(x1, x2, /, out=None, *, where=True)
Syntax for the heaveside() function
Parameters
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.Return value
It returns an array containing the Heaviside step function on the input array.
Example
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)
Explanation
- Line 1: We import the
numpymodule.
- Line 4: We define the input array,
x1, and assign it some values.
- Line 5: We define the input value,
x2, and assign it a value of0.5.
- Line 8: We implement the
heaviside()function on the arrays,x1andx2, and assign the result to a variablemyarray.
- Lines 10–12: We print the input array,
x1andx2. We also print the output array,myarray, to the console.