在 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([48.7, 100.8, 50.7, 67.9, 34.5, 69.8])
显示数组 -
print("Array...
", arr)获取数组的类型 -
print("
Our Array type...
", arr.dtype)
获取数组的维度 -
print("
Our Array Dimensions...
", arr.ndim)获取数组中元素的数量 -
print("
Number of elements...
", arr.size)
创建另一个具有相同形状的数组来存储结果 -
arrRes = np.array([5.2, 10.1, 15.7, 20.2, 25.9, 45.9])
要返回数组元素的上限(逐元素),请在 Python NumPy 中使用 numpy.ceil() 方法。我们将存储结果的新位置是 arrRes -
print("
Result (ceil)...
",np.ceil(arr, arrRes))
检查存储结果的新数组的值 -
print("
Result...
",arrRes)示例
import numpy as np
# Create an array using the array() method
arr = np.array([48.7, 100.8, 50.7, 67.9, 34.5, 69.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)
# Create another array with the same shape to store the result
arrRes = np.array([5.2, 10.1, 15.7, 20.2, 25.9, 45.9])
# To return the ceil of the array elements, element-wise, use the numpy.ceil() method in Python Numpy
# The new location where we will store the result is arrRes
print("
Result (ceil)...
",np.ceil(arr, arrRes))
# Check the value of the new array where our result is stored
print("
Result...
",arrRes)输出
Array... [ 48.7 100.8 50.7 67.9 34.5 69.8] Our Array type... float64 Our Array Dimensions... 1 Number of elements... 6 Result (ceil)... [ 49. 101. 51. 68. 35. 70.] Result... [ 49. 101. 51. 68. 35. 70.]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP