numpy.random.Generator.permuted#
方法
- random.Generator.permuted(x, axis=None, out=None)#
沿指定的 axis 随机打乱 x。
与
shuffle不同,给定轴的每个切片都独立于其他切片进行打乱。- 参数:
- xarray_like, 至少一维
要打乱的数组。
- axisint, optional
沿此轴的 x 的切片将被打乱。每个切片独立于其他切片进行打乱。如果 axis 为 None,则打乱扁平化后的数组。
- outndarray,可选
如果给定,则此处为打乱后数组的目的地。如果 out 为 None,则返回一个打乱后的数组副本。
- 返回:
- ndarray
如果 out 为 None,则返回 x 的一个打乱后的副本。否则,打乱后的数组存储在 out 中,并返回 out。
另请参阅
备注
方法
shuffle和permuted之间的一个重要区别在于它们如何处理axis参数,该参数可以在 Handling the axis parameter 中找到。示例
创建
numpy.random.Generator实例>>> rng = np.random.default_rng()
创建测试数组
>>> x = np.arange(24).reshape(3, 8) >>> x array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23]])
打乱 x 的行
>>> y = rng.permuted(x, axis=1) >>> y array([[ 4, 3, 6, 7, 1, 2, 5, 0], # random [15, 10, 14, 9, 12, 11, 8, 13], [17, 16, 20, 21, 18, 22, 23, 19]])
x 未被修改
>>> x array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23]])
要就地打乱 x 的行,请将 x 作为 out 参数传递
>>> y = rng.permuted(x, axis=1, out=x) >>> x array([[ 3, 0, 4, 7, 1, 6, 2, 5], # random [ 8, 14, 13, 9, 12, 11, 15, 10], [17, 18, 16, 22, 19, 23, 20, 21]])
请注意,当给出
out参数时,返回值是out。>>> y is x True