numpy find the max value in a row and return back to it's column index










3














I have a 180295* 10 numpy array called lda_trans, row means words, and column means 10 topics.



array([[0.01841009, 0.01840699, 0.35798764, ..., 0.38443892, 0.01841072,
0.12870054],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
...,
[0.0416964 , 0.62473603, 0.0416964 , ..., 0.04169395, 0.04169796,
0.04169232],
[0.03772096, 0.03775132, 0.66048403, ..., 0.03771698, 0.03772411,
0.0377139 ],
[0.03754747, 0.03756587, 0.66206395, ..., 0.03754399, 0.037551 ,
0.03753927]])


Now I want to turn back each row's maximum value's column name, I only know how to extract the max value in each row, but I don't know how to get the column name.
I know in pandas can use idxmax. But is there any similar function in Numpy? Thanks!



for i in range(180295):
lda_trans_max.append(np.max(lda_trans[i]))









share|improve this question





















  • Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
    – timgeb
    Nov 12 at 11:29










  • numpy.ndarray.argmax and specify axis.
    – jpp
    Nov 12 at 11:30











  • You can use np.argmax with axis=1 to get the column index of the maximum value in each row.
    – jdehesa
    Nov 12 at 11:30















3














I have a 180295* 10 numpy array called lda_trans, row means words, and column means 10 topics.



array([[0.01841009, 0.01840699, 0.35798764, ..., 0.38443892, 0.01841072,
0.12870054],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
...,
[0.0416964 , 0.62473603, 0.0416964 , ..., 0.04169395, 0.04169796,
0.04169232],
[0.03772096, 0.03775132, 0.66048403, ..., 0.03771698, 0.03772411,
0.0377139 ],
[0.03754747, 0.03756587, 0.66206395, ..., 0.03754399, 0.037551 ,
0.03753927]])


Now I want to turn back each row's maximum value's column name, I only know how to extract the max value in each row, but I don't know how to get the column name.
I know in pandas can use idxmax. But is there any similar function in Numpy? Thanks!



for i in range(180295):
lda_trans_max.append(np.max(lda_trans[i]))









share|improve this question





















  • Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
    – timgeb
    Nov 12 at 11:29










  • numpy.ndarray.argmax and specify axis.
    – jpp
    Nov 12 at 11:30











  • You can use np.argmax with axis=1 to get the column index of the maximum value in each row.
    – jdehesa
    Nov 12 at 11:30













3












3








3







I have a 180295* 10 numpy array called lda_trans, row means words, and column means 10 topics.



array([[0.01841009, 0.01840699, 0.35798764, ..., 0.38443892, 0.01841072,
0.12870054],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
...,
[0.0416964 , 0.62473603, 0.0416964 , ..., 0.04169395, 0.04169796,
0.04169232],
[0.03772096, 0.03775132, 0.66048403, ..., 0.03771698, 0.03772411,
0.0377139 ],
[0.03754747, 0.03756587, 0.66206395, ..., 0.03754399, 0.037551 ,
0.03753927]])


Now I want to turn back each row's maximum value's column name, I only know how to extract the max value in each row, but I don't know how to get the column name.
I know in pandas can use idxmax. But is there any similar function in Numpy? Thanks!



for i in range(180295):
lda_trans_max.append(np.max(lda_trans[i]))









share|improve this question













I have a 180295* 10 numpy array called lda_trans, row means words, and column means 10 topics.



array([[0.01841009, 0.01840699, 0.35798764, ..., 0.38443892, 0.01841072,
0.12870054],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
...,
[0.0416964 , 0.62473603, 0.0416964 , ..., 0.04169395, 0.04169796,
0.04169232],
[0.03772096, 0.03775132, 0.66048403, ..., 0.03771698, 0.03772411,
0.0377139 ],
[0.03754747, 0.03756587, 0.66206395, ..., 0.03754399, 0.037551 ,
0.03753927]])


Now I want to turn back each row's maximum value's column name, I only know how to extract the max value in each row, but I don't know how to get the column name.
I know in pandas can use idxmax. But is there any similar function in Numpy? Thanks!



for i in range(180295):
lda_trans_max.append(np.max(lda_trans[i]))






python numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 11:27









Shin Yu Wu

968




