-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
invert_permutation() function #9880
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
Comments
What about supporting the def inverse_permutation(indices, axis=0):
# I don't think axis == None has a useful interpretation unless indices.ndim == 1
axis = np.core.multiarray.normalize_axis_index(axis, indices.ndim)
perm = np.empty(indices.shape, dtype=np.intp)
values_shape = [1] * indices.ndim
values_shape[axis] = -1
values = np.arange(indices.shape[axis], dtype=np.intp).reshape(values_shape)
np.put_along_axis(perm, indices, values, axis=axis)
return perm |
pandas has a similar function named import pandas as pd
pd.DataFrame([[3],[2],[9],[-1],[0],[11],[-3]]).rank()
Out[3]:
0
0 5.0
1 4.0
2 6.0
3 2.0
4 3.0
5 7.0
6 1.0 |
Updated my comment above with the axis-generic implementation |
Would |
Yes, probably -- do we expose this module as public API? |
The "obvious" way to get the rank of elements in a NumPy array is to use
array.argsort().argsort()
:https://stackoverflow.com/questions/5284646/rank-items-in-an-array-using-python-numpy
This is obviously slightly inefficient, due to sorting the array twice. In contrast, the optimal solution simply inverts the order of the array elements for the second
argsort()
, which can be done in a single pass using indexing assignment:I'd like to propose adding an
invert_permutation()
orinverse_permutation()
helper function to take care of this logic, which could be modeled off this helper function in xarray:https://github.com/pydata/xarray/blob/2949558b75a65404a500a237ec54834fd6946d07/xarray/core/nputils.py#L38-L55
As precedent, note that TensorFlow has such a function, too: https://www.tensorflow.org/versions/r1.0/api_docs/python/tf/invert_permutation
The text was updated successfully, but these errors were encountered: