Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter integers in numpy float array

Tags:

python

numpy

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]) 
like image 903
Oli Avatar asked Aug 30 '18 10:08

Oli


People also ask

How do you filter a float in Python?

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.

How we can filter values evaluate values in NumPy arrays?

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.

What is subsetting in NumPy?

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.


2 Answers

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]) 
like image 167
Joe Iddon Avatar answered Sep 19 '22 18:09

Joe Iddon


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] 
like image 34
SpghttCd Avatar answered Sep 21 '22 18:09

SpghttCd