返回 NumPy 中特定数组元素的截断值
要返回特定元素的截断值,请在 Python NumPy 中的 **numpy.trunc()** 方法中使用索引值。
该函数返回 x 中每个元素的截断值。如果 x 是标量,则这是一个标量。标量 x 的截断值是比 x 更接近零的整数 i。简而言之,带符号数字 x 的小数部分将被丢弃。
out 是存储结果的位置。如果提供,它必须具有输入广播到的形状。如果未提供或为 None,则返回一个新分配的数组。元组(仅可能作为关键字参数)的长度必须等于输出的数量。
条件在输入上广播。在条件为 True 的位置,out 数组将设置为 ufunc 结果。在其他地方,out 数组将保留其原始值。请注意,如果通过默认的 out=None 创建了一个未初始化的 out 数组,则其中条件为 False 的位置将保持未初始化状态。
步骤
首先,导入所需的库 -
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 (trunc)...
",np.ceil(arr))要返回特定元素的截断值,请在 Python NumPy 中的 numpy.trunc() 方法中使用索引值 -
print("
Result (trunc of a specific element)...
",np.trunc(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 truncated value of the array elements, element-wise
print("
Result (trunc)...
",np.ceil(arr))
# To return the truncated value of a specific element, use the index value in the numpy.trunc() method in Python Numpy
print("
Result (trunc of a specific element)...
",np.trunc(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 (trunc)... [ 40. 58. 101. 35. 77. -59.] Result (trunc of a specific element)... 34.0
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP