python numpy np.finfo()函数 eps

本文深入解析了Numpy库中的finfo函数,该函数用于获取浮点数类型的机器限制,如最小非零值、最大值、精度等。通过具体示例展示了如何使用finfo函数,以及它在处理浮点数运算时的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用法

finfo函数是根据括号中的类型来获得信息,获得符合这个类型的数型

例1:

import numpy as np
a=np.array([[1],[2],[-1],[0]])
b=np.maximum(a,np.finfo(np.float32).eps)
print(b)	

结果:

[[1.0000000e+00]
 [2.0000000e+00]
 [1.1920929e-07]
 [1.1920929e-07]]

例2:

ious = np.maximum(1.0 * inter_area / union_area, np.finfo(np.float32).eps)

eps是取非负的最小值。当计算的IOU为0或为负(但从代码上来看不太可能为负),使用np.finfo(np.float32).eps来替换

doc

finfo(dtype)

Machine limits for floating point types. 浮点类型的机器限制。

Attributes 属性
----------
eps : floating point number of the appropriate type
    The smallest representable number such that ``1.0 + eps != 1.0``.
    适当类型的浮点数
	最小的可表示数字,例如``1.0 + eps!= 1.0''。
	
epsneg : floating point number of the appropriate type
    The smallest representable number such that ``1.0 - epsneg != 1.0``.
    适当类型的浮点数
     最小的可表示数字,例如``1.0-epsneg!= 1.0''。

iexp : int
    The number of bits in the exponent portion of the floating point
    representation.
    浮点表示形式的指数部分中的位数。
    
machar : MachAr
    The object which calculated these parameters and holds more detailed
    information.
    计算这些参数并保存更多详细信息的对象。
    
machep : int
    The exponent that yields ``eps``.
    产生``eps''的指数。
    
max : floating point number of the appropriate type
    The largest representable number.
    适当类型的浮点数
     可表示的最大数字。

maxexp : int
    The smallest positive power of the base (2) that causes overflow.
    导致溢出的基极(2)的最小正功率。
    
min : floating point number of the appropriate type
    The smallest representable number, typically ``-max``.
    适当类型的浮点数
     最小的可表示数字,通常是“ -max”。

minexp : int
    The most negative power of the base (2) consistent with there being
    no leading 0s in the mantissa.
    基数(2)的最大负幂与尾数中没有前导0一致。
    
negep : int
    The exponent that yields ``epsneg``.
    产生``epsneg''的指数。
    
nexp : int
    The number of bits in the exponent including its sign and bias.
    指数中的位数,包括其符号和偏差。
    
nmant : int
    The number of bits in the mantissa.
    尾数的位数。
    
precision : int
    The approximate number of decimal digits to which this kind of float
    is precise.
    这种浮点数精确到的近似十进制数字。
    
resolution : floating point number of the appropriate type
    The approximate decimal resolution of this type, i.e.
    ``10**-precision``.
    适当类型的浮点数
     此类型的近似十进制分辨率,即``10 **-precision''。

tiny : floating point number of the appropriate type
    The smallest-magnitude usable number.
    适当类型的浮点数
     最小幅度的可用数字。

Parameters
----------
dtype : floating point type, dtype, or instance
    The kind of floating point data type to get information about.
    浮点类型,dtype或实例
     获取有关信息的浮点数据类型的种类。

See Also
--------
numpy.lib.machar.MachAr :
    The implementation of the tests that produce this information.
iinfo :
    The equivalent for integer data types.

Notes
-----
For developers of numpy: do not instantiate this at the module level. The
initial calculation of these parameters is expensive and negatively impacts
import times. These objects are cached, so calling ``finfo()`` repeatedly
inside your functions is not a problem.
对于numpy的开发人员:不要在模块级别实例化它。 
这些参数的初始计算非常昂贵,并且会对导入时间产生负面影响。 
这些对象被缓存,因此在函数内重复调用finfo()不会有问题。

示例

>>> from numpy import *
>>> f = finfo(float)                                           # the numbers given are machine dependent 
															   # 给出的数字取决于机器
															   
>>> f.nmant, f.nexp                                            # nr of bits in the mantissa and in the exponent
															   # 尾数和指数中的位nr
(52, 11)
>>> f.machep                                                   # most negative n so that 1.0 + 2**n != 1.0
-52
>>> f.eps                                                      # floating point precision: 2**machep
array(2.2204460492503131e-16)
>>> f.precision                                                # nr of precise decimal digits: int(-log10(eps))
15
>>> f.resolution                                               # 10**(-precision)
array(1.0000000000000001e-15)
>>> f.negep                                                    # most negative n so that 1.0 - 2**n != 1.0
-53
>>> f.epsneg                                                   # floating point precision: 2**negep
array(1.1102230246251565e-16)
>>> f.minexp                                                   # most negative n so that 2**n gives normal numbers
-1022
>>> f.tiny                                                     # smallest usuable floating point nr: 2**minexp
array(2.2250738585072014e-308)
>>> f.maxexp                                                   # smallest positive n so that 2**n causes overflow
1024
>>> f.min, f.max                                               # the most negative and most positive usuable floating number
(-1.7976931348623157e+308, array(1.7976931348623157e+308))

参考文章1:https://scipy.github.io/old-wiki/pages/Numpy_Example_List_With_Doc.html#finfo.28.29

参考文章2:Numpy : 关于np.finfo函数

参考文章3:Numpy 中文用户指南 3.1 数据类型

在这里插入图片描述
参考文章4:Python第三方库——Numpy

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值