Written and maintained by CASRAI Editorial Board
Last updated
numpy.zeros returns a new array of a given shape and type, filled with zeros. In research code it is the standard way to reserve a result array before a loop fills it in. Its signature in the current NumPy documentation is numpy.zeros(shape, dtype=None, order='C', *, device=None, like=None). This guide covers the call (also searched as python numpy zeros), the parameters that change its output, and how it differs from np.empty, np.ones and np.full.
What np.zeros is used for in research
The main research use is preallocation: create an array of the final size, filled with a known value, then write each result into it. A matrix of zeros is also the neutral starting point for an accumulator (running totals, a confusion-matrix tally, a histogram you build by hand) and for a mask or design matrix you fill column by column.
The reason to prefer zeros over an uninitialised array for this work is that every cell has a defined value. A cell your code never reached still reads 0, not whatever was in memory, and that matters for computational reproducibility: the same script must give the same array on a different machine. For converting existing Python lists rather than creating blank arrays, see creating a NumPy array from a list.
Install or access
NumPy is installed from the official installation page at https://numpy.org/install/. The two commands it gives are:
pip install numpyconda install numpy(inside a conda environment, created withconda create -n my-envand activated withconda activate my-env)
The same page recommends a virtual environment for better dependency management: python -m venv my-env, then source my-env/bin/activate on macOS and Linux or my-env\Scripts\activate on Windows, then pip install numpy. To share the environment with collaborators see sharing a reproducible conda environment. Import the package as np; print(np.__version__) confirms the installed version.
Step-by-step: create an array of zeros
- Import the package:
import numpy as np. - Choose the shape. A single integer gives a one-dimensional array:
np.zeros(5). A tuple gives more dimensions:np.zeros((2, 3))is 2 rows by 3 columns. - Choose the dtype. The default is
numpy.float64. For counts, pass an integer type:np.zeros((5,), dtype=np.int_). - Optionally choose the memory order.
order='C'(the default) stores rows contiguously (row-major);order='F'stores columns contiguously (column-major). - Check what you got:
a.shapeanda.dtype. - Fill the array in your analysis code, and record the shape and dtype in your methods note.
Parameters in the current signature:
shape: an int or a tuple of ints, for example(2, 3)or2.dtype: the data type, defaultnumpy.float64.order:'C'or'F', default'C'.device: for Array API interoperability only; it must be"cpu". Added in NumPy 2.0.0.like: lets the call create an array that is not a NumPy array, through the__array_function__protocol. Added in NumPy 1.20.0.
The function returns an ndarray.
Output and how to read it
The examples below follow the NumPy documentation. The output shown was also reproduced on NumPy 2.5.1 on 2026-10-29.
| Call | Output | What it shows |
|---|---|---|
np.zeros(5) |
array([0., 0., 0., 0., 0.]) |
Floats by default; the trailing dots mark float values |
np.zeros((5,), dtype=np.int_) |
array([0, 0, 0, 0, 0]) |
Integers when you ask for them |
np.zeros((2, 3)) |
two rows of [0., 0., 0.] |
A tuple sets the dimensions |
np.zeros((2,), dtype=[('x','i4'),('y','i4')]) |
array([(0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')]) |
A structured dtype gives named fields, each zeroed |
The documentation’s own example shows the same zeros with extra padding (array([ 0., 0., 0., 0., 0.])); current NumPy prints them without it, which is cosmetic.
np.zeros(5) versus np.zeros((5,))
Both produce the same one-dimensional array of length 5, because an int is accepted as a shape. The tuple form becomes necessary as soon as you want a second dimension.
np.zeros versus np.empty, np.ones and np.full
The NumPy quickstart describes the difference directly: “The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory.” The numpy.empty documentation adds that, unlike zeros, ones and full, it “does not initialize the values of the array, and may therefore be marginally faster”, and that the stored values are arbitrary: “For reproducible behavior, be sure to set each element of the array before reading.” The practical rule for research code is to use zeros unless you will certainly write every element before reading any, and to expect unrelated-looking numbers if you print an empty array.
When you want an array with the same shape and type as one you already have, the documentation’s “See also” points to zeros_like.
Troubleshooting
- TypeError when passing dimensions as separate arguments. Tested on NumPy 2.5.1:
np.zeros(2, 3)raisedTypeError: Cannot interpret '3' as a data type, because the second positional argument isdtype. Wrap the dimensions in a tuple:np.zeros((2, 3)). - Output shows
0.instead of0. The default dtype isfloat64. Passdtype=int(ornp.int_) if you need integer counts. Tested:np.zeros((2, 3), dtype=int)returns an integer array. - Unexpected values in a new array. If an array you expected to be zero holds arbitrary numbers, check whether it was created with
np.emptyrather thannp.zeros. - A keyword is rejected.
deviceexists from NumPy 2.0.0 andlikefrom 1.20.0; checknp.__version__if an older install does not accept them.
Free alternatives
These are related functions in NumPy itself, listed in the zeros documentation’s “See also” section. All ship with NumPy.
| Function | What it returns |
|---|---|
numpy.zeros |
New array of the given shape and type, filled with zeros |
numpy.zeros_like |
Array of zeros with the same shape and type as a given array |
numpy.ones |
Array filled with ones |
numpy.full |
Array filled with a value you choose |
numpy.empty |
New array of the given shape and type without initialising entries; values are arbitrary |
For evenly spaced values rather than a constant fill, see NumPy linspace; for choosing values by condition, see NumPy where.
FAQ
What does numpy zeros do?
It returns a new array of the shape and dtype you give, with every element set to zero. The default dtype is float64.
How do I make a 2D array of zeros in NumPy?
Pass the shape as a tuple: np.zeros((2, 3)) gives 2 rows and 3 columns.
How do I get integer zeros instead of floats?
Set the dtype: np.zeros((5,), dtype=np.int_) returns array([0, 0, 0, 0, 0]).
What is the difference between np.zeros and np.empty?
zeros fills the array with 0. empty does not initialise it, so its values are arbitrary and you must set each element before reading; it may be marginally faster.
What do the order and like parameters do?
order chooses row-major (‘C’) or column-major (‘F’) memory layout. like (NumPy 1.20.0 and later) lets the function create a non-NumPy array through __array_function__.
Sources
- NumPy documentation,
numpy.zeros: https://numpy.org/doc/stable/reference/generated/numpy.zeros.html (checked 2026-10-29). - NumPy documentation,
numpy.empty: https://numpy.org/doc/stable/reference/generated/numpy.empty.html (checked 2026-10-29). - NumPy quickstart: https://numpy.org/doc/stable/user/quickstart.html (checked 2026-10-29).
- NumPy installation instructions: https://numpy.org/install/ (checked 2026-10-29).








