mindspore.numpy.eye

mindspore.numpy.eye(N, M=None, k=0, dtype=mstype.float32)[源代码]

Returns a 2-D tensor with ones on the diagonal and zeros elsewhere.

参数
  • N (int) – Number of rows in the output, must be larger than 0.

  • M (int, optional) – Number of columns in the output. If is None, default: N , if defined, must be larger than 0. Default: None.

  • k (int, optional) – Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. Default: 0 .

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype. Default: mstype.float32 .

返回

A tensor of shape (N, M). A tensor where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.

异常

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

样例

>>> import mindspore.numpy as np
>>> print(np.eye(2, 2))
[[1. 0.]
[0. 1.]]