Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return an element-wise indication of the sign of complex types in Numpy
To return an element-wise indication of the sign of complex types, use the numpy.sign() method in Python Numpy.
The sign function returns -1 if x 0. nan is returned for nan inputs. For complex inputs, the sign function returns sign(x.real) + 0j if x.real != 0 else sign(x.imag) + 0j.
The complex(nan, 0) is returned for complex nan inputs. There is more than one definition of sign in common use for complex numbers. The definition used here is equivalent to x/x*x which is different from a common alternative, x/|x|.
Steps
At first, import the required library −
import numpy as np
Create an array with complex type using the array() method −
arr = np.array([56.+0.j, 27.+0.j, 68.-2.j, 49.+0.j, 120.-5.j,3 + 4.j])
Display the array −
print("Array...<br>", arr)
Get the type of the array −
print("\nOur Array type...<br>", arr.dtype)
Get the dimensions of the Array −
print("\nOur Array Dimension...<br>",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...<br>",arr.shape)
To return an element-wise indication of the sign of complex types, use the numpy.sign() method −
print("\nResult...<br>",np.sign(arr))
Example
import numpy as np
# Create an array with complex type using the array() method
arr = np.array([56.+0.j, 27.+0.j, 68.-2.j, 49.+0.j, 120.-5.j,3 + 4.j])
# Display the array
print("Array...<br>", arr)
# Get the type of the array
print("\nOur Array type...<br>", arr.dtype)
# Get the dimensions of the Array
print("\nOur Array Dimension...<br>",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...<br>",arr.shape)
# To return an element-wise indication of the sign of complex types, use the numpy.sign() method in Python Numpy
print("\nResult...<br>",np.sign(arr))
Output
Array... [ 56.+0.j 27.+0.j 68.-2.j 49.+0.j 120.-5.j 3.+4.j] Our Array type... complex128 Our Array Dimension... 1 Our Array Shape... (6,) Result... [1.+0.j 1.+0.j 1.+0.j 1.+0.j 1.+0.j 1.+0.j]
