np.zeros() — A Simple Illustrated Guide

5/5 - (1 vote)

In Python, the numpy.zeros() function returns a new array of given shape and type, filled with zeros.Β 

Here is the parameter table of numpy.zeros().

If it sounds great to you, please continue reading, and you will fully understand the numpy.zeros() function through Python code snippets and vivid visualization.

  • Concretely, I will introduce its syntax and parameters.Β 
  • Then, you will learn some basic examples of this function.
  • Finally, I will address three top questions about numpy.zeros(), including numpy.zeros 3d array, numpy.zeros vs. numpy.zeros_like, and numpy.zeros dtype.

You can find all the codes in this tutorial here.

Syntax and Parameters

Here is the syntax of numpy.zeros():

# Syntax
numpy.zeros(shape[, dtype=float[, order='C'[, *[, like=None]]]])

Here is the parameter table of numpy.zeros():

ParameterAcceptDescription
shapeint or tuple of intsThe shape of the new array, e.g., (2, 3) or 2.
dtypedata-type, optionalThe desired data type of the array, e.g., numpy.int8. Default is numpy.float64.
order{'C', 'F'}, optionalWhether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Default is 'C'.
likearray_likeReference object to allow the creation of arrays that are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.

The output of numpy.zeros() function is an array of zeros with the given shape, dtype, and order.Β 

Generally, you will only need to land arguments on the first two parameters, shape and dtype, to control the shape and data type of the output all-zeros array.

Basic Examples

We can just pass a shape argument to the np.zeros and get an all-zeros array back.

For example, we pass a size of (3,3) to the shape parameter and the numpy.zeros returns a new all-zeros array with the size (3,3) and default date type, numpy.float64.

# Basic Example
import numpy as np
shape = (3, 3)
all_zeros_array = np.zeros(shape)
print(all_zeros_array)

Output:

Besides, we can pass an int to the shape parameter.

For instance, let’s pass 3 to the shape parameter and see what the numpy.zeros returns.

Code:

# Basic Example 2
import numpy as np
shape = 3
all_zeros_array = np.zeros(shape)
print(all_zeros_array)

Output:

Great! It returns an all-zeros array with the shape of (1,3). So when you pass an integer to the shape argument, the numpy.zeros will return a one-dimension array with the length as the input integer.

np.zeros() 3d array

So far, we have understood how to generate 1d and 2d all-zeros array with the numpy.zeros. What about a 3d array or even a higher dimensional array? We can achieve it in two ways.

First, you can explicitly pass an (x, y, z) alike tuple to the shape parameter to get a 3d all-zeros array. For example, we’d like a 3d array with the size of (1, 2, 3).

Code:

import numpy as np

# three_dim Example 1
shape = (1, 2, 3)
all_zeros_array = np.zeros(shape)
print(all_zeros_array)

Output:

πŸ’‘ Tip: Take a closer look at the output and see what the size of (1, 2, 3) means. πŸ™‚

Second, you can use the little multiplication trick and get a higher-dimensional all-zeros array in a more compact way. In order to use this trick, you need to ensure that every dimension size in your new array is the same.

For instance, let’s create a 4d all-zeros array with the size of (2, 2, 2, 2).Β 

Code:

import numpy as np

# three_dim Example 2
shape = (2,) * 4
print('shape:\n', shape)
print('-'*85)

all_zeros_array = np.zeros(shape)
print('output array:\n', all_zeros_array)

Output:

⭐ Caveat: Remember to add the comma when representing a single-element tuple.

np.zeros() vs. np.zeros_like()

When searching for np.zeros, you might encounter another alike method, np.zeros_like.

The main difference between np.zeros and np.zeros_like is that the former returns an all-zeros array based on the shape you pass and the latter one based on the array-like thing you pass.

For example, to get a 2d all-zeros array, you can either use our old friend, np.zeros, or use our new friend, np.zerps_like.

Code:

import numpy as np

# np.zeros way:
shape = (2, 3)
first_output = np.zeros(shape)
print('first output:\n', first_output)
print('-' * 85)

# np.zeros_like way:
# given a 2d array like thing
array_like = [[1, 2, 3], [4, 5, 6]]
second_output = np.zeros_like(array_like)
print('second output:\n', second_output)

Output:

Yes, as shown above, we can use either np.zeros or np.zeros_like to generate the same size all-zeros array.

But you may notice that the data types of the output arrays are not the same. This is because we do not specify the dtype parameter in the first output, so it takes the default numpy.float64 data type.

Besides, the input array_like for np.zeros_like has an integer data type. Therefore, the np.zeros_like will return an array of zeros with the same shape and type as a given array, which has the integer data type.

To sum up, you can choose to use numpy.zeros or numpy.zeros_like based on what you have.

When to use np.zeros() and when np.zeros_like()?

If you want to make an all-zeros copy of an array-like object, you will be better off using the numpy.zeros_like. Otherwise, please consider the numpy.zeros.

np.zeros() dtype

Based on our previous example, you probably get more curious about another commonly used parameters of np.zeros, the dtype.

Let’s learn more about it!

To jog your memory, here is the parameter table of numpy.zeros():

First, you can specify the data type of the output array through the dtype parameter.Β 

For example, in our previous 2d array, we also want the first array to have an integer data type. 

Code:

import numpy as np

# dtype unspecified:
shape = (2, 3)
first_output = np.zeros(shape)
print('previous first output:\n', first_output)
print('-' * 85)

# dtype = np.int8:
shape = (2, 3)
first_output = np.zeros(shape, dtype=np.int8)
print('present first output:\n', first_output)

Output:

Lastly, if you want to custom data type for each field, you can also a list of tuples to the dtype parameter.

For example, in a 2d array, I want the x field (the first column) to be numpy.float64 and the y field (the second column) to be numpy.int8.

Code:

import numpy as np

# dtype unspecified:
shape = (3, 2)
output = np.zeros(shape, dtype=[('x', np.float64), ('y', np.int8)])
print('output:\n', output)
print('\noutput dtype:\n', output.dtype)

Output:

Summary

That’s it for our np.zeros() article.Β 

We learned about its syntax, parameters, and basic examples. 

We also worked on the top three questions about the np.zeros() function, ranging from numpy.zeros 3d array, numpy.zeros vs. numpy.zeros_like, and numpy.zeros dtype.Β 

Hope you enjoy all this, and happy coding!