Re: [Numpy-discussion] can argmax be used to return row and column indices?

Thanks, Gregorio. I would like it if argmax had a keyword option to return the row,column index automatically (or whatever the dimension of the array). Afterall, argmax already knows the shape of the array. Calling np.unravel_index( np.argmax( A ) ) seems unnecessarily long. But it works well though! I am not sure that such a PR would get much support.... Thanks again, Mark Hi Mark,
You're looking for "np.unravel_index" function.
Cheers, Gregorio
Hello list,
I am trying to find the indices of the maximum value in a 2D array. argmax works fine, but returns the index in the flattened array. That is often not very convenient. Is there a function that returns the index of the row and column? Or can the index of the flattened array easily be converted to the row can column indices (I know, I can write a function for that, but I figure numpy already has one).
Thanks,
Mark

There's already been some dance around this topic, maybe you will find the concept behind it: http://search.gmane.org/?query=unravel_index&group=gmane.comp.python.numeric.general 2013/9/13 Mark Bakker <markbak@gmail.com>:
Thanks, Gregorio. I would like it if argmax had a keyword option to return the row,column index automatically (or whatever the dimension of the array). Afterall, argmax already knows the shape of the array. Calling np.unravel_index( np.argmax( A ) ) seems unnecessarily long. But it works well though! I am not sure that such a PR would get much support.... Thanks again, Mark
Hi Mark,
You're looking for "np.unravel_index" function.
Cheers, Gregorio
Hello list,
I am trying to find the indices of the maximum value in a 2D array. argmax works fine, but returns the index in the flattened array. That is often not very convenient. Is there a function that returns the index of the row and column? Or can the index of the flattened array easily be converted to the row can column indices (I know, I can write a function for that, but I figure numpy already has one).
Thanks,
Mark
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Fri, Sep 13, 2013 at 4:27 AM, Mark Bakker <markbak@gmail.com> wrote:
Thanks, Gregorio. I would like it if argmax had a keyword option to return the row,column index automatically (or whatever the dimension of the array). Afterall, argmax already knows the shape of the array. Calling np.unravel_index( np.argmax( A ) ) seems unnecessarily long. But it works well though! I am not sure that such a PR would get much support.... Thanks again, Mark
What should it do when np.argmax() gets "axis=1" argument? I see confusion occurring with parsing the returned results for arbitrary dimension inputs. -1 on any such PR. +1 on making sure all arg*() functions have unravel_index() very prominent in their documentation (which it does right now for argmax()). Ben Root
participants (3)
-
Benjamin Root
-
Gregorio Bastardo
-
Mark Bakker