究竟是什么“分辨率” numpy float的参数 [英] What exactly is the "resolution" parameter of numpy float
问题描述
请考虑以下脚本:
import numpy as np
a = np.finfo(10.1)
打印a
其他东西打印出来:
$ $ p $ code精度= 15分辨率= 1.0000000000000001e-15
最大= 1.797(...) e + 308
min = -max
分辨率是从精度派生的,但是不幸的是这个定义有点循环precision(int):这种浮点数的近似数目是精确的。 源
据我所知,浮点数只是实数的有限表示,因此在表示中有错误,而精度可能是这种偏差的度量。但实际上,如果我用低于分辨率的数字进行操作,那么这是否意味着结果会是错误的呢?我怎样才能量化两个浮点数的精度?如果分辨率与1e-15一样大,为什么最小允许的数字是1e-308?
提前谢谢!
简短的答案是 dont'confuse numpy.finfo
与 numpy .spacing
。
$ b $ finfo
在 dtype
上操作,而<$
背景信息
第一个,但是,一些一般的解释:
要理解的关键是浮点数与科学记数法类似。就像你把0.000001写成 1.0 x 10 ^ -6
一样,浮点类似于 c x 2 ^ q
。换句话说,它们有两个独立的部分 - 一个系数( c
,又名有效数)和一个指数( q
)。这两个值被存储为整数。
因此,可以表示一个值的密切程度(让我们把它看作离散化的程度)是两个部分的函数,并且取决于幅度的价值。
但是,精确度(如 np.finfo
)基本上是有效数字的数字,如果这个数字是用10进制表示的。如果数值是以相同的10位科学记数法(即 10 ^ -precision
)写成的,则分辨率是系数的分辨率(前面的部分)。换句话说,两者只是系数的一个函数。
Numpy特定的
对于 numpy.finfo
,精确度和分辨率是相互颠倒的。 两人都没有告诉你一个特定的号码被表示得多么紧密。它们纯粹是 dtype
的功能。
相反,如果您担心离散化,请使用 numpy.spacing(your_float)
。这将返回该特定格式中的下一个最大值的差异(例如,对于 float32
而言,它不同于 float64
)
示例
举例:
<在[1]中:import numpy as np
In [2]:np.spacing(10.1)
Out [2]:1.7763568394002505e -15
In [3]:np.spacing(10000000000.1)
Out [3]:1.9073486328125e-06
In [4]:np.spacing (1000000000000.1)
Out [4]:0.0001220703125
In [5]:np.spacing(100000000000000.1)
Out [5]:0.015625
在[6]中:np.spacing(10000000000000000.1)
Out [6]:2.0
但是精度和分辨率并没有改变:
pre $ In [7]:np.finfo(10.1).precision $ b $在[8]中:np.finfo(10000000000000000.1).precision
out [8]:15
在[9] :np.finfo(10.1).resolution
Out [9]:1.0000000000000001e-15
在[10]中:np.finfo(10000000000 000000000.1).resolution
Out [10]:1.0000000000000001e-15
另外请注意,全部这些取决于你正在使用的数据类型:
pre $ In [11]:np.spacing(np.float32( 10.1))
Out [11]:9.5367432e-07
In [12]:np.spacing(np.float32(10000000000000.1))
Out [12]:1048576.0
在[13]中:np.finfo(np.float32).precision
Out [13]:6
In [14]:np.finfo np.float32).resolution
[14]:1e-06
In [15]:np.spacing(np.float128(10.1))
Out [15] :8.6736173798840354721e-19
In [16]:np.spacing(np.float128(10000000000000.1))
Out [16]:9.5367431640625e-07
在[17]中:np.finfo(np.float128).precision
Out [17]:18
在[18]中:np.finfo(np.float128).resolution
Out [18]:1.0000000000000000007e-18
具体问题
现在就您的具体问题:
如果我使用小于分辨率的数字来执行操作,这是否意味着我应该期望结果是错误的?没有,因为精度/分辨率(在
numpy.finfo
条件下)只是系数的一个函数,并没有考虑到指数。非常小的和非常大的数字具有相同的精度,但这不是一个绝对的错误。根据经验,当使用finfo
中的分辨率或精度项时,想一想科学计数法。如果我们使用类似的小数字进行操作,我们不需要太担心。
让我们用6位有效数字(有点类似到
float32
):
1.20000 x 10 ^ -19 + 2.50000 ×10 ^ -20 =>然而,如果我们使用数值大小不一的数字,但精度有限(再次,6位有效数字):
1.20000 x 10 ^ 6 + 2.50000 x 10 ^ -5 => 1.20000
我们将开始很清楚地看到效果。
如何量化两个浮点数的误差,例如加法,给定它们的精度?
使用
np.spacing(result)
。
如果分辨率与1e-15一样大,为什么最小允许的数字是1e-308?
同样,在这种情况下,分辨率不考虑指数,只是前面的部分。 >
希望这有助于澄清事情。所有这一切都有点令人困惑,每个人都会在某个时候被咬伤。试着建立一点直觉,并且知道在你选择的平台上找到什么样的函数来找到它是很好的!
I am seeking some more understanding about the "resolution" parameter of a numpy float (I guess any computer defined float for that matter).
Consider the following script:
import numpy as np a = np.finfo(10.1) print a
I get an output which among other things prints out:
precision=15 resolution= 1.0000000000000001e-15 max= 1.797(...)e+308 min= -max
The numpy documentation specifies: "resolution: (floating point number of the appropriate type) The approximate decimal resolution of this type, i.e., 10**-precision." source
resolution is derived from precision, but unfortunately this definition is somewhat circular "precision (int): The approximate number of decimal digits to which this kind of float is precise." source
I understand that floating point numbers are merely finite representations of real numbers and therefore have error in their representation, and that precision is probably a measure of this deviation. But practically, does it mean that I should expect results to be erroneous if I preform operations using numbers less than the resolution? How can I quantify the error, for say addition, of two floating point numbers given their precision? If the resolution is as "large" as 1e-15, why would the smallest allowable number be on the order of 1e-308?
Thank you in advance!
解决方案The short answer is "dont' confuse
numpy.finfo
withnumpy.spacing
".
finfo
operates on thedtype
, whilespacing
operates on the value.Background Information
First, though, some general explanation:
The key part to understand is that floating point numbers are similar to scientific notation. Just like you'd write 0.000001 as
1.0 x 10^-6
, floats are similar toc x 2^q
. In other words, they have two separate parts - a coefficient (c
, a.k.a. "significand") and an exponent (q
). These two values are stored as integers.Therefore, how closely a value can be represented (let's think of this as the degree of discretization) is a function of both parts, and depends on the magnitude of the value.
However, the "precision" (as referred to by
np.finfo
) is essentially the number of significant digits if the number were written in base-10 scientific notation. The "resolution" is the resolution of the coefficient (part in front) if the value were written in the same base-10 scientific notation (i.e.10^-precision
). In other words, both are only a function of the coefficient.Numpy-specific
For
numpy.finfo
, "precision" and "resolution" are simply the inverse of each other. Neither one tells you how closely a particular number is being represented. They're purely a function of thedtype
.Instead, if you're worried about the absolute degree of discretization, use
numpy.spacing(your_float)
. This will return the difference in the next largest value in that particular format (e.g. it's different for afloat32
than afloat64
).Examples
As an example:
In [1]: import numpy as np In [2]: np.spacing(10.1) Out[2]: 1.7763568394002505e-15 In [3]: np.spacing(10000000000.1) Out[3]: 1.9073486328125e-06 In [4]: np.spacing(1000000000000.1) Out[4]: 0.0001220703125 In [5]: np.spacing(100000000000000.1) Out[5]: 0.015625 In [6]: np.spacing(10000000000000000.1) Out[6]: 2.0
But the precision and resolution don't change:
In [7]: np.finfo(10.1).precision Out[7]: 15 In [8]: np.finfo(10000000000000000.1).precision Out[8]: 15 In [9]: np.finfo(10.1).resolution Out[9]: 1.0000000000000001e-15 In [10]: np.finfo(10000000000000000000.1).resolution Out[10]: 1.0000000000000001e-15
Also note that all of these depend on the data type that you're using:
In [11]: np.spacing(np.float32(10.1)) Out[11]: 9.5367432e-07 In [12]: np.spacing(np.float32(10000000000000.1)) Out[12]: 1048576.0 In [13]: np.finfo(np.float32).precision Out[13]: 6 In [14]: np.finfo(np.float32).resolution Out[14]: 1e-06 In [15]: np.spacing(np.float128(10.1)) Out[15]: 8.6736173798840354721e-19 In [16]: np.spacing(np.float128(10000000000000.1)) Out[16]: 9.5367431640625e-07 In [17]: np.finfo(np.float128).precision Out[17]: 18 In [18]: np.finfo(np.float128).resolution Out[18]: 1.0000000000000000007e-18
Specific Questions
Now on to your specific questions:
But practically, does it mean that I should expect results to be erroneous if I preform operations using numbers less than the resolution?
No, because the precision/resolution (in
numpy.finfo
terms) is only a function of the coefficient, and doesn't take into account the exponent. Very small and very large numbers have the same "precision", but that's not an absolute "error".As a rule of thumb, when using the "resolution" or "precision" terms from
finfo
, think of scientific notation. If we're operating on small numbers with similar magnitudes, we don't need to worry about much.Let's take the decimal math case with 6 significant digits (somewhat similar to a
float32
):1.20000 x 10^-19 + 2.50000 x 10^-20 => 1.45000 x 10^19
However, if we operate on numbers with wildly different magnitudes but limited precision (again, 6 significant digits):
1.20000 x 10^6 + 2.50000 x 10^-5 => 1.20000
We'll start to see the effects quite clearly.
How can I quantify the error, for say addition, of two floating point numbers given their precision?
Use
np.spacing(result)
.If the resolution is as "large" as 1e-15, why would the smallest allowable number be on the order of 1e-308?
Again, the "resolution" in this case doesn't take into account the exponent, just the part in front.
Hopefully that helps clarify things somewhat. All of this is a bit confusing, and everyone gets bitten by it at some point. It's good to try to build up a bit of intuition about it and to know what functions to call to find out exactly in your platform-of-choice!
这篇关于究竟是什么“分辨率” numpy float的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!