寻找 Python 中标量值的最小数据类型
numpy.min_scalar() 方法查找最小数据类型。第一个参数是需要查找最小数据类型的值。对于标量,返回大小最小、标量类型最小的数据类型,它可以保存自己的值。对于非标量数组,返回矢量的 dtype 而不进行修改。浮点数不会降为整数,复杂值不会降为浮点数。
步骤
首先,导入必需的库 −
import numpy as np
numpy.min_scalar() 方法查找最小数据类型 −
print("Using the min_scalar() method in Numpy\n") print("Result...",np.min_scalar_type(55)) print("Result...",np.min_scalar_type(38.9)) print("Result...",np.min_scalar_type(-78)) print("Result...",np.min_scalar_type(479)) print("Result...",np.min_scalar_type(2e100)) print("Result...",np.min_scalar_type(-45.8)) print("Result...",np.min_scalar_type(6.5e100))
示例
# For scalar, returns the data type with the smallest size and smallest scalar kind which can hold its value. # For non-scalar array, returns the vector’s dtype unmodified. # Floating point values are not demoted to integers, and complex values are not demoted to floats. import numpy as np # The numpy.min_scalar() method finds the minimal data type. # The 1st parameter is the value whose minimal data type is to be found. print("Using the min_scalar() method in Numpy\n") print("Result...",np.min_scalar_type(55)) print("Result...",np.min_scalar_type(38.9)) print("Result...",np.min_scalar_type(-78)) print("Result...",np.min_scalar_type(479)) print("Result...",np.min_scalar_type(2e100)) print("Result...",np.min_scalar_type(-45.8)) print("Result...",np.min_scalar_type(6.5e100))
输出
Using the min_scalar() method in Numpy Result... uint8 Result... float16 Result... int8 Result... uint16 Result... float64 Result... float16 Result... float64
广告