ones_like(a, dtype=None, order='C', device=None, out=None)

Return an array of ones with the same shape and type as a given array.

  • a (ndarray) – The shape and data-type of a define these same attributes of the returned array.

  • dtype (data-type, optional) – Overrides the data type of the result. Temporarily do not support boolean type.

  • order ({'C'}, optional) – Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory. Currently only supports C order.

  • device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().

  • out (ndarray or None, optional) – A location into which the result is stored. If provided, it must have the same shape and dtype as input ndarray. If not provided or None, a freshly-allocated array is returned.


out – Array of ones with the same shape and type as a.

Return type


See also


Return an empty array with shape and type of input.


Return an array of zeros with shape and type of input.


Return a new array with shape of input filled with value.


Return a new array setting values to one.


>>> x = np.arange(6)
>>> x = x.reshape((2, 3))
>>> x
array([[0., 1., 2.],
       [3., 4., 5.]])
>>> np.ones_like(x)
array([[1., 1., 1.],
       [1., 1., 1.]])
>>> np.ones_like(x, int)
array([[1, 1, 1],
       [1, 1, 1]], dtype=int64)
>>> y = np.arange(3, dtype=float)
>>> y
array([0., 1., 2.], dtype=float64)
>>> np.ones_like(y)
array([1., 1., 1.], dtype=float64)