ones(shape, dtype=None, order='C', device=None)

Return a new array of given shape and type, filled with ones. This function currently only supports storing multi-dimensional data in row-major (C-style).

  • shape (int or tuple of int) – The shape of the empty array.

  • dtype (str or numpy.dtype, optional) – An optional value type. Default is depend on your current default dtype. When npx.is_np_default_dtype() returns False, default dtype is float32; When npx.is_np_default_dtype() returns True, default dtype is float64. Note that this behavior is different from NumPy’s ones function where float64 is the default value.

  • order ({'C'}, optional, default: 'C') – How to store multi-dimensional data in memory, currently only row-major (C-style) is supported.

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


out – Array of ones with the given shape, dtype, and device.

Return type



>>> np.ones(5)
array([1., 1., 1., 1., 1.])
>>> np.ones((5,), dtype=int)
array([1, 1, 1, 1, 1], dtype=int64)
>>> np.ones((2, 1))
>>> s = (2,2)
>>> np.ones(s)
array([[1., 1.],
       [1., 1.]])