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', *, like=None)
| 136 | @set_array_function_like_doc |
| 137 | @set_module('numpy') |
| 138 | def ones(shape, dtype=None, order='C', *, like=None): |
| 139 | """ |
| 140 | Return a new array of given shape and type, filled with ones. |
| 141 | |
| 142 | Parameters |
| 143 | ---------- |
| 144 | shape : int or sequence of ints |
| 145 | Shape of the new array, e.g., ``(2, 3)`` or ``2``. |
| 146 | dtype : data-type, optional |
| 147 | The desired data-type for the array, e.g., `numpy.int8`. Default is |
| 148 | `numpy.float64`. |
| 149 | order : {'C', 'F'}, optional, default: C |
| 150 | Whether to store multi-dimensional data in row-major |
| 151 | (C-style) or column-major (Fortran-style) order in |
| 152 | memory. |
| 153 | ${ARRAY_FUNCTION_LIKE} |
| 154 | |
| 155 | .. versionadded:: 1.20.0 |
| 156 | |
| 157 | Returns |
| 158 | ------- |
| 159 | out : ndarray |
| 160 | Array of ones with the given shape, dtype, and order. |
| 161 | |
| 162 | See Also |
| 163 | -------- |
| 164 | ones_like : Return an array of ones with shape and type of input. |
| 165 | empty : Return a new uninitialized array. |
| 166 | zeros : Return a new array setting values to zero. |
| 167 | full : Return a new array of given shape filled with value. |
| 168 | |
| 169 | |
| 170 | Examples |
| 171 | -------- |
| 172 | >>> np.ones(5) |
| 173 | array([1., 1., 1., 1., 1.]) |
| 174 | |
| 175 | >>> np.ones((5,), dtype=int) |
| 176 | array([1, 1, 1, 1, 1]) |
| 177 | |
| 178 | >>> np.ones((2, 1)) |
| 179 | array([[1.], |
| 180 | [1.]]) |
| 181 | |
| 182 | >>> s = (2,2) |
| 183 | >>> np.ones(s) |
| 184 | array([[1., 1.], |
| 185 | [1., 1.]]) |
| 186 | |
| 187 | """ |
| 188 | if like is not None: |
| 189 | return _ones_with_like(like, shape, dtype=dtype, order=order) |
| 190 | |
| 191 | a = empty(shape, dtype, order) |
| 192 | multiarray.copyto(a, 1, casting='unsafe') |
| 193 | return a |
| 194 | |
| 195 |