返回NumPy中特定陣列元素的上界


要返回特定元素的上界,請在Python NumPy中使用**numpy.ceil()**方法中的索引值。標量x的上界是最小的整數i,使得i >= x。它通常表示為**$\mathrm{\lceil X \rceil}$**。該函式返回x中每個元素的上界,資料型別為浮點數。如果x是標量,則這是一個標量。

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

步驟

首先,匯入所需的庫:

import numpy as np

使用array()方法建立一個數組:

arr = np.array([39.5, 57.4, 100.8, 34.9, 76.5, -59.8])

顯示陣列:

print("Array...
", arr)

獲取陣列的型別:

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

獲取陣列的維度:

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

獲取陣列中元素的數量:

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

逐元素返回陣列元素的上界:

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

要返回特定元素的上界,請在Python NumPy中使用numpy.ceil()方法中的索引值:

print("
Result (ceil of a specific element)...
",np.ceil(arr[3]))

示例

import numpy as np

# Create an array using the array() method
arr = np.array([39.5, 57.4, 100.8, 34.9, 76.5, -59.8])

# 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) # Return the ceil of the array elements, element-wise print("
Result (ceil)...
",np.ceil(arr)) # To return the ceil of a specific element, use the index value in the numpy.ceil() method in Python Numpy print("
Result (ceil of a specific element)...
",np.ceil(arr[3]))

輸出

Array...
[ 39.5 57.4 100.8 34.9 76.5 -59.8]

Our Array type...
float64

Our Array Dimensions...
1

Number of elements...
6

Result (ceil)...
[ 40. 58. 101. 35. 77. -59.]

Result (ceil of a specific element)...
35.0

更新於:2022年2月8日

156 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.