How to invert a permutation array in numpy
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Inverting a permutation array means finding the array that undoes the permutation — if P[i] = j, then the inverse Q[j] = i. In NumPy, the most efficient method is np.argsort(P), which returns the indices that would sort P, effectively computing the inverse permutation. An alternative is direct index assignment: Q[P] = np.arange(len(P)). Both approaches run in O(n) or O(n log n) time and are vectorized.
What Is a Permutation Inverse?
Method 1: np.argsort (Simplest)
np.argsort has O(n log n) complexity due to sorting. For permutation arrays (which contain each index exactly once), this gives the correct inverse.
Method 2: Direct Index Assignment (Fastest)
This method is O(n) and avoids the overhead of sorting. It is the fastest approach for large arrays.
Performance Comparison
Verifying the Inverse
An inverse permutation satisfies both P[Q] = identity and Q[P] = identity.
Practical Use Case: Undoing a Shuffle
Inverting a Permutation Matrix
Self-Inverse (Involution) Permutations
Common Pitfalls
- Using
np.argsorton non-permutation arrays:np.argsortalways returns valid indices, but the result is only a true permutation inverse when the input is a valid permutation (contains each integer from 0 to n-1 exactly once). For arrays with duplicates, the result is not an inverse. - Forgetting that NumPy uses 0-based indexing: If your permutation is 1-based (e.g.,
[3, 1, 2]meaning positions 1-3), subtract 1 before inverting and add 1 after:Q = np.argsort(P - 1) + 1. - Modifying the array in-place during inversion:
P[P] = np.arange(len(P))does not work because the left side and right side both depend onP. Use a separate output array:Q = np.empty_like(P); Q[P] = np.arange(len(P)). - Assuming
argsortis always the fastest: For small arrays (< 1000 elements),argsortand direct assignment have similar performance. For millions of elements, the O(n) direct assignment method is significantly faster than O(n log n) argsort. - Not verifying the result: A common validation is to check that
P[Q]equalsnp.arange(len(P)). Skipping this check can hide bugs when the input is not a valid permutation.
Summary
- Use
np.argsort(P)for the simplest one-liner permutation inverse - Use
Q[P] = np.arange(len(P))for the fastest O(n) inverse computation - Verify the inverse with
assert np.array_equal(P[Q], np.arange(len(P))) - Both
P[Q]andQ[P]must equal the identity array for a valid inverse - For 1-based permutations, convert to 0-based before inverting
- The direct assignment method is 2-3x faster than argsort for large arrays
Related reading
- How to invert the x or y axis
- How to iterate a dataset several times using TensorFlow's Dataset API?
- How to iterate over columns of a pandas dataframe
- How to keep index when using pandas merge
- How to iterate over a list in chunks
- How to iterate over a TreeMap?
- How to know if a binary number divides by 3?
- How to limit prediction probability to one class

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.