#๐ Array shuffling mapping
27 messages ยท Page 1 of 1 (latest)
@small siren
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
For Example:
arr1 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
arr2 = np.array([[4, 5, 6],
[7, 8, 9],
[1, 2, 3]])
I want to reset arr2 back to arr1 based on the indices on arr1
import random
a = [1, 2, 3,4 , 5]
b = somehow_shuffle(a)
a[:] = b
you can also shuffle "in place" with random.shuffle
ah, those are numpy arrays. My idea might work, or it might not; I don't use numpy
im not talking about how to shuffle, i'm talking about how to return the suffled array back to the original array
It would be easier if you shuffled the indices and not the arrays themselves
see above under a[:] = b
might not work, but it shouldn't take long to try it
E.g. you had this instead
arr = np.array(...)
indices = np.random.shuffle(np.arange(arr.shape[0]))
shuffled_arr = arr[indices]
im trying to convert arr2 back to arr1
Your snippet doesn't make much sense right now, because instead of somehow "converting" arr2 back to arr1, you could just set arr2 = arr1.copy() and it would achieve the same thing
arr2 is a shuffled version based on arr1 by shuffling the rows, what I want is a general method to resort the shuffled rows from arr2 back to arr1
based on the row indices
What I mean is, the end result of "resorting" arr2 is just arr1; Thus just copying would achieve the same thing
i want to find the index mapping to reshuffle it back
So do you mean that in addition to
arr1 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
arr2 = np.array([[4, 5, 6],
[7, 8, 9],
[1, 2, 3]])
```You also have this
```py
shuffled_indices = [2, 1, 0]
```And you want to use `shuffled_indices` to transform `arr2` back?
No, shuffled_indices is how you get from arr1 to arr2
So what you actually mean is you want to calculate [1, 2, 0] from an original array arr1 and a shuffled array arr2
One problem is that there might be multiple solutions (think duplicate elements)
my problem there wont be duplicates
I think this answers your question
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.