Numpy 仅按行 shuffle 多维数组,保持列顺序不变 [英] Numpy shuffle multidimensional array by row only, keep column order unchanged

查看:30
本文介绍了Numpy 仅按行 shuffle 多维数组,保持列顺序不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅在 Python 中按行混洗多维数组(因此不要混洗列).

How can I shuffle a multidimensional array by row only in Python (so do not shuffle the columns).

我正在寻找最有效的解决方案,因为我的矩阵非常庞大.是否也可以在原始数组上高效地执行此操作(以节省内存)?

示例:

import numpy as np
X = np.random.random((6, 2))
print(X)
Y = ???shuffle by row only not colls???
print(Y)

我现在期望的是原始矩阵:

What I expect now is original matrix:

[[ 0.48252164  0.12013048]
 [ 0.77254355  0.74382174]
 [ 0.45174186  0.8782033 ]
 [ 0.75623083  0.71763107]
 [ 0.26809253  0.75144034]
 [ 0.23442518  0.39031414]]

输出随机排列行而不是列,例如:

Output shuffle the rows not cols e.g.:

[[ 0.45174186  0.8782033 ]
 [ 0.48252164  0.12013048]
 [ 0.77254355  0.74382174]
 [ 0.75623083  0.71763107]
 [ 0.23442518  0.39031414]
 [ 0.26809253  0.75144034]]

推荐答案

您可以使用 numpy.random.shuffle().

You can use numpy.random.shuffle().

此函数仅沿 a 的第一个轴打乱数组多维数组.子数组的顺序改变了,但它们的内容保持不变.

This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.

In [2]: import numpy as np                                                                                                                                                                                  

In [3]:                                                                                                                                                                                                     

In [3]: X = np.random.random((6, 2))                                                                                                                                                                        

In [4]: X                                                                                                                                                                                                   
Out[4]: 
array([[0.71935047, 0.25796155],
       [0.4621708 , 0.55140423],
       [0.22605866, 0.61581771],
       [0.47264172, 0.79307633],
       [0.22701656, 0.11927993],
       [0.20117207, 0.2754544 ]])

In [5]: np.random.shuffle(X)                                                                                                                                                                                

In [6]: X                                                                                                                                                                                                   
Out[6]: 
array([[0.71935047, 0.25796155],
       [0.47264172, 0.79307633],
       [0.4621708 , 0.55140423],
       [0.22701656, 0.11927993],
       [0.20117207, 0.2754544 ],
       [0.22605866, 0.61581771]])

对于其他功能,您还可以查看以下功能:

For other functionalities you can also check out the following functions:

随机.Generator.permutation

随机.Generator.permuted

函数 random.Generator.permuted 是在 Numpy 的 1.20.0 Release 中引入的.

The function random.Generator.permuted is introduced in Numpy's 1.20.0 Release.

新函数与 shufflepermutation 的不同之处在于由轴索引的子数组被置换而不是轴被置换对于另一个的每个组合,都被视为一个单独的一维数组索引.例如,现在可以置换行或二维数组的列.

The new function differs from shuffle and permutation in that the subarrays indexed by an axis are permuted rather than the axis being treated as a separate 1-D array for every combination of the other indexes. For example, it is now possible to permute the rows or columns of a 2-D array.

这篇关于Numpy 仅按行 shuffle 多维数组,保持列顺序不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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