NumPy sign() Function



The NumPy sign() function is used to determine the sign of each element in an array. It returns -1 for negative values, 0 for zero, and 1 for positive values.

Syntax

Following is the syntax of the NumPy sign() function −

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

Parameters

This function accepts the following parameters −

  • x: The input array whose elements' signs are to be determined.
  • out (optional): A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where (optional): This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Otherwise, it will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are summed. The dtype of x is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array containing the signs of each element in the input array x. If out is provided, it returns a reference to out.

Example: Basic Usage of sign() Function

In the following example, we create a 1-dimensional array with mixed signed numbers and use the sign() function to determine the sign of each element −

import numpy as np

# Creating a 1-dimensional array
arr = np.array([-5, 0, 3, -2, 4])

# Determining the sign of each element
result = np.sign(arr)
print(result)

Following is the output obtained −

[-1  0  1 -1  1]

Example: sign() Function with Floating-Point Numbers

In this example, we create an array of floating-point numbers and use the sign() function to find the signs of these numbers −

import numpy as np

# Creating a 1-dimensional array of floats
arr = np.array([-1.5, 2.0, -3.5, 0.0, 4.5])

# Determining the sign of each element
result = np.sign(arr)
print(result)

This will produce the following result −

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

Example: sign() Function with Complex Numbers

In this example, we create an array of complex numbers and use the sign() function to determine the signs of their real parts −

import numpy as np

# Creating an array of complex numbers
arr = np.array([1+2j, -3-4j, 0+1j, -2+0j])

# Determining the sign of the real part of each element
result = np.sign(arr)
print(result)

Following is the output of the above code −

[ 1.+0.j -1.+0.j  1.+0.j -1.+0.j]

Example: sign() Function with a Matrix

In this example, we create a 2-dimensional array (matrix) with mixed signed numbers and use the sign() function to determine the sign of each element −

import numpy as np

# Creating a 2-dimensional array
arr = np.array([[1, -2, 0], [-3, 4, -5]])

# Determining the sign of each element
result = np.sign(arr)
print(result)

This will produce the following result −

[[ 1 -1  0]
 [-1  1 -1]]
numpy_arithmetic_operations.htm
Advertisements