identity(n, dtype=None, device=None)

Return the identity array.

The identity array is a square array with ones on the main diagonal.

  • n (int) – Number of rows (and columns) in n x n output.

  • dtype (data-type, optional) – Data-type of the output. When npx.is_np_default_dtype() returns False, default dtype is float32; When npx.is_np_default_dtype() returns True, default dtype is float64.

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


outn x n array with its main diagonal set to one, and all other elements 0.

Return type



>>> np.identity(3)
>>> np.identity(3)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])