反向打乱数据

Reverse shuffled data

我正在使用 sklearnshuffle() 方法打乱 m x n x o 矩阵的行并打乱 m x 1 向量:

from sklearn.utils import shuffle
import numpy as np
X = np.random.rand(10,4,3)
y = np.random.rand(10)
X, y = shuffle(X, y, random_state=1)

有没有办法取消数据洗牌,即反转洗牌?我无法同时存储已打乱和未打乱的数据,因为 X 非常大(在上面的示例中它很小)。

这里有一种方法可以让你打乱顺序然后不打乱它们:

import random


def getperm(l):
    seed = sum(sum(a) for a in l)
    random.seed(seed)
    perm = list(range(len(l)))
    random.shuffle(perm)
    random.seed()  # optional, in order to not impact other code based on random
    return perm


def shuffle(l):
    perm = getperm(l)
    l[:] = [l[j] for j in perm]


def unshuffle(l):
    perm = getperm(l)
    res = [None] * len(l)
    for i, j in enumerate(perm):
        res[j] = l[i]
    l[:] = res

函数调用示例:

l=[(1,2),(3,4),(5,6),(7,8),(9,10)]
print(l)
shuffle(l)
print(l)  # shuffled
unshuffle(l)
print(l)  # the original

输出:

[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

[(5, 6), (7, 8), (9, 10), (3, 4), (1, 2)]

[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]