NumPy sign()

The numpy.sign() method determines the sign of each element in an array.

Example

import numpy as np

# create an array
array1 = np.array([9, -3, -8, 0])

# determine the sign of each element in the array result = np.sign(array1)
print(result) # Output:[1 -1 -1 0]

sign() Syntax

The syntax of the numpy.sign() method is:

numpy.sign(x, out = None, where = True, casting = 'same_kind', dtype = None)

sign() Arguments

The numpy.sign() method takes the following arguments:

  • x - the array whose elements' signs are to be determined
  • out (optional) - the output array where the result will be stored
  • where (optional) - a boolean array or condition specifying which elements should be updated
  • casting (optional) - casting behavior when converting data types
  • dtype (optional) - the data type of the returned output

sign() Return Value

The numpy.sign() method returns an array with the same shape as the input, containing the sign of each array element.


Example 1: Determine the Sign of Each Array Element

import numpy as np

# create an input array
array1 = np.array([-2, 5, 0, -7])

# determine the sign of each element in the array result = np.sign(array1)
print(result)

Output

[-1  1  0 -1]

In the above example, we saw how we can use numpy.sign() to determine the sign of each element in an array.

The resulting array contains -1 for negative values, 0 for zero, and 1 for positive values.


Example 2: Use of out and where in sign()

import numpy as np

array1 = np.array([[-2, 0, 3], [5, -4, 0]])

# create an output array with the same shape as x
output = np.zeros_like(array1)  

# compute the sign of each element # where the element is non-zero, # and store the result in the output array np.sign(array1, where = array1 != 0, out = output)
print(output)

Output

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

Notice the following code in the program above:

np.sign(array1, where = array1 != 0, out = output)

This code computes the sign of each element in array1, but only where the element is non-zero (based on the where condition).

And the result is stored in the output array.


Example 3: Using Optional casting Argument in sign()

The casting argument specifies the casting behavior when converting data types.

The casting can be:

  • 'no' - data types should not be cast at all
  • 'equiv' - only byte-order changes are allowed
  • 'safe' - only casts that can preserve the precision of the values are allowed
  • 'same_kind' - only safe casts or casts within a kind are allowed
  • 'unsafe' - any data conversions may be done

Let's look at an example.

import numpy as np

# array of floating-point numbers
array1 = np.array([1, -2, 0, 4, -5], dtype = np.float32)

# no casting is allowed # same data type as array1 is maintained array2 = np.sign(array1, casting = 'no')
# casting is allowed to equivalent # data types (floating-point numbers) array3 = np.sign(array1, casting = 'equiv')
# casting is allowed to safe data types that # preserve precision (floating-point numbers) array4 = np.sign(array1, casting = 'safe')
# casting is allowed to data types of # the same kind (floating-point numbers) array5 = np.sign(array1, casting = 'same_kind')
# casting is allowed to any data type # without checks (signed integers) array6 = np.sign(array1, casting = 'unsafe')
print("Array with 'no' casting:", array2) print("Array with 'equiv' casting:", array3) print("Array with 'safe' casting:", array4) print("Array with 'same_kind' casting:", array5) print("Array with 'unsafe' casting:", array6)

Output

Array with 'no' casting: [ 1. -1.  0.  1. -1.]
Array with 'equiv' casting: [ 1. -1.  0.  1. -1.]
Array with 'safe' casting: [ 1. -1.  0.  1. -1.]
Array with 'same_kind' casting: [ 1. -1.  0.  1. -1.]
Array with 'unsafe' casting: [ 1. -1.  0.   1. -1.]

Example 4: Use of dtype Argument in sign()

import numpy as np

# original array of integers
array1 = np.array([1, -2, 0, 4, -5])

# compute the sign and specify the data type as float32 result = np.sign(array1, dtype = np.float32)
print(result)

Output

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

Here, the original array array1 contains integers, but by specifying dtype = np.float32, the np.sign() method casts the result to np.float32.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges