NumPy sign()
The numpy.sign()
function returns an element-wise indication of the sign of a number.
It determines whether each element in an array is negative (-1), zero (0), or positive (1). For complex numbers, it returns x / abs(x)
.
Syntax
</>
Copy
numpy.sign(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Input array containing numeric values. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the sign function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with -1 for negative numbers, 0 for zero values, and 1 for positive numbers. If the input is a complex number, it returns x / abs(x)
. If the input is a scalar, a scalar is returned.
Examples
1. Computing Sign for a Single Number
Here, we compute the sign of a single numeric value.
</>
Copy
import numpy as np
# Define a number
num = -7
# Compute the sign of the number
result = np.sign(num)
# Print the result
print("Sign of -7:", result)
Output:
Sign of -7: -1

2. Computing Sign for an Array of Numbers
We compute the sign values for multiple numbers provided in an array.
</>
Copy
import numpy as np
# Define an array of numbers
numbers = np.array([-10, -3, 0, 4, 12])
# Compute the sign of each number
sign_values = np.sign(numbers)
# Print the results
print("Numbers:", numbers)
print("Sign values:", sign_values)
Output:
Numbers: [-10 -3 0 4 12]
Sign values: [-1 -1 0 1 1]

3. Computing Sign for Complex Numbers
For complex numbers, the sign function returns x / abs(x)
.
</>
Copy
import numpy as np
# Define an array of complex numbers
complex_numbers = np.array([3 + 4j, -2 - 2j, 0 + 0j])
# Compute the sign of each complex number
sign_complex = np.sign(complex_numbers)
# Print the results
print("Complex numbers:", complex_numbers)
print("Sign values:", sign_complex)
Output:
Complex numbers: [ 3.+4.j -2.-2.j 0.+0.j]
Sign values: [ 0.6+0.8j -0.70710678-0.70710678j 0. +0.j ]

4. Using the where
Parameter
Using a condition to compute sign only for selected elements.
</>
Copy
import numpy as np
# Define an array of numbers
numbers = np.array([-5, 0, 3, -8, 7])
# Define a mask (compute sign only where mask is True)
mask = np.array([True, False, True, False, True])
# Compute sign values where mask is True
result = np.sign(numbers, where=mask)
# Print the results
print("Computed sign values with mask:", result)
Output:
Computed sign values with mask: [ -1 4602678819172646912 1
4609434218613702656 1]

The sign values are computed only for elements where mask=True
.