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 whole number.
Does numpy have a similar operation that can be applied to arrays? Something that would allow the 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 is possible to do something like
>>> mask = (x == np.floor(x))
or
>>> mask = (x == np.round(x))
but they involve calling extra methods and creating a bunch of temp arrays that could be potentially avoided.
Does numpy have a vectorized function that checks for fractional parts of floats in a way similar to Python's float.is_integer
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…