How to Use Numpy Sign Function | np.sign

The numpy library in python is used for working with matrices and multi-dimensional arrays in python. There are many functions available in the numpy library that aid in processing arrays and matrices at a faster rate. The functions help in performing scientific and mathematical operations efficiently. In this article, we shall be looking into one such numpy function – np.sign.

The numpy sign function

The sign function in numpy, which is accessed using np.sign, is used to indicate the sign of a given number. It takes an array like value as an input and indicates the sign of each element individually. The function can also be used for indicating the sign of a complex number.

The sign is indicated by using the below convention:


If the input number or the element of the array is less than 0, then the sign() function will return -1. If the number or element is greater than 0, then the sign() function will return 1. Else, if the number or element is equal to zero, then the return value shall be zero.

Syntax of np.sign

The syntax of numpy’s sign function is:

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

Parameters of np.sign:

x: It is an array like input whose elements’ sign has to be found.

out: It is an optional parameter which can be either an n dimensional array, a tuple of n dimensional array or None. By default, it has None value. It is the location where the function’s result will be stored. If given, the shape of the tuple or array should be similar to the input shape. Else, the return value will be stored in a new array.

where: It is also an optional value which is array like. This indicates the condition to be mentioned for the input values.

Return Value of np.sign:

y: It is an n dimensional array which indicates the sign of the input elements.

Working of np.sign

Let us understand the basic working of np.sign function using python. First, we will have to import the numpy library.

import numpy as np

Passing a negative number:

We shall pass a negative number as an argument to the np.sign function. The output would be -1 as the number shall be less than zero.




Passing zero:

On passing zero to the np.sign function, the output will also be zero.




Passing a positive number:

On passing a number greater than zero, the output will be 1.




The entire code is:

import numpy as np

Passing arrays to the sign function

Now, we shall be passing a numpy array as an argument to the sign function. The function will print a sign for each individual element.

After importing numpy, we shall be creating an array using np.array() function. Then, we shall pass that array to the sign() function.

import numpy as np
array = np.array([-4, 11, 2.0, 0.01, 0, -8])

The output is:

[-1.  1.  1.  1.  0. -1.]

Here each individual number signifies the sign for each individual digit respectively. Since there were two floating values – 2.0 and 0.01, the output array also contains floating numbers.

Passing matrices into the sign function

We can also use the np.sign function to indicate the sign of the elements present in a matrix. For that, we shall be creating a matrix using the matrix function present in numpy. It will be a 4 by 4 matrix where the range of the numbers stored will be from -5 to 11.

import numpy as np
matrix = np.matrix(np.arange(-5,11).reshape((4,4)))

On printing the matrix, it will be:

[[-5 -4 -3 -2]
 [-1  0  1  2]
 [ 3  4  5  6]
 [ 7  8  9 10]]

Now, we shall pass this ‘matrix’ as an argument to the sign function and print the return value.


The output will be:

[[-1 -1 -1 -1]
 [-1  0  1  1]
 [ 1  1  1  1]
 [ 1  1  1  1]]

As you can see here the output is also a matrix of same size as the input matrix – 4 by 4. The values from -5 to -1 are given the values of -1, the 0 value remains 0 and the values 1 to 10 are 1.

The entire code is:

import numpy as np
matrix = np.matrix(np.arange(-5,11).reshape((4,4)))


Complex values on np.sign function

We can also use the sign function for indicating the sign of complex numbers. These numbers have two parts – the real part and the imaginary part.

When given complex numbers, the sign of the real part will be returned as that of a real number along with 0j.

If x is the real part and yj is the imaginary part, then for ( x + yj ), the output will be ( np.sign(x) + 0j ).

When real number is nonzero:

Here, it will apply sign function on the real part and return that along with 0j.

import numpy as np
print(np.sign(-9 + 2j))
print(np.sign(7 + 2j))

The output is:


When real number is zero:

Here, it will apply the sign function on the imaginary part and return that along with 0j.

import numpy as np
print(np.sign(0 + 2j))

The output is:


Had the imaginary part also been zero, the output would be 0j.

import numpy as np
print(np.sign(0 + 0j))



Also, Read

Np.sign FAQ

Here are a few questions we hear often when talking about Np.sign.

What will be the output of sign function for null values?

For all the ‘nan’ values, the numpy sign function will output ‘nan’ .

import numpy as np



How to ignore nan in np.sign function?

We can make use of Masked Arrays in order to ignore the nan values while using np.sign function. So, the nan values will not be considered while using the sign function.

import numpy as np
array = np.array([11, np.nan,-5, np.nan,6,0])
array =, np.isnan(array))


[1.0 -- -1.0 -- 1.0 0.0]

That is all for numpy’s sign function. If you have any questions in your mind, feel free to let us know in the comments below.

Until next time, Keep Learning!

Notify of
Inline Feedbacks
View all comments