找出 Python 中类数组的最小数据类型


numpy.min_scalar() 方法找出最小数据类型。第一个参数是要找出其最小数据类型的值。对于标量,返回具有最小大小和最小标量类型的可以保存其值的数据类型。对于非标量数组,返回未修改的矢量数据类型。浮点值不会降级为整数,复数不会降级为浮点数。

步骤

首先,导入所需库 −

import numpy as np

numpy.min_scalar() 方法找出最小数据类型。第一个参数是要找出其最小数据类型的值 −

print("Using the min_scalar() method in Numpy\n")
print("Result...",np.min_scalar_type(np.arange(4,dtype='f8')))
print("Result...",np.min_scalar_type(np.arange(38.9, dtype = 'f8')))
print("Result...",np.min_scalar_type(np.array(6.5e100, np.float64)))
print("Result...",np.min_scalar_type(np.array(280, 'i1')))
print("Result...",np.min_scalar_type(np.array(80, 'u1')))
print("Result...",np.min_scalar_type(np.array(300.7, np.float32)))
print("Result...",np.min_scalar_type(np.array(120.6, np.float64)))
print("Result...",np.min_scalar_type(np.array(7.2e100, np.float32)))
print("Result...",np.min_scalar_type(np.array(6.5e100, np.float64)))

示例

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(np.arange(4,dtype='f8')))
print("Result...",np.min_scalar_type(np.arange(38.9, dtype = 'f8')))
print("Result...",np.min_scalar_type(np.array(6.5e100, np.float64)))
print("Result...",np.min_scalar_type(np.array(280, 'i1')))
print("Result...",np.min_scalar_type(np.array(80, 'u1')))
print("Result...",np.min_scalar_type(np.array(300.7, np.float32)))
print("Result...",np.min_scalar_type(np.array(120.6, np.float64)))
print("Result...",np.min_scalar_type(np.array(7.2e100, np.float32)))
print("Result...",np.min_scalar_type(np.array(6.5e100, np.float64)))

输出

Using the min_scalar() method in Numpy

Result... float64
Result... float64
Result... float64
Result... uint8
Result... uint8
Result... float16
Result... float16
Result... float16
Result... float64

更新于: 24-2-2022

135 次浏览

开始你的 职业

通过完成课程获得认证

开始
广告
© . All rights reserved.