diff --git a/python/mxnet/ndarray/numpy/random.py b/python/mxnet/ndarray/numpy/random.py index 9d1a6f9119ee..e67c766c6bdf 100644 --- a/python/mxnet/ndarray/numpy/random.py +++ b/python/mxnet/ndarray/numpy/random.py @@ -23,7 +23,7 @@ from ..ndarray import NDArray -__all__ = ['randint', 'uniform', 'normal', "choice", "rand", "multinomial"] +__all__ = ['randint', 'uniform', 'normal', "choice", "rand", "multinomial", "shuffle"] def randint(low, high=None, size=None, dtype=None, ctx=None, out=None): @@ -344,3 +344,39 @@ def rand(*size, **kwargs): for s in size: output_shape += (s,) return uniform(0, 1, size=output_shape, **kwargs) + + +def 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 remain the same. + + Parameters + ---------- + x: ndarray + The array or list to be shuffled. + + Returns + ------- + None + + Examples + -------- + >>> arr = np.arange(10) + >>> np.random.shuffle(arr) + >>> arr + array([5., 1., 0., 6., 7., 3., 9., 8., 4., 2.]) # random + + Multi-dimensional arrays are only shuffled along the first axis: + + >>> arr = np.arange(9).reshape((3, 3)) + >>> np.random.shuffle(arr) + >>> arr + array([[6., 7., 8.], # random + [3., 4., 5.], + [0., 1., 2.]]) + """ + _npi.shuffle(x, out=x) diff --git a/python/mxnet/numpy/random.py b/python/mxnet/numpy/random.py index 1cad4a55c466..ebc24de63282 100644 --- a/python/mxnet/numpy/random.py +++ b/python/mxnet/numpy/random.py @@ -20,7 +20,7 @@ from __future__ import absolute_import from ..ndarray import numpy as _mx_nd_np -__all__ = ["randint", "uniform", "normal", "choice", "rand", "multinomial"] +__all__ = ["randint", "uniform", "normal", "choice", "rand", "multinomial", "shuffle"] def randint(low, high=None, size=None, dtype=None, ctx=None, out=None): @@ -321,3 +321,39 @@ def rand(*size, **kwargs): for s in size: output_shape += (s,) return _mx_nd_np.random.uniform(0, 1, size=output_shape, **kwargs) + + +def 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 remain the same. + + Parameters + ---------- + x: ndarray + The array or list to be shuffled. + + Returns + ------- + None + + Examples + -------- + >>> arr = np.arange(10) + >>> np.random.shuffle(arr) + >>> arr + array([5., 1., 0., 6., 7., 3., 9., 8., 4., 2.]) # random + + Multi-dimensional arrays are only shuffled along the first axis: + + >>> arr = np.arange(9).reshape((3, 3)) + >>> np.random.shuffle(arr) + >>> arr + array([[6., 7., 8.], # random + [3., 4., 5.], + [0., 1., 2.]]) + """ + _mx_nd_np.random.shuffle(x) diff --git a/python/mxnet/symbol/numpy/random.py b/python/mxnet/symbol/numpy/random.py index 48bccb64a2b4..94c29f407acc 100644 --- a/python/mxnet/symbol/numpy/random.py +++ b/python/mxnet/symbol/numpy/random.py @@ -21,7 +21,7 @@ from ...context import current_context from . import _internal as _npi -__all__ = ['randint', 'uniform', 'normal', 'rand'] +__all__ = ['randint', 'uniform', 'normal', 'rand', 'shuffle'] def randint(low, high=None, size=None, dtype=None, ctx=None, out=None): @@ -288,3 +288,39 @@ def choice(a, size=None, replace=True, p=None, ctx=None, out=None): return _npi.choice(a=a, size=size, replace=replace, ctx=ctx, weighted=False, out=out) else: return _npi.choice(p, a=a, size=size, replace=replace, ctx=ctx, weighted=True, out=out) + + +def 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 remain the same. + + Parameters + ---------- + x: _Symbol + The array or list to be shuffled. + + Returns + ------- + None + + Examples + -------- + >>> arr = np.arange(10) + >>> np.random.shuffle(arr) + >>> arr + array([5., 1., 0., 6., 7., 3., 9., 8., 4., 2.]) # random + + Multi-dimensional arrays are only shuffled along the first axis: + + >>> arr = np.arange(9).reshape((3, 3)) + >>> np.random.shuffle(arr) + >>> arr + array([[6., 7., 8.], # random + [3., 4., 5.], + [0., 1., 2.]]) + """ + _npi.shuffle(x, out=x) diff --git a/src/operator/random/shuffle_op.cc b/src/operator/random/shuffle_op.cc index 86797c136bab..0f64fbc51449 100644 --- a/src/operator/random/shuffle_op.cc +++ b/src/operator/random/shuffle_op.cc @@ -122,7 +122,7 @@ void ShuffleForwardCPU(const nnvm::NodeAttrs& attrs, NNVM_REGISTER_OP(_shuffle) .add_alias("shuffle") -.add_alias("_np__random_shuffle") +.add_alias("_npi_shuffle") .describe(R"code(Randomly shuffle the elements. This shuffles the array along the first axis.