Return an array of ones with the same shape and type as a given array. Parameters ---------- a : array_like The shape and data-type of `a` define these same attributes of the returned array. dtype : data-type, optional Overrides the data type of the resu
(a, dtype=None, order='K', subok=True, shape=None)
| 202 | |
| 203 | @array_function_dispatch(_ones_like_dispatcher) |
| 204 | def ones_like(a, dtype=None, order='K', subok=True, shape=None): |
| 205 | """ |
| 206 | Return an array of ones with the same shape and type as a given array. |
| 207 | |
| 208 | Parameters |
| 209 | ---------- |
| 210 | a : array_like |
| 211 | The shape and data-type of `a` define these same attributes of |
| 212 | the returned array. |
| 213 | dtype : data-type, optional |
| 214 | Overrides the data type of the result. |
| 215 | |
| 216 | .. versionadded:: 1.6.0 |
| 217 | order : {'C', 'F', 'A', or 'K'}, optional |
| 218 | Overrides the memory layout of the result. 'C' means C-order, |
| 219 | 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous, |
| 220 | 'C' otherwise. 'K' means match the layout of `a` as closely |
| 221 | as possible. |
| 222 | |
| 223 | .. versionadded:: 1.6.0 |
| 224 | subok : bool, optional. |
| 225 | If True, then the newly created array will use the sub-class |
| 226 | type of `a`, otherwise it will be a base-class array. Defaults |
| 227 | to True. |
| 228 | shape : int or sequence of ints, optional. |
| 229 | Overrides the shape of the result. If order='K' and the number of |
| 230 | dimensions is unchanged, will try to keep order, otherwise, |
| 231 | order='C' is implied. |
| 232 | |
| 233 | .. versionadded:: 1.17.0 |
| 234 | |
| 235 | Returns |
| 236 | ------- |
| 237 | out : ndarray |
| 238 | Array of ones with the same shape and type as `a`. |
| 239 | |
| 240 | See Also |
| 241 | -------- |
| 242 | empty_like : Return an empty array with shape and type of input. |
| 243 | zeros_like : Return an array of zeros with shape and type of input. |
| 244 | full_like : Return a new array with shape of input filled with value. |
| 245 | ones : Return a new array setting values to one. |
| 246 | |
| 247 | Examples |
| 248 | -------- |
| 249 | >>> x = np.arange(6) |
| 250 | >>> x = x.reshape((2, 3)) |
| 251 | >>> x |
| 252 | array([[0, 1, 2], |
| 253 | [3, 4, 5]]) |
| 254 | >>> np.ones_like(x) |
| 255 | array([[1, 1, 1], |
| 256 | [1, 1, 1]]) |
| 257 | |
| 258 | >>> y = np.arange(3, dtype=float) |
| 259 | >>> y |
| 260 | array([0., 1., 2.]) |
| 261 | >>> np.ones_like(y) |