返回NumPy中輸入的向下取整值


要返回輸入的向下取整值,請在Python NumPy中使用**numpy.floor()**方法。標量x的向下取整值是最大的整數i,使得i <= x。它通常表示為**$\mathrm{\lfloor X \rfloor}$**。該函式返回x中每個元素的向下取整值。如果x是標量,則這是一個標量。

out是一個將結果儲存其中的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果沒有提供或為None,則返回一個新分配的陣列。元組(只能作為關鍵字引數)的長度必須等於輸出的數量。

步驟

首先,匯入所需的庫:

import numpy as np

要返回輸入的向下取整值,請在Python NumPy中使用numpy.floor()方法。

檢查浮點數的floor():

print("Result? ", np.floor(45.7))
print("
Result? ", np.floor(-487.4))

檢查無窮大的floor():

print("
Result? ", np.floor(-np.inf))

檢查NaN和無窮大的floor():

print("
Result? ", np.floor(np.nan)) print("
Result? ", np.floor(np.inf))

檢查對數的floor():

print("
Result? ", np.floor(np.log(1))) print("
Result? ", np.floor(np.log(2)))

示例

import numpy as np

# To return the floor of the input, use the numpy.floor() method in Python Numpy
print("Returning the floor value...
") # Check floor() for float print("Result? ", np.floor(45.7)) print("
Result? ", np.floor(-487.4)) # Check floor() for inf print("
Result? ", np.floor(-np.inf)) # Check floor() for nan and inf print("
Result? ", np.floor(np.nan)) print("
Result? ", np.floor(np.inf)) # Check floor() for log print("
Result? ", np.floor(np.log(1))) print("
Result? ", np.floor(np.log(2)))

輸出

Returning the floor value...

Result? 45.0

Result? -488.0

Result? -inf

Result? nan

Result? inf

Result? 0.0

Result? 0.0

更新於:2022年2月8日

173 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告