🌐
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.zeros_like.html
numpy.zeros_like — NumPy v2.4 Manual
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)[source]# Return an array of zeros with the same shape and type as a given array. Parameters: aarray_like · The shape and data-type of a define these same attributes of the returned array.
NEPs
This page provides an overview of development priorities for NumPy. Specifically, it contains a roadmap with a higher-level overview, as well as NumPy Enhancement Proposals (NEPs)—suggested changes to the library—in various stages of discussion or completion.
Learn
This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference · This set of tutorials and educational materials is being developed in the numpy-tutorials repository, and is not a part of the NumPy source tree. The goal of this repository is to provide ...
User Guide
Fundamentals and usage · Advanced usage and interoperability
API reference
This reference manual details functions, modules, and objects included in NumPy, describing what they are and what they do. For learning how to use NumPy, see the complete documentation · Large parts of this manual originate from Travis E. Oliphant’s book Guide to NumPy (which generously ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-zeros_like-python
numpy.zeros_like() in Python - GeeksforGeeks
March 8, 2024 - Syntax: numpy.zeros_like(array, dtype = None, order = 'K', subok = True)
People also ask

How is zeros_like different from zeros?
zeros_like takes an array template and inherits its shape and dtype by default. zeros takes the requested shape directly.
🌐
askpython.com
askpython.com › python-modules › numpy › numpy-zeros_like
NumPy zeros_like: Create a Zero Array from a Template - AskPython
How do I make zeros_like return floats?
Pass a floating-point dtype, such as dtype=np.float64, to override the input array's data type.
🌐
askpython.com
askpython.com › python-modules › numpy › numpy-zeros_like
NumPy zeros_like: Create a Zero Array from a Template - AskPython
🌐
w3resource
w3resource.com › numpy › array-creation › zeros_like.php
NumPy: numpy.zeros_like() function - w3resource
April 21, 2026 - The numpy.zeros_like() function takes an array_like object as input and returns an array of the same shape, size, and dtype with all elements set to zero.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.zeros_like.html
numpy.zeros_like — NumPy v2.5 Manual
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)[source]# Return an array of zeros with the same shape and type as a given array. Parameters: aarray_like · The shape and data-type of a define these same attributes of the returned array.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.zeros_like.html
numpy.zeros_like — NumPy v2.6.dev0 Manual
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)[source]# Return an array of zeros with the same shape and type as a given array. Parameters: aarray_like · The shape and data-type of a define these same attributes of the returned array.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.zeros_like.html
numpy.zeros_like — NumPy v2.2 Manual
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)[source]# Return an array of zeros with the same shape and type as a given array. Parameters: aarray_like · The shape and data-type of a define these same attributes of the returned array.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.zeros_like.html
numpy.zeros_like — NumPy v2.1 Manual
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)[source]# Return an array of zeros with the same shape and type as a given array. Parameters: aarray_like · The shape and data-type of a define these same attributes of the returned array.
Find elsewhere
🌐
AskPython
askpython.com › python-modules › numpy › numpy-zeros_like
NumPy zeros_like: Create a Zero Array from a Template - AskPython
October 4, 2026 - NumPy’s zeros_like function creates a zero-filled array from an existing array, carrying over its shape and data type unless you override either property.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.zeros_like.html
numpy.zeros_like — NumPy v2.0 Manual
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)[source]# Return an array of zeros with the same shape and type as a given array. Parameters: aarray_like · The shape and data-type of a define these same attributes of the returned array.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.zeros_like.html
numpy.zeros_like — NumPy v2.3 Manual
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None, *, device=None)[source]# Return an array of zeros with the same shape and type as a given array. Parameters: aarray_like · The shape and data-type of a define these same attributes of the returned array.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_zeros_like_function.htm
Numpy zeros_like() Function
The numpy.zeros_like() function can be used to create an array filled with zeros that matches the shape and type of an existing multi-dimensional array.
🌐
Educative
educative.io › answers › what-is-the-numpyzeroslike-function-in-python
What is the numpy.zeros_like() function in Python?
The numpy.zeros_like() function in Python is used to return an array of zeros (0) with the same shape and data type as the array passed to it.
🌐
NumPy
numpy.org › doc › 1.15 › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v1.15 Manual
numpy.ones_like · numpy.zeros_like · numpy.zeros(shape, dtype=float, order='C')¶ · Return a new array of given shape and type, filled with zeros. See also · zeros_like · Return an array of zeros with shape and type of input. empty · Return a new uninitialized array.
🌐
Skytowner
skytowner.com › explore › numpy_zeros_like_method
NumPy | zeros_like method with Examples
Numpy's zeros_like(~) method creates a Numpy array from an existing array, and fills it with all zeroes. This is similar to the other Numpy _like methods such as full_like(~) and full_empty(~).
Top answer
1 of 2
27

