[Numpy-discussion] nonzero() behaviour has changed

Bill Baxter wbaxter at gmail.com
Thu May 18 02:37:01 EDT 2006


Aren't there quite a few functions that return indices like nonzero() does?
Should they all have arg*** versions that just transpose the output of the
non arg*** version?

Maybe the answer is yes, but to me if all it takes to get what you needs is
transpose() then I'm not sure how that fails to be obvious.  You have [[x y
z] [p d q]] and you want ([x p][y d][z q]).  Looks like a job for
transpose!

Maybe a note in the docstring pointing out "the obvious" is what is really
warranted.  As in "hey, look, you can transpose the output of this function
if you'd like the indices the other way, but the way it is it's more useful
for indexing, as in a[a.nonzero()]."

Sure would be nice if all you had to type was a.nonzero().T, though... ;-P

--bill

On 5/18/06, Ed Schofield <schofield at ftw.at> wrote:
>
> Alan G Isaac wrote:
> > On Tue, 16 May 2006, Alan G Isaac apparently wrote:
> >
> >> 2. How are people using this?  I trust that the numarray
> >> behavior was well considered, but I would have expected
> >> coordinates to be grouped rather than spread across the
> >> arrays in the tuple.
> >>
> >
> > OK, just to satisfy my curiosity:
> > does the silence mean that nobody is using
> > 'nonzero' in a fashion that leads them to
> > prefer the current behavior to the "obvious"
> > alternative of grouping the coordinates?
> > Is the current behavior just an inherited
> > convention, or is it useful in specific applications?
> >
>
> I also think a function that groups coordinates would be useful.  Here's
> a prototype:
>
> def argnonzero(a):
>     return transpose(a.nonzero())
>
> This has the same effect as:
>
> def argnonzero(a):
>     nz = a.nonzero()
>     return array([[nz[i][j] for i in xrange(a.ndim)] for j in
> xrange(len(nz[0]))])
>
> The output is always a 2d array, so
>
> >>> a
> array([[ 0,  1,  2,  3],
>        [ 4,  0,  6,  7],
>        [ 8,  9,  0, 11]])
>
> >>> argnonzero(a)
> array([[0, 1],
>        [0, 2],
>        [0, 3],
>        [1, 0],
>        [1, 2],
>        [1, 3],
>        [2, 0],
>        [2, 1],
>        [2, 3]])
>
> >>> b = a[0]
> >>> argnonzero(b)
> array([[1],
>        [2],
>        [3]])
>
> >>> c = array([a, a-1, a-2])
> >>> argnonzero(c)
> array([[0, 0, 1],
>        [0, 0, 2],
>        ...
>
> It looks a little clumsy for 1d arrays, but I'd argue that, if NumPy
> were to offer a function like this, it should always return a 2d array
> whose rows are the coordinates for consistency, rather than returning
> some squeezed version for indices into 1d arrays.
>
> I'd support the addition of such a function to NumPy.  Although it's
> tiny, it's not obvious, and it might be useful.
>
> -- Ed
>
>
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job
> easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>



-- 
William V. Baxter III
OLM Digital
Kono Dens Building Rm 302
1-8-8 Wakabayashi Setagaya-ku
Tokyo, Japan  154-0023
+81 (3) 3422-3380
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060518/6a235672/attachment.html>


More information about the NumPy-Discussion mailing list