larray.eye

larray.eye(rows, columns=None, k=0, title=None, dtype=None, meta=None)[source]

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

Parameters:
rows : int or Axis

Rows of the output.

columns : int or Axis, optional

Columns of the output. If None, defaults to rows.

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.

title : str, optional

Deprecated. See ‘meta’ below.

dtype : data-type, optional

Data-type of the returned array. Defaults to float.

meta : list of pairs or dict or OrderedDict or Metadata, optional

Metadata (title, description, author, creation_date, …) associated with the array. Keys must be strings. Values must be of type string, int, float, date, time or datetime.

Returns:
Array of shape (rows, columns)

An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.

Examples

>>> eye(2, dtype=int)
{0}*\{1}*  0  1
        0  1  0
        1  0  1
>>> sex = Axis('sex=M,F')
>>> eye(sex)
sex\sex    M    F
      M  1.0  0.0
      F  0.0  1.0
>>> age = Axis('age=0..2')
>>> eye(age, sex)
age\sex    M    F
      0  1.0  0.0
      1  0.0  1.0
      2  0.0  0.0
>>> eye(3, k=1)
{0}*\{1}*    0    1    2
        0  0.0  1.0  0.0
        1  0.0  0.0  1.0
        2  0.0  0.0  0.0