w3resource

NumPy array creation: zeros() function

zeros() function

The zeros() function is used to get a new array of given shape and type, filled with zeros.

Syntax:

numpy.zeros(a, dtype=None, order='K', subok=True)
NumPy array: zeros() function

Version: 1.15.0

Parameter:

Name Description Required /
Optional
shape Shape of the new array, e.g., (2, 3) or 2. Required
dtype The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64. optional
order Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory optional

Return value:

[ndarray] Array of zeros with the given shape, dtype, and order.

Example: numpy.zeros() function

>>> import numpy as np
>>> a = (3,2)
>>> np.zeros(a)
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

Pictorial Presentation:

NumPy array: zeros() function

Example: numpy.zeros() where data type is int

>>> import numpy as np
>>> np.zeros(6)
array([ 0.,  0.,  0.,  0.,  0.,  0.])
>>> np.zeros((6,), dtype=int)
array([0, 0, 0, 0, 0, 0])
>>> np.zeros((3, 1))
array([[ 0.],
       [ 0.],
       [ 0.]])

Pictorial Presentation:

NumPy array: zeros() function
NumPy array: zeros() function

Python - NumPy Code Editor:

Previous: ones_like()
Next: zeros_like()