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)
| 2177 | @finalize_array_function_like |
| 2178 | @set_module('numpy') |
| 2179 | def identity(n, dtype=None, *, like=None): |
| 2180 | """ |
| 2181 | Return the identity array. |
| 2182 | |
| 2183 | The identity array is a square array with ones on |
| 2184 | the main diagonal. |
| 2185 | |
| 2186 | Parameters |
| 2187 | ---------- |
| 2188 | n : int |
| 2189 | Number of rows (and columns) in `n` x `n` output. |
| 2190 | dtype : data-type, optional |
| 2191 | Data-type of the output. Defaults to ``float``. |
| 2192 | ${ARRAY_FUNCTION_LIKE} |
| 2193 | |
| 2194 | .. versionadded:: 1.20.0 |
| 2195 | |
| 2196 | Returns |
| 2197 | ------- |
| 2198 | out : ndarray |
| 2199 | `n` x `n` array with its main diagonal set to one, |
| 2200 | and all other elements 0. |
| 2201 | |
| 2202 | Examples |
| 2203 | -------- |
| 2204 | >>> import numpy as np |
| 2205 | >>> np.identity(3) |
| 2206 | array([[1., 0., 0.], |
| 2207 | [0., 1., 0.], |
| 2208 | [0., 0., 1.]]) |
| 2209 | |
| 2210 | """ |
| 2211 | if like is not None: |
| 2212 | return _identity_with_like(like, n, dtype=dtype) |
| 2213 | |
| 2214 | from numpy import eye |
| 2215 | return eye(n, dtype=dtype, like=like) |
| 2216 | |
| 2217 | |
| 2218 | _identity_with_like = array_function_dispatch()(identity) |
searching dependent graphs…