首页 > 解决方案 > Numpy 随机播放数组的子集

问题描述

我正在处理一个热编码数据的向量,并且我对随机游走进行编码,其中一个步骤是随机更改一个 ohencoded 维度,也是随机选取的。

这是代码:

MASK = random.choice(MASKS) #picks one of the ohencoded features (its mask)
SUB = X[MASK]
np.random.shuffle(SUB) #changes the ohencoded dimension by shuffling the 0s and single 1
X[MASK] = SUB #propagates back the new permutation

其中MASKS是所有掩码的列表,每个 ohencoded 特征一个,X是随机步行者的位置。

切入正题,我正在寻找一种更清洁、更优化的方式来进行洗牌,而无需复制、置换和向后复制。有没有办法就地改组 numpy 数组的子数组?

标签: pythonnumpyone-hot-encoding

解决方案


推荐阅读