NumPy signbit()

The numpy.signbit() function returns a boolean array indicating whether the sign bit of each element in the input array is set (i.e., whether the value is negative).

Syntax

</>
Copy
numpy.signbit(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput values for which the sign bit will be checked.
outndarray, None, or tuple of ndarray and None, optionalOptional output array to store results. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the sign bit.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns a boolean array where True indicates that the corresponding input element has a negative sign bit. If the input is a scalar, a scalar boolean is returned.


Examples

1. Checking the Sign Bit for Single Values

Here, we check if a single number has its sign bit set.

</>
Copy
import numpy as np

# Check the sign bit for a positive and a negative number
pos_value = 5.0
neg_value = -3.2

# Compute signbit for both values
result_pos = np.signbit(pos_value)
result_neg = np.signbit(neg_value)

# Print results
print("Sign bit for 5.0:", result_pos)
print("Sign bit for -3.2:", result_neg)

Output:

Sign bit for 5.0: False
Sign bit for -3.2: True

2. Checking the Sign Bit for an Array

We check whether each element in an array has its sign bit set.

</>
Copy
import numpy as np

# Define an array of numbers
arr = np.array([3.5, -2.1, 0, -0.0, -10, 8])

# Compute signbit for each element in the array
sign_bits = np.signbit(arr)

# Print results
print("Input array:", arr)
print("Sign bit array:", sign_bits)

Output:

Input array: [  3.5  -2.1   0.   -0.  -10.    8. ]
Sign bit array: [False  True False  True  True False]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of numbers
arr = np.array([-5, 7, -8, 0, -2.5])

# Create an output array with the same shape
output_array = np.empty_like(arr, dtype=bool)

# Compute signbit and store the result in output_array
np.signbit(arr, out=output_array)

# Print the results
print("Computed sign bit array:", output_array)

Output:

Computed sign bit array: [ True False  True False  True]

4. Using the where Parameter

Using a condition to compute the sign bit only for selected elements.

</>
Copy
import numpy as np

# Define an array of numbers
arr = np.array([3, -2, 5, -7, 0, -1])

# Define a mask (compute signbit only where mask is True)
mask = np.array([False, True, False, True, False, True])

# Compute signbit values where mask is True
result = np.signbit(arr, where=mask)

# Print the results
print("Computed sign bit array with mask:", result)

Output:

Computed sign bit array with mask: [False  True False  True False  True]

The sign bit values are computed only for elements where mask=True. The other values remain unchanged.