Return a new array of given shape and type, filled with ones. Parameters ---------- shape : int or sequence of ints Shape of the new array, e.g., ``(2, 3)`` or ``2``. dtype : data-type, optional The desired data-type for the array, e.g., `numpy.int8`. Default i
(shape, dtype=None, order='C', *, device=None, like=None)
| 170 | @finalize_array_function_like |
| 171 | @set_module('numpy') |
| 172 | def ones(shape, dtype=None, order='C', *, device=None, like=None): |
| 173 | """ |
| 174 | Return a new array of given shape and type, filled with ones. |
| 175 | |
| 176 | Parameters |
| 177 | ---------- |
| 178 | shape : int or sequence of ints |
| 179 | Shape of the new array, e.g., ``(2, 3)`` or ``2``. |
| 180 | dtype : data-type, optional |
| 181 | The desired data-type for the array, e.g., `numpy.int8`. Default is |
| 182 | `numpy.float64`. |
| 183 | order : {'C', 'F'}, optional, default: C |
| 184 | Whether to store multi-dimensional data in row-major |
| 185 | (C-style) or column-major (Fortran-style) order in |
| 186 | memory. |
| 187 | device : str, optional |
| 188 | The device on which to place the created array. Default: None. |
| 189 | For Array-API interoperability only, so must be ``"cpu"`` if passed. |
| 190 | |
| 191 | .. versionadded:: 2.0.0 |
| 192 | ${ARRAY_FUNCTION_LIKE} |
| 193 | |
| 194 | .. versionadded:: 1.20.0 |
| 195 | |
| 196 | Returns |
| 197 | ------- |
| 198 | out : ndarray |
| 199 | Array of ones with the given shape, dtype, and order. |
| 200 | |
| 201 | See Also |
| 202 | -------- |
| 203 | ones_like : Return an array of ones with shape and type of input. |
| 204 | empty : Return a new uninitialized array. |
| 205 | zeros : Return a new array setting values to zero. |
| 206 | full : Return a new array of given shape filled with value. |
| 207 | |
| 208 | Examples |
| 209 | -------- |
| 210 | >>> import numpy as np |
| 211 | >>> np.ones(5) |
| 212 | array([1., 1., 1., 1., 1.]) |
| 213 | |
| 214 | >>> np.ones((5,), dtype=np.int_) |
| 215 | array([1, 1, 1, 1, 1]) |
| 216 | |
| 217 | >>> np.ones((2, 1)) |
| 218 | array([[1.], |
| 219 | [1.]]) |
| 220 | |
| 221 | >>> s = (2,2) |
| 222 | >>> np.ones(s) |
| 223 | array([[1., 1.], |
| 224 | [1., 1.]]) |
| 225 | |
| 226 | """ |
| 227 | if like is not None: |
| 228 | return _ones_with_like( |
| 229 | like, shape, dtype=dtype, order=order, device=device |
searching dependent graphs…