Python np.inf 和 float('Inf') 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42315541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between np.inf and float('Inf')
提问by aretor
Is there some difference between NumPy np.inf
and float('Inf')
?
float('Inf') == np.inf
returns True
, so it seems they are interchangeable, thus I was wondering why NumPy has defined its own "inf" constant, and when should I use one constant instead of the other (considering style concerns too)?
NumPynp.inf
和之间有什么区别float('Inf')
吗?
float('Inf') == np.inf
Returns True
,所以它们似乎可以互换,因此我想知道为什么 NumPy 定义了自己的“inf”常量,我什么时候应该使用一个常量而不是另一个(还要考虑样式问题)?
采纳答案by MSeifert
TL, DR: There is no difference and they can be used interchangeably.
TL、DR:没有区别,它们可以互换使用。
Besides having the same value as math.inf
and float('inf')
:
除了与math.inf
和具有相同的值float('inf')
:
>>> import math
>>> import numpy as np
>>> np.inf == float('inf')
True
>>> np.inf == math.inf
True
It also has the same type:
它也有相同的类型:
>>> import numpy as np
>>> type(np.inf)
float
>>> type(np.inf) is type(float('inf'))
float
That's interesting because NumPy also has it's own floating point types:
这很有趣,因为 NumPy 也有它自己的浮点类型:
>>> np.float32(np.inf)
inf
>>> type(np.float32(np.inf))
numpy.float32
>>> np.float32('inf') == np.inf # nevertheless equal
True
So it has the same value and the same type as math.inf
and float('inf')
which means it's interchangeable.
所以它与 和 具有相同的值和相同的类型math.inf
,float('inf')
这意味着它是可以互换的。
Reasons for using np.inf
使用理由 np.inf
It's less to type:
np.inf
(6 chars)math.inf
(8 chars; new in python 3.5)float('inf')
(12 chars)
That means if you already have NumPy imported you can save yourself 6 (or 2) chars per occurrence compared to
float('inf')
(ormath.inf
).Because it's easier to remember.
At least for me, it's far easier to remember
np.inf
than that I need to callfloat
with a string.Also NumPy also defines some additional aliases for infinity:
np.Inf np.inf np.infty np.Infinity np.PINF
It also defines an alias for negative infinity:
np.NINF
Similarly for
nan
:np.nan np.NaN np.NAN
Constants are constants
This point is based on CPython and could be completely different in another Python implementation.
A
float
CPython instance requires 24 Bytes:>>> import sys >>> sys.getsizeof(np.inf) 24
If you can re-use the same instance you might save a lot of memory compared to creating lots of new instances. Of course, this point is mute if you create your own
inf
constant but if you don't then:a = [np.inf for _ in range(1000000)] b = [float('inf') for _ in range(1000000)]
b
would use 24 * 1000000 Bytes (~23 MB) more memory thana
.Accessing a constant is faster than creating the variable.
%timeit np.inf 37.9 ns ± 0.692 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) %timeit float('inf') 232 ns ± 13.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) %timeit [np.inf for _ in range(10000)] 552 μs ± 15.4 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each) %timeit [float('inf') for _ in range(10000)] 2.59 ms ± 78.7 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Of course, you can create your own constant to counter that point. But why bother if NumPy already did that for you.
输入更少:
np.inf
(6 个字符)math.inf
(8 个字符;python 3.5 中的新内容)float('inf')
(12 个字符)
这意味着如果您已经导入了 NumPy,与
float('inf')
(ormath.inf
)相比,每次出现可以节省 6(或 2)个字符。因为更容易记住。
至少对我来说,记住
np.inf
比需要float
用字符串调用要容易得多。此外,NumPy 还为无穷大定义了一些额外的别名:
np.Inf np.inf np.infty np.Infinity np.PINF
它还定义了负无穷大的别名:
np.NINF
同样对于
nan
:np.nan np.NaN np.NAN
常数是常数
这一点基于 CPython,在另一个 Python 实现中可能完全不同。
一个
float
CPython 实例需要 24 个字节:>>> import sys >>> sys.getsizeof(np.inf) 24
如果您可以重用同一个实例,与创建大量新实例相比,您可能会节省大量内存。当然,如果您创建自己的
inf
常量,这一点是静音的,但如果您不这样做:a = [np.inf for _ in range(1000000)] b = [float('inf') for _ in range(1000000)]
b
将使用 24 * 1000000 字节(~23 MB)比a
.访问常量比创建变量快。
%timeit np.inf 37.9 ns ± 0.692 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) %timeit float('inf') 232 ns ± 13.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) %timeit [np.inf for _ in range(10000)] 552 μs ± 15.4 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each) %timeit [float('inf') for _ in range(10000)] 2.59 ms ± 78.7 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
当然,您可以创建自己的常量来反驳这一点。但是如果 NumPy 已经为你做了那又何必呢。