返回NumPy陣列元素的向下取整值


要返回陣列元素的向下取整值,逐元素使用 Python NumPy 中的 **numpy.floor()** 方法。標量 x 的向下取整值是最大的整數 i,使得 i <= x。它通常表示為 **$\mathrm{\lceil X \rceil}$**。該函式返回 x 中每個元素的向下取整值。如果 x 是標量,則這是一個標量。out 是儲存結果的位置。如果提供,它必須具有輸入廣播到的形狀。如果未提供或為 None,則返回一個新分配的陣列。元組(僅可能作為關鍵字引數)的長度必須等於輸出的數量。

步驟

首先,匯入所需的庫 -

import numpy as np

建立一個數組 -

arr = np.array([97.3, 57.4, 100.8, 50, -10.5])

顯示陣列 -

print("Array...
", arr)

獲取陣列的型別 -

print("
Our Array type...
", arr.dtype)

獲取陣列的維度 -

print("
Our Array Dimensions...
", arr.ndim)

獲取陣列的元素數量 -

print("
Number of elements...
", arr.size)

要返回陣列元素的向下取整值,逐元素使用 Python NumPy 中的 numpy.floor() 方法 -

print("
Result (floor)...
",np.floor(arr))

示例

import numpy as np

# Create an array
arr = np.array([97.3, 57.4, 100.8, 50, -10.5])

# Display the array
print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimensions...
", arr.ndim) # Get the number of elements in the Array print("
Number of elements...
", arr.size) # To return the floor of the array elements, element-wise, use the numpy.floor() method in Python Numpy print("
Result (floor)...
",np.floor(arr))

輸出

Array...
[ 97.3 57.4 100.8 50. -10.5]

Our Array type...
float64

Our Array Dimensions...
1

Number of elements...
5

Result (floor)...
[ 97. 57. 100. 50. -11.]

更新於: 2022年2月8日

110 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.