Numpy signbit – Returns element-wise True where signbit is set (less than zero)

Numpysignbit

Numpy is a free-to-use, open-source python library for various mathematical operations on multi-dimensional arrays that includes numerous in-built functions and packages to perform on the arrays.

In this article, we will be learning about the numpy.signbit() function which is one of the functions in the Numpy library.

What is signbit used for?

Numeric data can be Signed or Unsigned numbers. Signed numbers contain a sign bit (leftmost bit) that indicates the sign of the number. If the sign bit is 0, it means the number is positive and if the sign bit is 1, then it means the number is negative. Signbit is used to identify whether a number is positive or negative.

Signbit

Let’s understand with an example, the number -45 is written as 1 0 1 0 1 1 0 1 where the leftmost bit is the signbit. In this case, the leftmost bit (also known as the most significant bit) is 1 . One indicates a negative sign.

Signbit Example

What is Numpy signbit()?

NumPy signbit() is a mathematical function that returns an element-wise ‘True’ value if the element is less than zero (signbit is set) or ‘False’ value if the element is greater than zero (signbit is not set).

Syntax

numpy.signbit( arr, out = None, *, where = True)

Parameters

  • arr : array_like input values whose signbit you wish to check.
  • out : [Optional] ndarray, tuple of ndarray or None . It is a location to store the result. It requires the same dimensions as the input array. If it is not specified then a new array is allocated. A tuple (possible only as **kwargs) must have a length equal to the number of outputs.
  • **kwargs are keyword arguments, used to handle named arguments in a function. They allow you to pass keyword variable length of argument to a function. Refer the ufunc docs.
  • where : [Optional] array_like. If the condition is True, the ufunc will be calculated for the selected input. If it is set to false, no calculation will be made.

Output :

  • output: ndarray, Returns an output array with boolean values. The result is scalar if the input is scalar.

Working with NumPy signbit()

Let’s now start working on the numpy.signbit() method to understand it through examples.

Example 1: Numpy signbit() for integer and rational inputs

#includes numpy.signbit()
import numpy as np 

#declare array list as input whose signbit is to be checked
arr = np.array([1, -23, +34, 11, 3.5, -8.75, -2/-3])

#using signbit() function on input array 'arr'
np.signbit(arr)

The np.array() to declare the array input. To know more about arrays, you can refer to our article on Numpy Arrays.

Output:

Signbit Output For Integer And Rational Inputs 1

Example 2: Numpy signbit() for zero, infinity and NaN inputs

#includes numpy.signbit()
import numpy as np 

#declare array list as input whose signbit is to be checked
arr = np.array([0.0, -0, np.inf, -np.inf, np.nan, -np.nan])

#using signbit() function on input array 'arr'
np.signbit(arr)

Output:

Signbit Output For Zeroinfinity And Nan 1

In the above example, we can see that the resulting output remains false when the signbit() function is given the input zero or negative zero. This is because signit returns true only for negative input values.

Example 3: Numpy signbit() for scalar inputs

#includes numpy.signbit()
import numpy as np

#signbit function also works for scalar values
np.signbit(-180)

Output:

Signbit Output For Scalar Numbers 1

By default signbit() returns a boolean value on the basis of the positive or negative input value.

Summary:

In this article, we have learned about the signbit() function of the NumPy library. It is a function that returns a boolean value for every element of an input array by checking if the number is positive or negative.

References:

You can refer to the documentation of the NumPy to know more about such mathematical functions.