返回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.