在 NumPy 中更改值的符號以與另一個值相同
要將一個值的符號更改為另一個值的符號,請在 Python NumPy 中使用 **numpy.copysign()** 方法。copysign() 的第一個引數是要更改其符號的值。第二個引數是要複製到第一個引數值的符號。
out 是將結果儲存到的位置。如果提供,則其形狀必須是輸入廣播到的形狀。如果不提供或為 None,則返回一個新分配的陣列。元組(僅可能作為關鍵字引數)的長度必須等於輸出的數量。
條件在輸入上廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化狀態。
步驟
首先,匯入所需的庫:
import numpy as np
要將一個值的符號更改為另一個值的符號,請在 Python NumPy 中使用 numpy.copysign() 方法。檢查整數:
print("Result? ", np.copysign(0, 1))
print("
Result? ", np.copysign(0, -1))檢查浮點數:
print("
Result? ", np.copysign(14., -1))
print("
Result? ", np.copysign(-3.6, 1))檢查 NaN:
print("
Result? ", np.copysign(np.nan, -1))
print("
Result? ", np.copysign(np.NAN, 1))檢查無窮大:
print("
Result? ", np.copysign(np.inf, -1))
print("
Result? ", np.copysign(np.NINF, -1))檢查對數:
print("
Result? ", np.copysign(np.log(1), 1))
print("
Result? ", np.copysign(np.log(2), -1))示例
import numpy as np
# To change the sign of a value to that of another, use the numpy.copysign() method in Python Numpy
# The 1st parameter of the copysign() is the value to change the sign of.
# The 2nd parameter is the sign to be copied to 1st parameter value.
print("Result? ", np.copysign(0, 1))
print("
Result? ", np.copysign(0, -1))
# Checking for float
print("
Result? ", np.copysign(14., -1))
print("
Result? ", np.copysign(-3.6, 1))
# Checking for NaN
print("
Result? ", np.copysign(np.nan, -1))
print("
Result? ", np.copysign(np.NAN, 1))
# Checking for infinity
print("
Result? ", np.copysign(np.inf, -1))
print("
Result? ", np.copysign(np.NINF, -1))
# Checking for log
print("
Result? ", np.copysign(np.log(1), 1))
print("
Result? ", np.copysign(np.log(2), -1))輸出
Result? 0.0 Result? -0.0 Result? -14.0 Result? 3.6 Result? nan Result? nan Result? -inf Result? -inf Result? 0.0 Result? -0.6931471805599453
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP