Find first threshold crossing in numpy array

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












Given the following artificially generated data:



t_steps = 30

data = np.array([
np.arange(t_steps) * .05,
np.arange(t_steps) * .1,
np.arange(t_steps) * .2,
np.arange(t_steps) * .3

])


I find the time-step the each line of data has passed a threshold. If it does not pass the given threshold, I assign a time-step of -1:



react_tms = 
thresh = 3.5

for dat in data:
whr = np.where(dat > thresh)
if len(whr[0]) == 0:
react_tms.append(-1)
else:
react_tms.append(whr[0][0])


This gives:



[-1, -1, 18, 12]


Is there some way to do this without the for-loop? Even before the for-loop is removed, should I be using something other than np.where to find the threshold crossing?







share|improve this question

























    up vote
    2
    down vote

    favorite












    Given the following artificially generated data:



    t_steps = 30

    data = np.array([
    np.arange(t_steps) * .05,
    np.arange(t_steps) * .1,
    np.arange(t_steps) * .2,
    np.arange(t_steps) * .3

    ])


    I find the time-step the each line of data has passed a threshold. If it does not pass the given threshold, I assign a time-step of -1:



    react_tms = 
    thresh = 3.5

    for dat in data:
    whr = np.where(dat > thresh)
    if len(whr[0]) == 0:
    react_tms.append(-1)
    else:
    react_tms.append(whr[0][0])


    This gives:



    [-1, -1, 18, 12]


    Is there some way to do this without the for-loop? Even before the for-loop is removed, should I be using something other than np.where to find the threshold crossing?







    share|improve this question





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Given the following artificially generated data:



      t_steps = 30

      data = np.array([
      np.arange(t_steps) * .05,
      np.arange(t_steps) * .1,
      np.arange(t_steps) * .2,
      np.arange(t_steps) * .3

      ])


      I find the time-step the each line of data has passed a threshold. If it does not pass the given threshold, I assign a time-step of -1:



      react_tms = 
      thresh = 3.5

      for dat in data:
      whr = np.where(dat > thresh)
      if len(whr[0]) == 0:
      react_tms.append(-1)
      else:
      react_tms.append(whr[0][0])


      This gives:



      [-1, -1, 18, 12]


      Is there some way to do this without the for-loop? Even before the for-loop is removed, should I be using something other than np.where to find the threshold crossing?







      share|improve this question











      Given the following artificially generated data:



      t_steps = 30

      data = np.array([
      np.arange(t_steps) * .05,
      np.arange(t_steps) * .1,
      np.arange(t_steps) * .2,
      np.arange(t_steps) * .3

      ])


      I find the time-step the each line of data has passed a threshold. If it does not pass the given threshold, I assign a time-step of -1:



      react_tms = 
      thresh = 3.5

      for dat in data:
      whr = np.where(dat > thresh)
      if len(whr[0]) == 0:
      react_tms.append(-1)
      else:
      react_tms.append(whr[0][0])


      This gives:



      [-1, -1, 18, 12]


      Is there some way to do this without the for-loop? Even before the for-loop is removed, should I be using something other than np.where to find the threshold crossing?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jul 18 at 14:36









      Seanny123

      758822




      758822




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          In principle you can use numpy.argmax for this. The only problem is that if no value is above the threshold, the maximum is False, so it returns 0 as the index of the maximum. We therefore need to subtract 1 for those cases:



          above_threshold = data > thresh
          react_tms = np.argmax(above_threshold, axis=1)
          react_tms = react_tms - (~np.any(data > thresh, axis=1)).astype(float)
          print(react_tms)
          # array([ -1., -1., 18., 12.])


          Whether or not that is really more readable, I am not sure. It is, however slightly faster than using numpy.where (and probably also faster than a list comprehension): https://stackoverflow.com/q/16243955/4042267



          In the end this does not really matter:



          enter image description here






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            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: "196"
            ;
            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: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fcodereview.stackexchange.com%2fquestions%2f199756%2ffind-first-threshold-crossing-in-numpy-array%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            In principle you can use numpy.argmax for this. The only problem is that if no value is above the threshold, the maximum is False, so it returns 0 as the index of the maximum. We therefore need to subtract 1 for those cases:



            above_threshold = data > thresh
            react_tms = np.argmax(above_threshold, axis=1)
            react_tms = react_tms - (~np.any(data > thresh, axis=1)).astype(float)
            print(react_tms)
            # array([ -1., -1., 18., 12.])


            Whether or not that is really more readable, I am not sure. It is, however slightly faster than using numpy.where (and probably also faster than a list comprehension): https://stackoverflow.com/q/16243955/4042267



            In the end this does not really matter:



            enter image description here






            share|improve this answer



























              up vote
              2
              down vote



              accepted










              In principle you can use numpy.argmax for this. The only problem is that if no value is above the threshold, the maximum is False, so it returns 0 as the index of the maximum. We therefore need to subtract 1 for those cases:



              above_threshold = data > thresh
              react_tms = np.argmax(above_threshold, axis=1)
              react_tms = react_tms - (~np.any(data > thresh, axis=1)).astype(float)
              print(react_tms)
              # array([ -1., -1., 18., 12.])


              Whether or not that is really more readable, I am not sure. It is, however slightly faster than using numpy.where (and probably also faster than a list comprehension): https://stackoverflow.com/q/16243955/4042267



              In the end this does not really matter:



              enter image description here






              share|improve this answer

























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                In principle you can use numpy.argmax for this. The only problem is that if no value is above the threshold, the maximum is False, so it returns 0 as the index of the maximum. We therefore need to subtract 1 for those cases:



                above_threshold = data > thresh
                react_tms = np.argmax(above_threshold, axis=1)
                react_tms = react_tms - (~np.any(data > thresh, axis=1)).astype(float)
                print(react_tms)
                # array([ -1., -1., 18., 12.])


                Whether or not that is really more readable, I am not sure. It is, however slightly faster than using numpy.where (and probably also faster than a list comprehension): https://stackoverflow.com/q/16243955/4042267



                In the end this does not really matter:



                enter image description here






                share|improve this answer















                In principle you can use numpy.argmax for this. The only problem is that if no value is above the threshold, the maximum is False, so it returns 0 as the index of the maximum. We therefore need to subtract 1 for those cases:



                above_threshold = data > thresh
                react_tms = np.argmax(above_threshold, axis=1)
                react_tms = react_tms - (~np.any(data > thresh, axis=1)).astype(float)
                print(react_tms)
                # array([ -1., -1., 18., 12.])


                Whether or not that is really more readable, I am not sure. It is, however slightly faster than using numpy.where (and probably also faster than a list comprehension): https://stackoverflow.com/q/16243955/4042267



                In the end this does not really matter:



                enter image description here







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jul 18 at 14:58


























                answered Jul 18 at 14:53









                Graipher

                20.4k42981




                20.4k42981






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199756%2ffind-first-threshold-crossing-in-numpy-array%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Python Lists

                    Aion

                    JavaScript Array Iteration Methods