Numpy: Check if float array contains whole numbers

前端 未结 3 707
后悔当初
后悔当初 2020-12-16 00:55

In Python, it is possible to check if a float contains an integer value using n.is_integer(), based on this QA: How to check if a float value is a

3条回答
  •  星月不相逢
    2020-12-16 01:39

    From what I can tell, there is no such function that returns a boolean array indicating whether floats have a fractional part or not. The closest I can find is np.modf which returns the fractional and integer parts, but that creates two float arrays (at least temporarily), so it might not be best memory-wise.

    If you're happy working in place, you can try something like:

    >>> np.mod(x, 1, out=x)
    >>> mask = (x == 0)
    

    This should save memory versus using round or floor (where you have to keep x around), but of course you lose the original x.

    The other option is to ask for it to be implemented in Numpy, or implement it yourself.

提交回复
热议问题