洗牌时避免缓冲

2024-03-29 13:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在努力为这个问题找到一个好的名字,所以一个好的答案(可能已经存在于:/),所以我不介意任何重命名的想法

我正在使用numpy数组,其中一行表示对象上的数据,通常类似于features = [feature0, feature1]

当使用这个数组时,我会先洗牌,然后再使用它来学习。 在使用它时(洗牌之后),我逐渐需要使用当前行中前一行的特性

为此,我使用了一个缓冲区,结果我使用了一个新的数组,其中的行如[featuresN-i, ..., featuresN-1, featuresN]表示第N行,然后对它进行洗牌

我想知道是否有一种方法可以将索引洗牌,然后从我的2d数组上的something\u函数中得到这样一个3d数组:

original_array.something_function(shuffled_index[N:M]) 
-> [
    [[features of shuffled_index[ N ] - i],
                   ...                    ,
     [features of shuffled_index[ N ]    ]], 
    [[features of shuffled_index[N+1] - i],
                   ...                    ,
     [features of shuffled_index[N+1]    ]],
                  .....                    ,
    [[features of shuffled_index[ M ] - i],
                   ...                    ,
     [features of shuffled_index[ M ]    ]]
   ]

如果有,是否值得调用它来将缓冲数组的大小减少一个因子i

欢迎任何提示


Tags: of数据对象答案numpyindex数组名字
1条回答
网友
1楼 · 发布于 2024-03-29 13:21:50

正如你自己意识到的:不要洗牌数组。洗牌指数

import numpy as np

# create data
nrows = 100
ncols = 4
arr = np.random.rand(nrows, ncols)

# create indices and shuffle
indices = np.arange(nrows)
np.random.shuffle(indices) # in-place operation!

# loop over shuffled indices, do stuff with array
for ii in indices:
    print ii, arr[[ii-1, ii, (ii+1) % nrows]] # (ii+1) % nrows to handle edge case (through wrap around) 

相关问题 更多 >