如何有效地将Numpy数组大块改组 [英] How to efficiently shuffle numpy array in chunks

查看:57
本文介绍了如何有效地将Numpy数组大块改组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy数组,看起来像下面的[-1,0,1,0,1,2,1,2,3,...,n-1,n,n + 1,n,n + 1,n + 2 ..] 我想将数组以3为单位随机播放,是否有一种有效的方法可以在numpy中进行处理?

I have a numpy array that looks like the following [-1,0,1,0,1,2,1,2,3,...,n-1,n,n+1,n,n+1,n+2..] I would like to shuffle the array in chunks of 3, is there an efficient way to do it in numpy?

我知道您可以使用下面的shuffle方法对numpy数组进行重组,但这给了我一个完全重组的数组.有没有一种方法可以将其在numpy中以大块的形式随机播放?

I know you can shuffle a numpy array using the following shuffle method, but this gives me a fully shuffled array. Is there a way to shuffle it in chunks in numpy?

import numpy.random as rng

ind = numpy.arange(100)
rng = numpy.random.RandomState(123)
rng.shuffle(ind)

推荐答案

整形为3列. shuffle 文档说,它只是洗牌了第一维:

Reshape into 3 columns. shuffle doc says it just shuffles the 1st dimension:

ind=np.arange(99)  # multple of 3
ind=ind.reshape(-1,3)
rng.shuffle(ind)
ind.flatten()

这篇关于如何有效地将Numpy数组大块改组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