Discussion:
[Numpy-discussion] np.nonzero - order guarantees?
Matthew Brett
2015-07-27 12:05:01 UTC
Permalink
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?

Cheers,

Matthew
Sebastian Berg
2015-07-28 15:45:07 UTC
Permalink
Yes, I think it is guaranteed C order in the results.
Post by Matthew Brett
Hi,
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?
Cheers,
Matthew
_______________________________________________
NumPy-Discussion mailing list
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Matthew Brett
2015-07-28 16:18:01 UTC
Permalink
On Tue, Jul 28, 2015 at 4:45 PM, Sebastian Berg
Post by Sebastian Berg
Yes, I think it is guaranteed C order in the results.
Post by Matthew Brett
Hi,
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
Post by Sebastian Berg
Post by Matthew Brett
A = scipy.sparse.coo_matrix(([2, 1], ([1, 0], [0, 1])))
A.todense()
matrix([[0, 1],
[2, 0]])
Post by Sebastian Berg
Post by Matthew Brett
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?

Cheers,

Matthew
Sebastian Berg
2015-07-28 17:52:20 UTC
Permalink
Post by Matthew Brett
On Tue, Jul 28, 2015 at 4:45 PM, Sebastian Berg
Post by Sebastian Berg
Yes, I think it is guaranteed C order in the results.
Post by Matthew Brett
Hi,
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
https://github.com/scipy/scipy/pull/4875#discussion_r35528827
Post by Sebastian Berg
Post by Matthew Brett
A = scipy.sparse.coo_matrix(([2, 1], ([1, 0], [0, 1])))
A.todense()
matrix([[0, 1],
[2, 0]])
Post by Sebastian Berg
Post by Matthew Brett
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
Post by Matthew Brett
Cheers,
Matthew
_______________________________________________
NumPy-Discussion mailing list
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Loading...