numpy.sign()
methodThe sign()
method in Python’s NumPy
library returns the sign of each number of the input array. It is done element-wise.
x
is greater than 0
, the method returns 1
for that particular element.x
is less than 0
, the method returns -1
for that particular element.x
is equal to 0
, the method returns 0
for that particular element.x.real!= 0
, the sign()
method returns sign(x.real) + 0j
. Otherwise, it returns sign(x.imag) + 0j
.Note: In Python, a list of lists can be used to generate a two-dimensional (2D) array.
numpy.sign(x,out=None, where=True)
x
: This represents the input array.out
: This specifies where the result is stored. This is an optional parameter.where
: This represents the condition in which the input gets broadcasted. This is an optional parameter.The numpy.sign()
method returns the sign of each number of the input array.
The following code demonstrates how to use the numpy.sign()
method for 2D arrays.
# Import numpyimport numpy as np# Create 2D array using np.arrayx = np.array([[-7, -3.4 , 1.2 + 1j], [2, 3 + 1j, -9.5]])# Find the sign of each element in the array# Use np.sign()result = np.sign(x)print(result)
numpy
library.x
consisting of complex and non-complex elements.np.sign()
method to compute the sign of each number of the x
input array.