Return the identity array. The identity array is a square array with ones on the main diagonal. Parameters ---------- n : int Number of rows (and columns) in `n` x `n` output. dtype : data-type, optional Data-type of the output. Defaults to ``float``.
(n, dtype=None, *, like=None)
| 2123 | @set_array_function_like_doc |
| 2124 | @set_module('numpy') |
| 2125 | def identity(n, dtype=None, *, like=None): |
| 2126 | """ |
| 2127 | Return the identity array. |
| 2128 | |
| 2129 | The identity array is a square array with ones on |
| 2130 | the main diagonal. |
| 2131 | |
| 2132 | Parameters |
| 2133 | ---------- |
| 2134 | n : int |
| 2135 | Number of rows (and columns) in `n` x `n` output. |
| 2136 | dtype : data-type, optional |
| 2137 | Data-type of the output. Defaults to ``float``. |
| 2138 | ${ARRAY_FUNCTION_LIKE} |
| 2139 | |
| 2140 | .. versionadded:: 1.20.0 |
| 2141 | |
| 2142 | Returns |
| 2143 | ------- |
| 2144 | out : ndarray |
| 2145 | `n` x `n` array with its main diagonal set to one, |
| 2146 | and all other elements 0. |
| 2147 | |
| 2148 | Examples |
| 2149 | -------- |
| 2150 | >>> np.identity(3) |
| 2151 | array([[1., 0., 0.], |
| 2152 | [0., 1., 0.], |
| 2153 | [0., 0., 1.]]) |
| 2154 | |
| 2155 | """ |
| 2156 | if like is not None: |
| 2157 | return _identity_with_like(like, n, dtype=dtype) |
| 2158 | |
| 2159 | from numpy import eye |
| 2160 | return eye(n, dtype=dtype, like=like) |
| 2161 | |
| 2162 | |
| 2163 | _identity_with_like = array_function_dispatch()(identity) |