'How to un-shuffle data?

it may exist a method to coming back from the function shuffle from sklearn.utils? I explain better my problem: I use the shuffle function to randomize the rows of two matrices:

A_s, B_s = shuffle(A, B, random_state = 1)

Next I use both matrices A_s, B_s in some operation and I obtain an other matrix C_s with the same dimension: e.g. C_s = f(A_s, B_s). How to come back to the original order of C as A and B?

I'm thinking something similar to sklearn.preprocessing.MinMaxScaler((0,+1)) and after I come back using sklearn.inverse_transform().



Solution 1:[1]

import numpy as np


def shuffle(x):
    x_s = x.copy()
    x_s.insert(0, x_s.pop())
    return x_s


def unshuffle(x, shuffle):
    shuffled_ind = shuffle(list(range(len(x))))
    rev_shuffled_ind = np.argsort(shuffled_ind)
    x_unshuffled = np.array(x)[rev_shuffled_ind].tolist()
    return x_unshuffled


x = [1, 2, 3, 4, 5, 6, 7]
x_s = shuffle(x)
print(x_s)
x_r = unshuffle(x_s, shuffle)
print(x_r)

A late answer here.

In reality, you have your own shuffle() function.

The idea is to make a sequence shuffled, and use np.argsoft() to get the index for unshuffling.

Hope it helps!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1