Summary: in this tutorial, you’ll learn how to create a numpy array of a given shape whose elements are filled with zeros.
The zeros()
function of the numpy
module allows you to create a numpy array of a given shape whose elements are filled with zeros.
For example, the following uses the zeros()
function to create an array with two axes, the first axis has two elements and the second axis has three elements:
import numpy as np
a = np.zeros((2, 3))
print(a)
Code language: Python (python)
Output:
[[0. 0. 0.]
[0. 0. 0.]]
Code language: Python (python)
In layman’s terms, this example creates a 2-D array or a matrix that has two rows and three columns.
By default, zeros()
function uses the type float64
for its elements. For example:
import numpy as np
a = np.zeros((2, 3))
print(a.dtype)
Code language: Python (python)
Output:
float64
Code language: Python (python)
To use a different type, you need to explicitly specify it in the zeros()
function via the dtype
argument. For example:
import numpy as np
a = np.zeros((2, 3), dtype=np.int32)
print(a)
print(a.dtype)
Code language: Python (python)
Output:
[[0 0 0]
[0 0 0]]
int32
Code language: Python (python)
In this example, we use int32
type for the elements. Hence, you don’t see the decimal point (.) in the output.
Summary
- Use numpy
zeros()
function to create an array of a given shape whose elements are filled with zeros.