Know About Numpy Heaviside in Python

Numpy, which stands for Numerical Python, is a python library used to work with multi-dimensional arrays and matrices. With the numpy library, we can incorporate several mathematical functions while using arrays. Moreover, with numpy, we can process arrays in python at a faster rate. In this article, we will be covering the heaviside() function present in the numpy library.

What is heaviside() step function

The heaviside step function is also known as the unit step function. A unit step function is a function that is zero when the value of time is negative and one when it is positive. Likewise, for a heaviside() function, its value is zero for all the negative values and one for all the positive values. If the value turns out to be zero, the function’s value is explicitly mentioned as the second argument. Let us understand the function with the help of an example.

numpy heaviside

Here, the function f(x,y) is a heaviside function. Here, the first parameter passed to the function contains the values which have to be checked.

If the value of x is less than zero, i.e., negative then the function value is zero. If the value of x is greater than zero, i.e., positive, then the function value is 1.

But if the value x turns out to be equal to zero, then the value of the function will be y, i.e., the second parameter.

For the function f( 0.5 , 2 ), the output will be 1 because the x value 0.5 is greater than zero.

If the function was f( -1.5, 2 ), then the output will be 0 because the x value -1. 5 is negative. On the other hand, had the function been f( 0, 2 ), the output would have been 2 because the value of x was zero. So, the function will be assigned the value of y, i.e., the second argument.

The graph for the heaviside() function is:

numpy heaviside graph

For all the negative values, the function would give value 0 as the output and 1 for all the positive values.

Syntax of numpy.Heaviside() function

The heaviside() function in numpy is used as the unit step function. Its syntax is:

numpy.heaviside(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'heaviside'>


  • x1: This is the first argument for the heaviside() function. It is the input value that has to be checked and based on that. After that function will be assigned a value. It can be a single value or even a list.
  • x2: This is the second argument. It is the value that we want to assign to the function if the first argument x1 turns out to be zero. It can be a single value or a list, depending on the size of the first argument.
  • out: It is an optional argument that, if not mentioned, is ‘None’ by default. It is the location where we want to store our results. If no value or None is mentioned, then a new array is allocated containing the result of the function.
  • where: This again is an optional argument that is ‘True’ by default. If it is False, then the function will return its original value. But, if it is True, then the output will be set to the ufunc result.

Return Value:

out: It is an n-dimensional array that is returned as the output, which is the step function value.

Using numpy.heaviside() in Python

To use the heaviside() function in python, we will first import the numpy library.

import numpy as np

Now, first, we shall pass individual values to understand how they function works. We shall pass three different values for the first argument to understand the three different possible outputs. For the second argument, we will pass value 2.


In the first statement, the value of x is negative. In the second statement, the value of x is positive and for the third statement the value of x is equal to zero.

The output will be:


Instead of passing individual values, we shall pass a list of values as the first argument. The list my_list contains five numbers for which a heaviside function will be checked.

import numpy as np
my_list = [-0.5, 1, 1.75, -1.5, 0]

The output will be:

[0. 1. 1. 0. 2.]

For the first value, -0.5, since the value is negative, the function will output zero. Similarly, this will apply to all the other values in the list.

We can also pass a list as the second argument. The length of the list should be the same as the length of the list passed as the first argument.

import numpy as np
list1 = [-0.5, 1, 1.75, -1.5, 0]
list2 = [1,2,1.5,1,2.5]

The output is:

[0.  1.  1.  0.  2.5]

Each value from both the lists is taken and the comparison is done accordingly.

-0.51.20 because x is negative
121 because x is positive
1.751.51 because x is positive
-1.510 because x is negative
02.52.5 because x is equal to zero

Also Read | Numpy Convolve For Different Modes in Python

FAQ’s on Numpy Heaviside

What are the applications of heaviside function?

The heaviside function is used for signals which have to be switched at some particular time intervals.

That is all for Numpy Heaviside In Python. Have any thoughts to share? Let us know in the comments below.

Until then, Keep Learning!

Notify of
Inline Feedbacks
View all comments