Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
python
numpy
communitycreator

# What is the numpy.heaviside() 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

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:

$H(x1,x2) = 0$, if $x1 < 0$

$H(x1,x2) = x2$, if $x1 = 0$

$H(x1,x2) = 1$, if $x1 > 0$

Note: The$H$notation is used here to represent the Heavyside step function. There are also other notations like $\theta$, $u$, 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 array
x1 = np.array([-1.5, 0, 2.0])
x2 = 0.5

# implementing the heaviside() function
myarray = np.heaviside(x1, x2)

print("Input array =", x1)
print("Input value =", x2)
print("Output array =", myarray)
Implementing the heaviside() function

### Explanation

• Line 1: We import the numpy module.

• 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 of 0.5.

• Line 8: We implement the heaviside() function on the arrays, x1 and x2, and assign the result to a variable myarray.

• Lines 10–12: We print the input array, x1 and x2. We also print the output array, myarray, to the console.

RELATED TAGS

function
python
numpy
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

Learn in-demand tech skills in half the time 