968











  • Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
    – timgeb
    Nov 12 at 11:29










  • numpy.ndarray.argmax and specify axis.
    – jpp
    Nov 12 at 11:30











  • You can use np.argmax with axis=1 to get the column index of the maximum value in each row.
    – jdehesa
    Nov 12 at 11:30
















  • Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
    – timgeb
    Nov 12 at 11:29










  • numpy.ndarray.argmax and specify axis.
    – jpp
    Nov 12 at 11:30











  • You can use np.argmax with axis=1 to get the column index of the maximum value in each row.
    – jdehesa
    Nov 12 at 11:30















Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
– timgeb
Nov 12 at 11:29




Could you make an example array that we can copy-paste? It does not have to be that large to demonstrate the desired output.
– timgeb
Nov 12 at 11:29












numpy.ndarray.argmax and specify axis.
– jpp
Nov 12 at 11:30





numpy.ndarray.argmax and specify axis.
– jpp
Nov 12 at 11:30













You can use np.argmax with axis=1 to get the column index of the maximum value in each row.
– jdehesa
Nov 12 at 11:30




You can use np.argmax with axis=1 to get the column index of the maximum value in each row.
– jdehesa
Nov 12 at 11:30












2 Answers
2






active

oldest

votes


















1














Use np.argmax.



Demo:



>>> a 
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> np.argmax(a, axis=1)
array([4, 4])


You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.



Another demo:



>>> a 
array([[5, 9, 7, 6, 8],
[8, 7, 7, 6, 9]])
>>> np.argmax(a, axis=1)
array([1, 4])





share|improve this answer




























    1














    Numpy quite mathematical sometimes. Try with
    argmax






    share|improve this answer






















      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      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%2f53261196%2fnumpy-find-the-max-value-in-a-row-and-return-back-to-its-column-index%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Use np.argmax.



      Demo:



      >>> a 
      array([[0, 1, 2, 3, 4],
      [5, 6, 7, 8, 9]])
      >>> np.argmax(a, axis=1)
      array([4, 4])


      You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.



      Another demo:



      >>> a 
      array([[5, 9, 7, 6, 8],
      [8, 7, 7, 6, 9]])
      >>> np.argmax(a, axis=1)
      array([1, 4])





      share|improve this answer

























        1














        Use np.argmax.



        Demo:



        >>> a 
        array([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
        >>> np.argmax(a, axis=1)
        array([4, 4])


        You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.



        Another demo:



        >>> a 
        array([[5, 9, 7, 6, 8],
        [8, 7, 7, 6, 9]])
        >>> np.argmax(a, axis=1)
        array([1, 4])





        share|improve this answer























          1












          1








          1






          Use np.argmax.



          Demo:



          >>> a 
          array([[0, 1, 2, 3, 4],
          [5, 6, 7, 8, 9]])
          >>> np.argmax(a, axis=1)
          array([4, 4])


          You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.



          Another demo:



          >>> a 
          array([[5, 9, 7, 6, 8],
          [8, 7, 7, 6, 9]])
          >>> np.argmax(a, axis=1)
          array([1, 4])





          share|improve this answer












          Use np.argmax.



          Demo:



          >>> a 
          array([[0, 1, 2, 3, 4],
          [5, 6, 7, 8, 9]])
          >>> np.argmax(a, axis=1)
          array([4, 4])


          You are getting [4, 4] here because in both rows, the element with the maximum value is at position 4.



          Another demo:



          >>> a 
          array([[5, 9, 7, 6, 8],
          [8, 7, 7, 6, 9]])
          >>> np.argmax(a, axis=1)
          array([1, 4])






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 11:31









          timgeb

          48.8k116390




          48.8k116390























              1














              Numpy quite mathematical sometimes. Try with
              argmax






              share|improve this answer



























                1














                Numpy quite mathematical sometimes. Try with
                argmax






                share|improve this answer

























                  1












                  1








                  1






                  Numpy quite mathematical sometimes. Try with
                  argmax






                  share|improve this answer














                  Numpy quite mathematical sometimes. Try with
                  argmax







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 at 13:54

























                  answered Nov 12 at 11:30









                  user3142459

                  448314




                  448314



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261196%2fnumpy-find-the-max-value-in-a-row-and-return-back-to-its-column-index%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      27

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      Category:Rhetoric