"
This article is part of in the series

codes on screen

NumPy is one of the foundational modules in the Python library that provides developers working with other modules such as scikit-learn and pandas rely on.

Its zeros() function enables the creation of new arrays of various types filled with zero values. It accepts three parameters and returns arrays filled with floating-point zeros by default. The datatype can be modified by passing the parameters. 

In this brief guide, we walk you through the syntax, parameters, expected return values, and other nuances of the zeros() function. We illustrate the creation of ndarrays filled with zeros with examples. 

NumPy zeros() Function: Basic Examples

# Begin by importing numpy
import numpy as np

# Creating a 1-D array with zeros() 
exampleArray = np.zeros(5)
print(exampleArray)
# Output: [0. 0. 0. 0. 0.]

# Creating an array of integer zeros 
exampleArray = np.zeros(3, int)
print(exampleArray)
# Output: [0 0 0]

# Creating a two-dimensional array of zeros
exampleArray = np.zeros((3,4))
print(exampleArray)
# Output:
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]

# Creating a three-dimensional array of zeros
exampleArray = np.zeros((4, 3, 5))
print(exampleArray)
# Output:
#[[[0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]]
#
# [[0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]]
#
# [[0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]]
#
# [[0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]]]

# Creating an array with heterogeneous data types
exampleArray = np.zeros((3,2), dtype=[('x', 'int'), ('y', 'float')])
print(exampleArray)
# Output:
# [[(0, 0.) (0, 0.)]
#  [(0, 0.) (0, 0.)]
#  [(0, 0.) (0, 0.)]]

NumPy zeros() Function: Syntax

The syntax of the zeros() function of the numpy module is:

numpy.zeros(shape, dtype=float, order='C')

As you can see, the function has three parameters:

  • dtype: Defines the type of elements such as numpy.int8. The default dtype is numpy.float64.
  • order: Stores multi-dimensional data in either row-major (C) or column-major (F) pattern in the memory location.
  • shape: Specifies the shape of an array, which can be an int or a tuple containing ints. 

The zeros() function returns a ndarray of zeros in the supplied shape, datatype, and order.

NumPy zeros() Function: Usage

The function creates ndarrays made out of zeroes in the shape and type you specify. By default, it returns floating zeros of ndarray. 

Let's see how to create an array by passing a single integer to zeros(). 

import numpy as np

exampleArray = np.zeros(4)
print(exampleArray)

# Output:
# [0. 0. 0. 0.]

As you can see, we didn't specify a datatype or the order. A one-dimensional array of the supplied size was created and filled with zeroes.

You can also use a datatype parameter to return an array with integers:

exampleArray = np.zeros(5, int)
print(exampleArray)

# Output
# [0 0 0 0 0]

Creating 2-D Arrays with zeros()

You can create a two-dimensional array of zeros using the zeros() function's shape parameter. Let's see how you can make an array with three rows and four columns using the function:

exampleArray = np.zeros((3,4))
print(exampleArray)

# Output
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]

You can also create three-dimensional arrays with the same function if you pass three numeric parameters instead of two. The first parameter defines the number of two-dimensional arrays that must be in this three-dimensional array. The second and third values define the row and column values of the two-dimensional arrays.