MCPcopy Index your code
hub / github.com/numpy/numpy / ones_like

Function ones_like

numpy/_core/numeric.py:247–314  ·  view source on GitHub ↗

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, *, device=None
)

Source from the content-addressed store, hash-verified

245
246@array_function_dispatch(_ones_like_dispatcher)
247def ones_like(
248 a, dtype=None, order='K', subok=True, shape=None, *, device=None
249):
250 """
251 Return an array of ones with the same shape and type as a given array.
252
253 Parameters
254 ----------
255 a : array_like
256 The shape and data-type of `a` define these same attributes of
257 the returned array.
258 dtype : data-type, optional
259 Overrides the data type of the result.
260 order : {'C', 'F', 'A', or 'K'}, optional
261 Overrides the memory layout of the result. 'C' means C-order,
262 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
263 'C' otherwise. 'K' means match the layout of `a` as closely
264 as possible.
265 subok : bool, optional.
266 If True, then the newly created array will use the sub-class
267 type of `a`, otherwise it will be a base-class array. Defaults
268 to True.
269 shape : int or sequence of ints, optional.
270 Overrides the shape of the result. If order='K' and the number of
271 dimensions is unchanged, will try to keep order, otherwise,
272 order='C' is implied.
273 device : str, optional
274 The device on which to place the created array. Default: None.
275 For Array-API interoperability only, so must be ``"cpu"`` if passed.
276
277 .. versionadded:: 2.0.0
278
279 Returns
280 -------
281 out : ndarray
282 Array of ones with the same shape and type as `a`.
283
284 See Also
285 --------
286 empty_like : Return an empty array with shape and type of input.
287 zeros_like : Return an array of zeros with shape and type of input.
288 full_like : Return a new array with shape of input filled with value.
289 ones : Return a new array setting values to one.
290
291 Examples
292 --------
293 >>> import numpy as np
294 >>> x = np.arange(6)
295 >>> x = x.reshape((2, 3))
296 >>> x
297 array([[0, 1, 2],
298 [3, 4, 5]])
299 >>> np.ones_like(x)
300 array([[1, 1, 1],
301 [1, 1, 1]])
302
303 >>> y = np.arange(3, dtype=np.float64)
304 >>> y

Callers 1

test_onesMethod · 0.90

Calls 1

empty_likeFunction · 0.85

Tested by 1

test_onesMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…