[SciPy-user] Regarding what "where" returns
Perry Greenfield
perry at stsci.edu
Wed Apr 12 17:00:02 CDT 2006
We've noticed that in numpy that the where() function behaves
differently than for numarray. In numarray, where() (when used with a
mask or condition array only) always returns a tuple of index arrays,
even for the 1D case whereas numpy returns an index array for the 1D
case and a tuple for higher dimension cases. While the tuple is a
annoyance for users when they want to manipulate the 1D case, the
benefit is that one always knows that where is returning a tuple, and
thus can write code accordingly. The problem with the current numpy
behavior is that it requires special case testing to see which kind
return one has before manipulating if you aren't certain of what the
dimensionality of the argument is going to be.
I'd like to raise the issue of whether or not numpy should change the
behavior. We often deal with both 1D and 2D arrays so it is an
inconvenience for us. How many others deal with this and have an
opinion on which way it should work. There is no difference in using
the result for where() as an index in any case. Tuples are handled
transparently, even for the 1-d case. For example (for numarray):
>>> x = arange(10)
>>> ind = where(x > 6)
>>> print x[ind]
[7 8 9]
>>> print ind
(array([7, 8, 9]),)
So to access the actuall index array, one must index the tuple, e.g.:
ind[0][:2]
Thoughts?
Perry
More information about the SciPy-user
mailing list