Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.ones_like

Creates an array filled with ones, using a supplied array-like object to determine the default shape and data type. Optional arguments can change the dtype, memory order, subclass handling, shape, or device.

How it works

Pass an array-like reference to produce a new array filled with ones. By default, the result follows the reference's shape, data type, and suitable memory layout; optional arguments can override the type, layout, subclass behavior, shape, or device.

Syntax

numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)

Parameters

NameTypeDescription
aarray_likeReference array-like object whose shape and data type provide the defaults for the result.
dtypedata-type, optionalReplacement data type for the result.
order{'C', 'F', 'A', or 'K'}, optionalRequested memory layout: 'C', 'F', 'A', or 'K'.
subokbool, optionalWhether to preserve the input array subclass.
shapeint or tuple of ints, optionalReplacement output shape.
devicestr, optionalDevice on which to place the result; the documentation lists this as an Array-API interoperability option.

Returns

out — ndarray: An array of ones with the same shape and type as a, unless overridden by the optional arguments.

Key points

  • The function returns a newly allocated array whose default shape and data type match the supplied reference.
  • The reference can be any array-like object; its shape and data type determine the corresponding defaults.
  • Use dtype when the output must use a type different from the reference.
  • Use order to request a particular memory arrangement, such as C-style or Fortran-style layout.
  • The default subok=True allows an input array subclass to influence the output class.

Common mistakes

  • Unless dtype is supplied, the output keeps the input's data type, so integer inputs produce integer ones rather than floating-point values.
  • With the default subok=True, array subclasses can be preserved; use subok=False when a base ndarray is desired.

Example: Create a matching array of ones

import numpy as np

x = np.arange(6, dtype=np.float64).reshape(2, 3)
y = np.ones_like(x)
print(y)
print(y.dtype)

Creates a 2×3 floating-point array filled with ones, preserving the input shape and data type.

When to use it

Shape- and dtype-compatible initialization

Allocate an array matching an existing tensor or ndarray before accumulation or masking.

Construct predictable test data

Build test fixtures or expected-value arrays without manually repeating dimensions.

Customized array allocation

Create a same-layout working buffer while optionally selecting a different dtype or shape.

Interview questions

QWhat does numpy.ones_like do?

It creates and returns an array containing ones. The reference argument supplies the default shape and data type, while dtype, order, subok, shape, and device can customize the result.

Follow-up: How would you guarantee floating-point output from an integer reference? Pass dtype=float or another floating-point dtype explicitly.

QHow is the output dtype selected?

The default dtype is inherited from the reference array, but the dtype argument overrides it. Thus, an integer input normally yields integer ones, whereas dtype=np.float32 yields single-precision ones.

Follow-up: What happens when dtype and shape are both specified? The result uses the requested dtype and requested shape independently of the reference's corresponding attributes.

QWhat is the purpose of order?

order controls the output memory layout using C, F, A, or K. The default K attempts to preserve the input's layout characteristics, subject to the implementation's layout rules.

Follow-up: When would shape be useful? It allows a new shape to be requested while still using the reference for other defaults such as dtype.

  • numpy.empty_like
  • numpy.zeros_like
  • numpy.full_like

Source: numpy.ones_like. Checked 2026-10-15.