Python中的 numpy.sign()
numpy.sign(array [, out])函数用于指示数字元素的符号。
对于整数输入,如果数组值大于 0,则返回 1,如果数组值小于 0,则返回 -1,如果数组值 0,则返回 0。
Syntax: numpy.sign()
Parameters :
array : [array_like] Input values.
out : [ndarray, optional] Output array placed with result.
Return : [ndarray] Returns the sign of array. If an array is scalar then the sign of array will be scalar.
代码 1:
# Python Program illustrating
# numpy.sign() method
# importing numpy
import numpy as geek
# input arrays
array1 = [1, 0, -13]
array2 = [-1, 0, 15]
# print the input arrays
print ("input array1 : ", array1)
print ("input array2 : ", array2)
# determine the sign of integer numbers in an array
print ("\nCheck sign of array1 : ", geek.sign(array1))
print ("\nCheck sign of array2 : ", geek.sign(array2))
输出 :
array1 : [1, 0, -13]
array2 : [-1, 0, 15]
Check sign of array1 : [ 1 0 -1]
Check sign of array2 : [-1 0 1]
代码 2:
# Python Program illustrating
# numpy.sign() method
# importing numpy
import numpy as geek
# determine the sign of complex number
print ("\n Check sign of complex input1 : ", geek.sign(7-3j))
print ("\n Check sign of complex input2 : ", geek.sign(-7 + 3j))
输出 :
Check sign of complex input1 : (1+0j)
Check sign of complex input2 : (-1+0j)