📜  Python中的 numpy.copysign()

📅  最后修改于: 2022-05-13 01:54:57.911000             🧑  作者: Mango

Python中的 numpy.copysign()

numpy.copysign(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) :这个数学函数帮助用户改变 arr1 和 arr2 的符号。 arr1 或 arr2 都可以是列表/序列或标量值。如果是序列,则两者必须具有相同的维度;否则 arr2 可以是标量值。

代码#1:

# Python program illustrating 
# copysign() method 
import numpy as np 
  
arr1 = [1, -23, +34, 11]
arr2 = [-1, 2, -3, -4]
  
print ("arr1 : ", arr1)
print ("arr2 : ", arr2)
  
print ("\nCheck sign of arr1 : ", np.signbit(arr1))
print ("\nCheck sign of arr2 : ", np.signbit(arr1))
print ("\nCheck for copysign : ", np.signbit(np.copysign(arr1, arr2)))

输出 :

arr1 :  [1, -23, 34, 11]
arr2 :  [-1, 2, -3, -4]

Check sign of arr1 :  [False  True False False]
Check sign of arr2 :  [False  True False False]
Check for copysign :  [ True False  True  True]


代码#2:

# Python program illustrating 
# copysign() method 
import numpy as np 
  
arr1 = [1, -23, +34, 11]
  
print ("\nCheck sign of arr2 : ", np.signbit(arr1))
print ("\nCheck for copysign : ", np.signbit(np.copysign(arr1, -3)))

输出 :

Check sign of arr2 :  [False  True False False]
Check for copysign :  [ True  True  True  True]