Filter integers in numpy float array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
18
down vote

favorite
1












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])






share|improve this question
















  • 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















up vote
18
down vote

favorite
1












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])






share|improve this question
















  • 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













up vote
18
down vote

favorite
1









up vote
18
down vote

favorite
1






1





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])






share|improve this question












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])








share|improve this question











share|improve this question




share|improve this question










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













  • 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













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])





share|improve this answer



























    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]





    share|improve this answer
















    • 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











    • 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


















    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.






    share|improve this answer



























      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])





      share|improve this answer





























        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






        share|improve this answer


















        • 10




          The idea of scipy is to never use for for efficiency.
          – Joe Iddon
          Aug 30 at 10:25

















        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.






        share|improve this answer






















        • 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











        • 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










        Your Answer





        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        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






























        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])





        share|improve this answer
























          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])





          share|improve this answer






















            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])





            share|improve this answer












            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])






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 30 at 10:23









            Joe Iddon

            13k31338




            13k31338






















                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]





                share|improve this answer
















                • 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











                • 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















                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]





                share|improve this answer
















                • 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











                • 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













                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]





                share|improve this answer












                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]






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 30 at 10:21









                SpghttCd

                2,0372312




                2,0372312







                • 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











                • 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




                  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








                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











                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.






                share|improve this answer
























                  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.






                  share|improve this answer






















                    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.






                    share|improve this answer












                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 30 at 10:21









                    jpp

                    63.7k173780




                    63.7k173780




















                        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])





                        share|improve this answer


























                          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])





                          share|improve this answer
























                            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])





                            share|improve this answer














                            I've always used np.equal with np.mod:



                            >>> A[~np.equal(np.mod(A, 1), 0)]
                            array([0.01 , 2.001, 2.002])






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 30 at 10:55

























                            answered Aug 30 at 10:40









                            user3483203

                            23.2k62147




                            23.2k62147




















                                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






                                share|improve this answer


















                                • 10




                                  The idea of scipy is to never use for for efficiency.
                                  – Joe Iddon
                                  Aug 30 at 10:25














                                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






                                share|improve this answer


















                                • 10




                                  The idea of scipy is to never use for for efficiency.
                                  – Joe Iddon
                                  Aug 30 at 10:25












                                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






                                share|improve this 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







                                share|improve this answer














                                share|improve this answer



                                share|improve this 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 use for for efficiency.
                                  – Joe Iddon
                                  Aug 30 at 10:25












                                • 10




                                  The idea of scipy is to never use for 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










                                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.






                                share|improve this answer






















                                • 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











                                • 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














                                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.






                                share|improve this answer






















                                • 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











                                • 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












                                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.






                                share|improve this answer














                                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.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                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.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











                                • 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










                                • 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

















                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                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













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                Confectionery