Numpy 随机序列 shuffle & permutation
1. numpy.random.shuffle(x)
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
注意:
- 无返回值,改变原有 array
- 对于多维 array,只 shuffle 第一维
2. numpy.random.permutation(x)
Randomly permute a sequence, or return a permuted range. If x is a multi-dimensional array, it is only shuffled along its first index. Parameters: x : int or array_like If x is an integer, randomly permute np.arange(x). If x is an array, make a copy and shuffle the elements randomly. Returns: out : ndarray Permuted sequence or array range.
注意:
- 跟 shuffle 的区别
- 有返回值
- 且不改变原来的 array,会生成一个新的 array
- x 为整型时,先将 x 变成 array: np.arange(x),再打乱顺序