8000 API: Partially revert unique with return_inverse by seberg · Pull Request #26914 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

API: Partially revert unique with return_inverse #26914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2024

Conversation

seberg
Copy link
Member
@seberg seberg commented Jul 11, 2024

There was a good argument that it is not possible to reconstruct the original array with axis=None without first reshaping and changing the result shape helped with it.

However, it was always possible to do it for other axis values by using np.take rather than np.take_along_axis. Changing it for all axis values is unnecessary to achieve reconstruction because np.take(arr, inverse, axis=axis) already performed the job except for axis=None.

Thus, this keeps the change for axis=None, but reverts gh-25553 for numerical axis.


I tried to go through some of the linked PRs, I extremely rough estimate is that at least 2/3 of the time, this would have save a code change. There are a few others that just need flattened (scipy because of a later call, sklearn/cuml because they use a view trick assuming the result is 1-D when it is not). So in many (I am sure not all), of the cases where this doesn't avoid a regression, it is very clean to reshape explicitly.

@charris I am not sure about release notes for 2.0.1, I don't think we incorporate the fragments, but this one might be worth stressing slightly more. I thought I'd add a note to the old release notes (even if we may not rebuild them for a while).

There was a good argument that it is not possible to reconstruct
the original array with `axis=None` without first reshaping and
changing the result shape helped with it.

However, it was always possible to do it for other axis values
by using `np.take` rather than `np.take_along_axis`.  Changing it
for all axis values is unnecessary to achieve reconstruction because
`np.take(arr, inverse, axis=axis)` already performed the job except
for `axis=None`.

Thus, this keeps the change for axis=None, but reverts numpygh-25553
for numerical axis.
< 8000 div class="TimelineItem-body"> @seberg seberg added the 30 - API label Jul 11, 2024
@seberg seberg added this to the 2.0.1 release milestone Jul 11, 2024
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Jul 13, 2024
@charris charris merged commit ca096a3 into numpy:main Jul 13, 2024
67 of 69 checks passed
@charris
Copy link
Member
charris commented Jul 13, 2024

Thanks Sebastian.

@seberg seberg deleted the partial-revert-unique-inverse branch July 13, 2024 15:55
dfm added a commit to dfm/jax that referenced this pull request Jul 15, 2024
In numpy/numpy#26914, the behavior of the
`return_inverse` argument to `np.unique` was partially reverted to the
pre-v2.0 behavior. The PR brings JAX's implementation compatible with
the `numpy>2.0.0` behavior.
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 16, 2024
@charris
Copy link
Member
charris commented Jul 16, 2024

@seberg Can we close data-apis/array-api#820?

@seberg
Copy link
Member Author
seberg commented Jul 17, 2024

We/I wanted to align both functions unique_inverse and unique here. This PR restores the divergence. (Partially but keeps the less problematic (and helpful) change for axis=None.)

IMO, the behavior here is strictly clearer, so I think it would make sense to align with NumPy there. For axis=None it was an improvement so keep it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0