Modern OS allocate memory virtually, ie., memory is given to a process only when it is first used. zeros obtains memory from the operating system so that the OS zeroes it when it is first used. zeros_like on the other hand fills the alloced memory with zeros by itself. Both ways require about same amount of work --- it's just that with zeros_like the zeroing is done upfront, whereas zeros ends up doing it on the fly.

Technically, in C the difference is calling calloc vs. malloc+memset.

2 of 2
25

My timings in Ipython are (with a simplier timeit interface):

In [57]: timeit np.zeros_like(x)
1 loops, best of 3: 420 ms per loop

In [58]: timeit np.zeros((12488, 7588, 3), np.uint8)
100000 loops, best of 3: 15.1 µs per loop

When I look at the code with IPython (np.zeros_like??) I see:

res = empty_like(a, dtype=dtype, order=order, subok=subok)
multiarray.copyto(res, 0, casting='unsafe')

while np.zeros is a blackbox - pure compiled code.

Timings for empty are:

In [63]: timeit np.empty_like(x)
100000 loops, best of 3: 13.6 µs per loop

In [64]: timeit np.empty((12488, 7588, 3), np.uint8)
100000 loops, best of 3: 14.9 µs per loop

So the extra time in zeros_like is in that copy.

In my tests, the difference in assignment times (x[]=1) is negligible.

My guess is that zeros, ones, empty are all early compiled creations. empty_like was added as a convenience, just drawing shape and type info from its input. zeros_like was written with more of an eye toward easy programming maintenance (reusing empty_like) than for speed.

np.ones and np.full also use the np.empty ... copyto sequence, and show similar timings.


https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/array_assign_scalar.c appears to be file that copies a scalar (such as 0) to an array. I don't see a use of memset.

https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/alloc.c has calls to malloc and calloc.

https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/ctors.c - source for zeros and empty. Both call PyArray_NewFromDescr_int, but one ends up using npy_alloc_cache_zero and the other npy_alloc_cache.

npy_alloc_cache in alloc.c calls alloc. npy_alloc_cache_zero calls npy_alloc_cache followed by a memset. Code in alloc.c is further confused with a THREAD option.

More on the calloc v malloc+memset difference at: Why malloc+memset is slower than calloc?

But with caching and garbage collection, I wonder whether the calloc/memset distinction applies.


This simple test with the memory_profile package supports the claim that zeros and empty allocate memory 'on-the-fly', while zeros_like allocates everything up front:

N = (1000, 1000) 
M = (slice(None, 500, None), slice(500, None, None))

Line #    Mem usage    Increment   Line Contents
================================================
     2   17.699 MiB    0.000 MiB   @profile
     3                             def test1(N, M):
     4   17.699 MiB    0.000 MiB       print(N, M)
     5   17.699 MiB    0.000 MiB       x = np.zeros(N)   # no memory jump
     6   17.699 MiB    0.000 MiB       y = np.empty(N)
     7   25.230 MiB    7.531 MiB       z = np.zeros_like(x) # initial jump
     8   29.098 MiB    3.867 MiB       x[M] = 1     # jump on usage
     9   32.965 MiB    3.867 MiB       y[M] = 1
    10   32.965 MiB    0.000 MiB       z[M] = 1
    11   32.965 MiB    0.000 MiB       return x,y,z
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v2.3 Manual
zeros_like · Return an array of zeros with shape and type of input. empty · Return a new uninitialized array. ones · Return a new array setting values to one. full · Return a new array of given shape filled with value. Examples · Try it in your browser! >>> import numpy as np >>> np.zeros(5) ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Create an array with the same value (np.zeros, np.ones, np.full) | note.nkmk.me
January 23, 2024 - To create an array filled with 0 that has the same shape and data type as another array, use np.zeros_like().