Filter integers in numpy float array
Clash Royale CLAN TAG#URR8PPP
up vote
18
down vote
favorite
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])
python numpy
add a comment |Â
up vote
18
down vote
favorite
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])
python numpy
3
All values in that array are floats. "float" doesn't imply anything about the value being non-integer.
– user2357112
Aug 30 at 19:00
add a comment |Â
up vote
18
down vote
favorite
up vote
18
down vote
favorite
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])
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])
python numpy
asked Aug 30 at 10:14
has
503315
503315
3
All values in that array are floats. "float" doesn't imply anything about the value being non-integer.
– user2357112
Aug 30 at 19:00
add a comment |Â
3
All values in that array are floats. "float" doesn't imply anything about the value being non-integer.
– user2357112
Aug 30 at 19:00
3
3
All values in that array are floats. "float" doesn't imply anything about the value being non-integer.
– user2357112
Aug 30 at 19:00
All values in that array are floats. "float" doesn't imply anything about the value being non-integer.
– user2357112
Aug 30 at 19:00
add a comment |Â
6 Answers
6
active
oldest
votes
up vote
13
down vote
accepted
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])
add a comment |Â
up vote
12
down vote
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]
3
As a general rule always useisclose
when comparing floats. I'm not sure it's necessary here, but upvoted for good practice.
– Daniel F
Aug 30 at 10:37
It's not a good practice to blindly follow what is called "good practice".isclose
, depending on the source of these numbers, may actually be actively harmful.
– Ruslan
Aug 31 at 6:03
add a comment |Â
up vote
5
down vote
I know of no in-built function. But you can create one yourself:
import numpy as np
A = np.array([0.0, 0.01, 1.0, 2.0, 2.001, 2.002])
def remove_ints(arr):
return arr[~(arr == arr.astype(int))]
res = remove_ints(A)
array([ 0.01 , 2.001, 2.002])
Aside, you should not use a built-in class such as input
as a variable name.
add a comment |Â
up vote
4
down vote
I've always used np.equal
with np.mod
:
>>> A[~np.equal(np.mod(A, 1), 0)]
array([0.01 , 2.001, 2.002])
add a comment |Â
up vote
4
down vote
If you do not have to much data (short list), maybe do not need numpy
:
>>> i = [0.0, 0.01, 1.0, 2.0, 2.001, 2.002]
>>> a=[j for j in i if not j.is_integer()]
>>> a
['0.01', '2.001', '2.002']
Otherwise see Joe Iddon answer
10
The idea of scipy is to never usefor
for efficiency.
– Joe Iddon
Aug 30 at 10:25
add a comment |Â
up vote
2
down vote
I don't know any builtin for this but you can filter those floats using:
filter(lambda x: int(str(x).split('.')[1]) != 0, input)
The lambda expression here checks if the decimal places are zero which I interpret as the number being an int.
precision is lost by converting to string. e.g2.001
becomes2.0009999999999999
.
– has
Aug 30 at 11:34
What? I just tried replicating this but[str(x) for x in input]
returned:['0.0', '0.01', '1.0', '2.0', '2.001', '2.002']
? But even if i assume a loss of accuracy, it wouldn't change the results since '001' will not come out as 0 after the int cast, just like '00099999'.
– meissner_
Aug 30 at 11:42
It happend in my python. Actually these are indexes of pandas dataframe, so loss of precision mean, i can't get back values by apply.loc
method.
– has
Aug 30 at 11:44
Well, this would be very interesting indeed if you can find a way to replicate the effect...
– meissner_
Aug 30 at 11:50
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
accepted
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])
add a comment |Â
up vote
13
down vote
accepted
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])
add a comment |Â
up vote
13
down vote
accepted
up vote
13
down vote
accepted
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])
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])
answered Aug 30 at 10:23


