python - Numpy: Check if float array contains whole numbers -
in python, possible check if float
contains integer value using n.is_integer()
, based on qa: how check if float value whole number.
does numpy have similar operation can applied arrays? allow following:
>>> x = np.array([1.0 2.1 3.0 3.9]) >>> mask = np.is_integer(x) >>> mask array([true, false, true, false], dtype=bool)
it possible like
>>> mask = (x == np.floor(x))
or
>>> mask = (x == np.round(x))
but involve calling methods , creating bunch of temp arrays potentially avoided.
does numpy have vectorized function checks fractional parts of floats in way similar python's float.is_integer
?
from can tell, there no such function returns boolean array indicating whether floats have fractional part or not. closest can find np.modf
returns fractional , integer parts, creates 2 float arrays (at least temporarily), might not best memory-wise.
if you're happy working in place, can try like:
>>> np.mod(x, 1, out=x) >>> mask = (x == 0)
this should save memory versus using round or floor (where have keep x
around), of course lose original x
.
the other option ask implemented in numpy, or implement yourself.
Comments
Post a Comment