返回給定 NumPy 元組的尾數和指數對


要將尾數和指數作為給定元組的一對返回,請在 Python NumPy 中使用 **numpy.frexp()** 方法。輸出是結果儲存到的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果沒有提供或為 None,則返回一個新分配的陣列。元組(僅可能作為關鍵字引數)的長度必須等於輸出的數量。

NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等等。它支援各種硬體和計算平臺,並且與分散式、GPU 和稀疏陣列庫配合良好。

步驟

首先,匯入所需的庫:

import numpy as np

建立一個元組:

myTuple = (37.2, 39.2, 166.8, -14.8, 78,6, -19.8)

顯示陣列:

print("Tuple...
", myTuple)

元組的長度:

print("
Tuple length...
", len(myTuple))

元組的型別:

print("
Tuple type...
", type(myTuple))

要將尾數和指數作為給定元組的一對返回,請在 Python NumPy 中使用 numpy.frexp() 方法:

print("
Result...
",np.frexp(myTuple))

示例

import numpy as np

# Create a tuple
myTuple = (37.2, 39.2, 166.8, -14.8, 78,6, -19.8)

# Display the tuple
print("Tuple...
", myTuple) # Length of the tuple print("
Tuple length...
", len(myTuple)) # Type of the tuple print("
Tuple type...
", type(myTuple)) # To return mantissa and exponent as a pair of a given tuple, use the numpy.frexp() method in Python Numpy print("
Result...
",np.frexp(myTuple))

輸出

Tuple...
(37.2, 39.2, 166.8, -14.8, 78, 6, -19.8)

Tuple length...
7

Tuple type...
<class 'tuple'>

Result...
(array([ 0.58125 , 0.6125 , 0.6515625, -0.925 , 0.609375 , 0.75 , -0.61875 ]), array([6, 6, 8, 4, 7, 3, 5], dtype=int32))

更新於:2022年2月8日

96 次檢視

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告