[Numpy-discussion] Indices of min/max element of an array?

Christos Siopis christos.siopis at ulb.ac.be
Sat Dec 10 10:32:12 EST 2005


On Tue, Dec 06, 2005 at 06:37:11PM -0500, Alan G Isaac wrote:

> On Tue, 6 Dec 2005, Christos Siopis apparently wrote: 
>
> > As a side note, i am wondering if there is a semantic 
> > asymmetry in using min() and max() to signify the min/max 
> > element in the entire array while argmin() and argmax() 
> > signify the min/max element along each axis. 
> 
> SciPy arrays function as "expected" in this sense:
> >>> import scipy
> >>> x=scipy.array([[1,2],[3,4]])
> >>> x.max()
> 4
> >>> x.argmax()
> 3
> 
> Note that, as I understand, argmax gives the index from x.flat

Thanks for the note. I do not have SciPy installed to test this, and i am not sure 
which version of SciPy you are using. I believe in the (remote?) past, SciPy was 
using Numeric as a core, but using the latest Numeric available on Gentoo AMD64 i 
obtain different results:

Python 2.4.2 (#1, Nov 19 2005, 12:30:12)
[GCC 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)] on linux2
>>> import Numeric
>>> Numeric.__version__
'23.7'
>>> x=Numeric.array([[1,2],[3,4]])
>>> x.max()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: max
>>> x.argmax()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: argmax

i.e., these methods are not supported for Numeric arrays. The max() function does 
not exist either:

>>> Numeric.max(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'max'

(i think one would have to use MA.Maximum() to find the global array maximum back 
at those days...).  And:

>>> Numeric.argmax(x)
array([1, 1])

i.e., it does not flatten the array.

---

Now, using numarray:

>>> import numarray
>>> numarray.__version__
'1.3.1'
>>> x=numarray.array([[1,2],[3,4]])
>>> x.max()
4
>>> x.argmax()
array([1, 1])

i.e., these methods for the array object now do exist, but the behavior of 
argmax() is not the same as in your SciPy.

My conjencture is that your SciPy uses some "intermediate" version between the old 
Numeric and the current scipy.core which, as i understand from what Travis said, 
supports the above numarray behavior.

 
> Also, the scipy ufuncs max and argmax have the symmetry you 
> seek, if I understand correctly.

With so many versions of things floating around, i think it's hard to tell what 
has what any more... One more reason to look forward to the outcome of Travis' 
work, and hope that things (or at least the API) will stabilize...

Thanks,
Christos




More information about the NumPy-Discussion mailing list