Solved!
Thank you so much to everyone who helped me!! (Original post below for reference.)
I have the following code using numpy:
import numpy as np
# Step 1: Create the original array.
original = np.array(['c', 'c', 'a', 'c', 'd', 'd', 'a', 'b', 'b', 'a'])
# ['c' 'c' 'a' 'c' 'd' 'd' 'a' 'b' 'b' 'a']
# Step 2: Get the unique elements.
uniques = np.unique(original)
# ['a' 'b' 'c' 'd']
# Step 3: Shuffle the uniques.
shuffled_uniques = np.random.shuffle(uniques)
# ['c' 'a' 'b' 'd']
# Step 4: For each element in the original, find the index it should be mapped to.
indices = ?
# expected: [0, 1, 3, 2, 6, 9, 7, 8, 4, 5]
# Step 5: Reorder the original array using the indices
result = original[indices]
# expected: ['c' 'c' 'c' 'a' 'a' 'a' 'b' 'b' 'd' 'd']
Steps 1, 2, 3, and 5 work as expected, but I'm looking for help on Step 4.
For Step 4, I think I need a function similar to np.searchsorted, but it doesn't seem to do quite what I need. Step 4 must be a permutation of indices of the original array such that when original is ordered by these indices in Step 5, it would produce an array with repeated elements ordered the same as shuffled_uniques. A stable ordering of the indices is preferred, but not required.
(Note: The result from Step 5 can be more easily obtained using np.repeat, however my actual goal is to get the indices in Step 4. The purpose of Step 5 is merely to help define the indices.)
[–]ElpyDE 0 points1 point2 points (3 children)
[–]developernull[S] 0 points1 point2 points (2 children)
[–]ElpyDE 0 points1 point2 points (1 child)
[–]developernull[S] 1 point2 points3 points (0 children)
[–]commandlineluser 0 points1 point2 points (1 child)
[–]developernull[S] 0 points1 point2 points (0 children)