numpy.sign(array [,out])函數用於按元素表示數字的符號。
對於整數輸入,如果數組值大於0,則返回1;如果數組值小於0,則返回-1;如果數組值0,則返回0。
用法: numpy.sign()
參數:
array:[數組]輸入值。
out :[ndarray,可選]輸出數組與結果一起放置。
Return :[ndarray]返回數組的符號。如果數組是標量,則數組的符號將是標量。
代碼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)
相關用法
注:本文由純淨天空篩選整理自sanjoy_62大神的英文原創作品 numpy.sign() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。