Is there any built in function to discard integer and keep only float number in numpy
.
import numpy as np input = np.array([0.0, 0.01, 1.0, 2.0, 2.001, 2.002]) desired_ouput = some_function(input) # Expected ouput # desired_output = np.array([0.01, 2.001, 2.002])
Method #1 : Using loop + Exception Handling In this, we loop through each element and try to convert each string into float value, if it's a success, means it's a float, else it raises a ValueError and we can get desired string.
In NumPy, you filter an array using a boolean index list. A boolean index list is a list of booleans corresponding to indexes in the array. If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array.
Creating such subsets from an existing numpy. ndarray object is based on several criteria. For example, a subset of an ndarray can be created from the elements of a diagonal, or one dimension can be omitted of an array if there is only one element in that dimension.
Mask with whether each element is equal to it as an integer.
arr = np.array([0.0, 0.01, 1.0, 2.0, 2.001, 2.002]) out = arr[arr != arr.astype(int)] #np.array([0.01, 2.001, 2.002])
I don't think so. My approach would be
import numpy as np a = np.array([0.0, 0.01, 1.0, 2.0, 2.001, 2.002]) mask = np.isclose(a, a.astype(int)) print(a[~mask]) #[ 0.01 2.001 2.002]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With