Joe Iddon
13k31338
13k31338
add a comment |Â
add a comment |Â
up vote
12
down vote
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]
3
As a general rule always useisclose
when comparing floats. I'm not sure it's necessary here, but upvoted for good practice.
– Daniel F
Aug 30 at 10:37
It's not a good practice to blindly follow what is called "good practice".isclose
, depending on the source of these numbers, may actually be actively harmful.
– Ruslan
Aug 31 at 6:03
add a comment |Â
up vote
12
down vote
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]
3
As a general rule always useisclose
when comparing floats. I'm not sure it's necessary here, but upvoted for good practice.
– Daniel F
Aug 30 at 10:37
It's not a good practice to blindly follow what is called "good practice".isclose
, depending on the source of these numbers, may actually be actively harmful.
– Ruslan
Aug 31 at 6:03
add a comment |Â
up vote
12
down vote
up vote
12
down vote
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]
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]
answered Aug 30 at 10:21
SpghttCd
2,0372312
2,0372312
3
As a general rule always useisclose
when comparing floats. I'm not sure it's necessary here, but upvoted for good practice.
– Daniel F
Aug 30 at 10:37
It's not a good practice to blindly follow what is called "good practice".isclose
, depending on the source of these numbers, may actually be actively harmful.
– Ruslan
Aug 31 at 6:03
add a comment |Â
3
As a general rule always useisclose
when comparing floats. I'm not sure it's necessary here, but upvoted for good practice.
– Daniel F
Aug 30 at 10:37
It's not a good practice to blindly follow what is called "good practice".isclose
, depending on the source of these numbers, may actually be actively harmful.
– Ruslan
Aug 31 at 6:03
3
3
As a general rule always use
isclose
when comparing floats. I'm not sure it's necessary here, but upvoted for good practice.– Daniel F
Aug 30 at 10:37
As a general rule always use
isclose
when comparing floats. I'm not sure it's necessary here, but upvoted for good practice.– Daniel F
Aug 30 at 10:37
It's not a good practice to blindly follow what is called "good practice".
isclose
, depending on the source of these numbers, may actually be actively harmful.– Ruslan
Aug 31 at 6:03
It's not a good practice to blindly follow what is called "good practice".
isclose
, depending on the source of these numbers, may actually be actively harmful.– Ruslan
Aug 31 at 6:03
add a comment |Â
up vote
5
down vote
I know of no in-built function. But you can create one yourself:
import numpy as np
A = np.array([0.0, 0.01, 1.0, 2.0, 2.001, 2.002])
def remove_ints(arr):
return arr[~(arr == arr.astype(int))]
res = remove_ints(A)
array([ 0.01 , 2.001, 2.002])
Aside, you should not use a built-in class such as input
as a variable name.
add a comment |Â
up vote
5
down vote
I know of no in-built function. But you can create one yourself:
import numpy as np
A = np.array([0.0, 0.01, 1.0, 2.0, 2.001, 2.002])
def remove_ints(arr):
return arr[~(arr == arr.astype(int))]
res = remove_ints(A)
array([ 0.01 , 2.001, 2.002])
Aside, you should not use a built-in class such as input
as a variable name.
add a comment |Â
up vote
5
down vote
up vote
5
down vote
I know of no in-built function. But you can create one yourself:
import numpy as np
A = np.array([0.0, 0.01, 1.0, 2.0, 2.001, 2.002])
def remove_ints(arr):
return arr[~(arr == arr.astype(int))]
res = remove_ints(A)
array([ 0.01 , 2.001, 2.002])
Aside, you should not use a built-in class such as input
as a variable name.
I know of no in-built function. But you can create one yourself:
import numpy as np
A = np.array([0.0, 0.01, 1.0, 2.0, 2.001, 2.002])
def remove_ints(arr):
return arr[~(arr == arr.astype(int))]
res = remove_ints(A)
array([ 0.01 , 2.001, 2.002])
Aside, you should not use a built-in class such as input
as a variable name.
answered Aug 30 at 10:21


