簡體   English   中英

在 Python 中單獨測試正無窮大或負無窮大

[英]Testing for positive infinity, or negative infinity, individually in Python

math.isinf()測試集中在一起的正無窮大或負無窮大。 明確測試它們的pythonic方法是什么?

測試正無窮大的方法:

  1. x == float('+inf')
  2. math.isinf(x) and x > 0

測試負無窮大的方法:

  1. x == float('-inf')
  2. math.isinf(x) and x < 0

拆卸方式一:

>>> def ispinf1(x): return x == float("inf")
...
>>> dis.dis(ispinf1)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_GLOBAL              0 (float)
              6 LOAD_CONST               1 ('inf')
              9 CALL_FUNCTION            1
             12 COMPARE_OP               2 (==)
             15 RETURN_VALUE

拆卸方式二:

>>> def ispinf2(x): return isinf(x) and x > 0
...
>>> dis.dis(ispinfs)
  1           0 LOAD_GLOBAL              0 (isinf)
              3 LOAD_FAST                0 (x)
              6 CALL_FUNCTION            1
              9 JUMP_IF_FALSE_OR_POP    21
             12 LOAD_FAST                0 (x)
             15 LOAD_CONST               1 (0)
             18 COMPARE_OP               4 (>)
        >>   21 RETURN_VALUE

除了 x>0 之外,這個答案似乎有利於方式 2。

“pythonic”方式是使用可讀和可維護的方式。

也就是說, x == float("inf")x == float("-inf")對我來說稍微有點可讀性,我更喜歡它們。 math.isinf(x) and x > 0更快,但每次調用僅約40 納秒

因此,除非你檢查了很多數字,否則它不會對運行時間產生太大影響。

還有numpy

>>> import numpy as np
>>> np.isneginf([np.inf, 0, -np.inf])
array([False, False,  True], dtype=bool)
>>> np.isposinf([np.inf, 0, -np.inf])
array([ True, False, False], dtype=bool)

然后有一般的isinf

>>> np.isinf([np.inf, 0, -np.inf])
array([ True, False,  True], dtype=bool)

這里有一些jupyterlab計時測試,看看什么是最快的方式(從最慢到最快排序):

准備:

import math
import numpy as np
n = 100000000
a = list(range(n))
a.extend([np.inf, float('inf'), math.inf])

結果:

%%time
def inf_to_none(x):
    if np.isinf(x):
        return None
    else:
        return x
r = list(map(inf_to_none, a))

>> CPU times: user 1min 30s, sys: 481 ms, total: 1min 31s
Wall time: 1min 31s


%%time
def inf_to_none(x):
    if x == float('inf'):
        return None
    else:
        return x
r = list(map(inf_to_none, a))

>> CPU times: user 19.6 s, sys: 494 ms, total: 20.1 s
Wall time: 20.2 s


%%time
def inf_to_none(x):
    if math.isinf(x):
        return None
    else:
        return x
r = list(map(inf_to_none_math, a))

>> CPU times: user 15 s, sys: 292 ms, total: 15.3 s
Wall time: 15.3 s


%%time
d = {np.inf: None}
l = lambda x: d.get(x,x)
r = list(map(l, a))

>> CPU times: user 11.7 s, sys: 256 ms, total: 12 s
Wall time: 12 s


%%time
def inf_to_none(x):
    if x == np.inf:
        return None
    else:
        return x
r = list(map(inf_to_none, a))

>> CPU times: user 11.2 s, sys: 280 ms, total: 11.5 s
Wall time: 11.5 s


%%time
def inf_to_none(x):
    if x == math.inf:
        return None
    else:
        return x
r = list(map(inf_to_none, a))

>> CPU times: user 11 s, sys: 276 ms, total: 11.3 s
Wall time: 11.3 s

我認為這些結果很有趣,為了使它簡短使用==比較。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM