正文

使用Numpy打乱数组或打乱矩阵行(用numpy做一个10×10二维数组)快来看

文章摘要

这篇文章介绍了NumPy中`shuffle`函数的使用及其作用。`shuffle`函数用于对输入数组(无论是1维还是多维)进行原地打乱顺序的操作。对于1维数组,它会完全重新排列元素;对于多维数组,它仅会在第一个维度上打乱顺序,而子数组的内容保持不变。文章还提供了两个示例,展示了`shuffle`函数在1维和多维数组中的实际效果。此外,文章详细说明了函数的参数和返回值。

 Modify a sequence in-place by shuffling its contents. 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. Parameters ———- x : array_like The array or list to be shuffled. Returns ——- None Examples ——– >>> arr=np.arange(10) >>> np.random.shuffle(arr) >>> arr [1 7 5 2 9 4 3 6 0 8] Multi-dimensional arrays are only shuffled along the first axis: >>> arr=np.arange(9).reshape((3, 3)) >>> np.random.shuffle(arr) >>> arr array([[3, 4, 5], [6, 7, 8], [0, 1, 2]]) “””