我如何控制我洗牌数据集的大小



我有一个数据集X,其中每个数据点(每行)都以特定的顺序排列。为了完全打乱X,我使用如下命令:

shufX = torch.randperm(len(X))
X=X[shufX]

假设我只想稍微洗牌(可能是移动几个数据点的位置),而不是完全洗牌。我想要一个参数p,当p=0时,它不会打乱顺序,当p=1时,它完全打乱顺序,就像代码所说的那样。这样,我就可以调整洗牌的数量,使其更温和或更广泛。

我尝试了,但意识到它可能导致重复的数据点,这不是我想要的。

p = 0.1 
mask = torch.bernoulli(p*torch.ones(len(X))).bool()
shufX = torch.randperm(len(X))
X1=X[shufX]
C = torch.where(mask1, X, X1)

创建一个shuffle函数,只交换有限数量的项。

import numpy as np
from random import randrange, seed
def shuffle( arr_in, weight = 1.0 ):
count = len( arr_in )
n = int( count * weight ) # Set the number of iterations
for ix in range( n ):
ix0 = randrange( count )
ix1 = randrange( count )
arr_in[ ix0 ], arr_in[ ix1 ] = arr_in[ ix1 ], arr_in[ ix0 ]
# Swap the items from the two chosen indices
seed ( 1234 )
arr = np.arange(50)
shuffle( arr, 0.25 )
print( arr )
# [ 7 15 42  3  4 44 28  0  8 29 10 11 12 13 14 22 16 17 18 19 20 21
#   1 23 24 25 26 27 49  9 41 31 32 33 34 35 36  5 38 30 40 39  2 43
#  37 45 46 47 48  6]

即使重量为1.0,有些物品(平均)也不会移动。您可以使用函数的参数来获得所需的行为。

最新更新