jpp
63.7k173780
63.7k173780
add a comment |Â
add a comment |Â
up vote
4
down vote
I've always used np.equal
with np.mod
:
>>> A[~np.equal(np.mod(A, 1), 0)]
array([0.01 , 2.001, 2.002])
add a comment |Â
up vote
4
down vote
I've always used np.equal
with np.mod
:
>>> A[~np.equal(np.mod(A, 1), 0)]
array([0.01 , 2.001, 2.002])
add a comment |Â
up vote
4
down vote
up vote
4
down vote
I've always used np.equal
with np.mod
:
>>> A[~np.equal(np.mod(A, 1), 0)]
array([0.01 , 2.001, 2.002])
I've always used np.equal
with np.mod
:
>>> A[~np.equal(np.mod(A, 1), 0)]
array([0.01 , 2.001, 2.002])
edited Aug 30 at 10:55
answered Aug 30 at 10:40


user3483203
23.2k62147
23.2k62147
add a comment |Â
add a comment |Â
up vote
4
down vote
If you do not have to much data (short list), maybe do not need numpy
:
>>> i = [0.0, 0.01, 1.0, 2.0, 2.001, 2.002]
>>> a=[j for j in i if not j.is_integer()]
>>> a
['0.01', '2.001', '2.002']
Otherwise see Joe Iddon answer
10
The idea of scipy is to never usefor
for efficiency.
– Joe Iddon
Aug 30 at 10:25
add a comment |Â
up vote
4
down vote
If you do not have to much data (short list), maybe do not need numpy
:
>>> i = [0.0, 0.01, 1.0, 2.0, 2.001, 2.002]
>>> a=[j for j in i if not j.is_integer()]
>>> a
['0.01', '2.001', '2.002']
Otherwise see Joe Iddon answer
10
The idea of scipy is to never usefor
for efficiency.
– Joe Iddon
Aug 30 at 10:25
add a comment |Â
up vote
4
down vote
up vote
4
down vote
If you do not have to much data (short list), maybe do not need numpy
:
>>> i = [0.0, 0.01, 1.0, 2.0, 2.001, 2.002]
>>> a=[j for j in i if not j.is_integer()]
>>> a
['0.01', '2.001', '2.002']
Otherwise see Joe Iddon answer
If you do not have to much data (short list), maybe do not need numpy
:
>>> i = [0.0, 0.01, 1.0, 2.0, 2.001, 2.002]
>>> a=[j for j in i if not j.is_integer()]
>>> a
['0.01', '2.001', '2.002']
Otherwise see Joe Iddon answer
edited Sep 4 at 7:04
answered Aug 30 at 10:23
Dadep
2,24421631
2,24421631
10
The idea of scipy is to never usefor
for efficiency.
– Joe Iddon
Aug 30 at 10:25
add a comment |Â
10
The idea of scipy is to never usefor
for efficiency.
– Joe Iddon
Aug 30 at 10:25
10
10
The idea of scipy is to never use
for
for efficiency.– Joe Iddon
Aug 30 at 10:25
The idea of scipy is to never use
for
for efficiency.– Joe Iddon
Aug 30 at 10:25
add a comment |Â
up vote
2
down vote
I don't know any builtin for this but you can filter those floats using:
filter(lambda x: int(str(x).split('.')[1]) != 0, input)
The lambda expression here checks if the decimal places are zero which I interpret as the number being an int.
precision is lost by converting to string. e.g2.001
becomes2.0009999999999999
.
– has
Aug 30 at 11:34
What? I just tried replicating this but[str(x) for x in input]
returned:['0.0', '0.01', '1.0', '2.0', '2.001', '2.002']
? But even if i assume a loss of accuracy, it wouldn't change the results since '001' will not come out as 0 after the int cast, just like '00099999'.
– meissner_
Aug 30 at 11:42
It happend in my python. Actually these are indexes of pandas dataframe, so loss of precision mean, i can't get back values by apply.loc
method.
– has
Aug 30 at 11:44
Well, this would be very interesting indeed if you can find a way to replicate the effect...
– meissner_
Aug 30 at 11:50
add a comment |Â
up vote
2
down vote
I don't know any builtin for this but you can filter those floats using:
filter(lambda x: int(str(x).split('.')[1]) != 0, input)
The lambda expression here checks if the decimal places are zero which I interpret as the number being an int.
precision is lost by converting to string. e.g2.001
becomes2.0009999999999999
.
– has
Aug 30 at 11:34
What? I just tried replicating this but[str(x) for x in input]
returned:['0.0', '0.01', '1.0', '2.0', '2.001', '2.002']
? But even if i assume a loss of accuracy, it wouldn't change the results since '001' will not come out as 0 after the int cast, just like '00099999'.
– meissner_
Aug 30 at 11:42
It happend in my python. Actually these are indexes of pandas dataframe, so loss of precision mean, i can't get back values by apply.loc
method.
– has
Aug 30 at 11:44
Well, this would be very interesting indeed if you can find a way to replicate the effect...
– meissner_
Aug 30 at 11:50
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I don't know any builtin for this but you can filter those floats using:
filter(lambda x: int(str(x).split('.')[1]) != 0, input)
The lambda expression here checks if the decimal places are zero which I interpret as the number being an int.
I don't know any builtin for this but you can filter those floats using:
filter(lambda x: int(str(x).split('.')[1]) != 0, input)
The lambda expression here checks if the decimal places are zero which I interpret as the number being an int.
edited Aug 30 at 10:29
MarianD
3,76861231
3,76861231
answered Aug 30 at 10:22
meissner_
36816
36816
precision is lost by converting to string. e.g2.001
becomes2.0009999999999999
.
– has
Aug 30 at 11:34
What? I just tried replicating this but[str(x) for x in input]
returned:['0.0', '0.01', '1.0', '2.0', '2.001', '2.002']
? But even if i assume a loss of accuracy, it wouldn't change the results since '001' will not come out as 0 after the int cast, just like '00099999'.
– meissner_
Aug 30 at 11:42
It happend in my python. Actually these are indexes of pandas dataframe, so loss of precision mean, i can't get back values by apply.loc
method.
– has
Aug 30 at 11:44
Well, this would be very interesting indeed if you can find a way to replicate the effect...
– meissner_
Aug 30 at 11:50
add a comment |Â
precision is lost by converting to string. e.g2.001
becomes2.0009999999999999
.
– has
Aug 30 at 11:34
What? I just tried replicating this but[str(x) for x in input]
returned:['0.0', '0.01', '1.0', '2.0', '2.001', '2.002']
? But even if i assume a loss of accuracy, it wouldn't change the results since '001' will not come out as 0 after the int cast, just like '00099999'.
– meissner_
Aug 30 at 11:42
It happend in my python. Actually these are indexes of pandas dataframe, so loss of precision mean, i can't get back values by apply.loc
method.
– has
Aug 30 at 11:44
Well, this would be very interesting indeed if you can find a way to replicate the effect...
– meissner_
Aug 30 at 11:50
precision is lost by converting to string. e.g
2.001
becomes 2.0009999999999999
.– has
Aug 30 at 11:34
precision is lost by converting to string. e.g
2.001
becomes 2.0009999999999999
.– has
Aug 30 at 11:34
What? I just tried replicating this but
[str(x) for x in input]
returned: ['0.0', '0.01', '1.0', '2.0', '2.001', '2.002']
? But even if i assume a loss of accuracy, it wouldn't change the results since '001' will not come out as 0 after the int cast, just like '00099999'.– meissner_
Aug 30 at 11:42
What? I just tried replicating this but
[str(x) for x in input]
returned: ['0.0', '0.01', '1.0', '2.0', '2.001', '2.002']
? But even if i assume a loss of accuracy, it wouldn't change the results since '001' will not come out as 0 after the int cast, just like '00099999'.– meissner_
Aug 30 at 11:42
It happend in my python. Actually these are indexes of pandas dataframe, so loss of precision mean, i can't get back values by apply
.loc
method.– has
Aug 30 at 11:44
It happend in my python. Actually these are indexes of pandas dataframe, so loss of precision mean, i can't get back values by apply
.loc
method.– has
Aug 30 at 11:44
Well, this would be very interesting indeed if you can find a way to replicate the effect...
– meissner_
Aug 30 at 11:50
Well, this would be very interesting indeed if you can find a way to replicate the effect...
– meissner_
Aug 30 at 11:50
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52094533%2ffilter-integers-in-numpy-float-array%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
All values in that array are floats. "float" doesn't imply anything about the value being non-integer.
– user2357112
Aug 30 at 19:00