[Numpy-discussion] np.nonzero - order guarantees?
Sebastian Berg
sebastian at sipsolutions.net
Tue Jul 28 13:52:20 EDT 2015
On Tue Jul 28 18:18:01 2015 GMT+0200, Matthew Brett wrote:
> On Tue, Jul 28, 2015 at 4:45 PM, Sebastian Berg
> <sebastian at sipsolutions.net> wrote:
> > Yes, I think it is guaranteed C order in the results.
> >
> >
> > On Mon Jul 27 14:05:01 2015 GMT+0200, Matthew Brett wrote:
> >> Hi,
> >>
> >> `np.nonzero` for a 2D array `A` returns:
> >>
> >> row_inds, col_inds = np.nonzero(A)
> >>
> >> I notice that `row_inds` appears to be sorted by value, and `col_inds`
> >> appears to be sorted by value, within each row.
> >>
> >> Is this a guarantee of the `np.nonzero` function? If not, does this
> >> function guarantee any property of the returned indices, other than
> >> the correspondence of the row, column entries?
>
> Joscha Reimer just pointed out that this is not guaranteed for scipy
> sparse arrays:
>
> https://github.com/scipy/scipy/pull/4875#discussion_r35528827
>
> >>> A = scipy.sparse.coo_matrix(([2, 1], ([1, 0], [0, 1])))
> >>> A.todense()
> matrix([[0, 1],
> [2, 0]])
> >>> A.nonzero()
> (array([1, 0], dtype=int32), array([0, 1], dtype=int32))
>
> This seems rather dangerous - I mean a convention that is nearly
> always observed. I guess at very least we should say what the
> guarantee is (or isn't) in the nonzero docstring?
>
Yes. We should double check and document it. But if it were to change, I would go for an order keyword anyway.
-Sebastian
> Cheers,
>
> Matthew
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
More information about the NumPy-Discussion
mailing list