Hi!
I was looking for a method to find the indices of the smallest element of an 3-dimensional array a. Therefore i used
a.argmax()
The problem was, that argmax gives me a flat index. My question is, if there is a build-in function to convert the flat index back to a multidimensional one. I know how to write such a procedure but was curious if one exists in numpy.
Thanks for response and the really impressive software package.
Tobi
Tobias Knopp wrote:
Hi!
I was looking for a method to find the indices of the smallest element of an 3-dimensional array a. Therefore i used
a.argmax()
The problem was, that argmax gives me a flat index. My question is, if there is a build-in function to convert the flat index back to a multidimensional one. I know how to write such a procedure but was curious if one exists in numpy.
See
numpy.unravel_index
-Travis
On Thu, May 31, 2007 at 11:05:52AM -0600, Travis Oliphant wrote:
Tobias Knopp wrote:
Hi!
I was looking for a method to find the indices of the smallest element of an 3-dimensional array a. Therefore i used
a.argmax()
The problem was, that argmax gives me a flat index. My question is, if there is a build-in function to convert the flat index back to a multidimensional one. I know how to write such a procedure but was curious if one exists in numpy.
See
numpy.unravel_index
When I first learned that the value returned by the argmax() function consists of a ravel()ed index (when axis is not specified), I was told that it makes perfect sense in that it is consistent with the behavior of max() and other functions that assume a ravel()ed behavior when axis isn't specified.
While I have to agree that it does make sense (some note of this behavior should probably be added to the docstrings ... ), I also feel that is it not intuitive, and will almost universally lead to confusion (initially) for those who use it.
One could even argue that a ravel()ed index is not correct, in that it cannot [immediately] be used as an argument that references the indicated element.
Whadaya think?
Glen
Glen W. Mabey wrote:
On Thu, May 31, 2007 at 11:05:52AM -0600, Travis Oliphant wrote:
Tobias Knopp wrote:
Hi!
I was looking for a method to find the indices of the smallest element of an 3-dimensional array a. Therefore i used
a.argmax()
The problem was, that argmax gives me a flat index. My question is, if there is a build-in function to convert the flat index back to a multidimensional one. I know how to write such a procedure but was curious if one exists in numpy.
See
numpy.unravel_index
When I first learned that the value returned by the argmax() function consists of a ravel()ed index (when axis is not specified), I was told that it makes perfect sense in that it is consistent with the behavior of max() and other functions that assume a ravel()ed behavior when axis isn't specified.
While I have to agree that it does make sense (some note of this behavior should probably be added to the docstrings ... ), I also feel that is it not intuitive, and will almost universally lead to confusion (initially) for those who use it.
One could even argue that a ravel()ed index is not correct, in that it cannot [immediately] be used as an argument that references the indicated element.
It can be used as an argument using
ind = x.argmax() x.flat[ind]
-Travis
Hi Tobias
On Thu, Jan 08, 2004 at 08:35:07PM +0100, Tobias Knopp wrote: ^^^^^^^^^^^^
Sorry I'm only answering now, but your mail took 3.5 years to arrive ;)
I was looking for a method to find the indices of the smallest element of an 3-dimensional array a. Therefore i used
a.argmax()
The problem was, that argmax gives me a flat index. My question is, if there is a build-in function to convert the flat index back to a multidimensional one. I know how to write such a procedure but was curious if one exists in numpy.
numpy.unravel_index:
""" Convert a flat index into an index tuple for an array of given shape.
e.g. for a 2x2 array, unravel_index(2,(2,2)) returns (1,0).
Example usage: p = x.argmax() idx = unravel_index(p,x.shape) x[idx] == x.max()
Note: x.flat[p] == x.max()
Thus, it may be easier to use flattened indexing than to re-map the index to a tuple. """
Cheers StÃ©fan