# Python NumPy zeros() Function

• Post author:
• Post category:NumPy / Python

NumPy `zeros()` function is used to create a new array of given shapes and types filled with zero values. The zeros() function takes three arguments and returns the array filled with zeros of floating values by default. We can customize the specific datatype and order by passing these parameters.

In this article, I will explain zero() function syntax, parameters, return value, and how to use this to create ndarray filled with zeros with examples.

## 1. Quick Examples of Python NumPy zeros() Function

If you are in a hurry, below are some quick examples of how to use the zeros() function to create a NumPy array filled with zero values.

``````
# Below are the quick examples

# Example 1: Create 1-D array use numpy.zeros()
arr = np.zeros(9)

# Example 2: For integer array of zeros
arr = np.zeros(7, int)

# Example 3: Create two-dimensional array with zeros
arr = np.zeros((4,5))

# Example 4: Create three-dimensional array with zeros
arr = np.zeros((4, 3, 5))

# Example 5: Create array with heterogeneous data types
arr = np.zeros((3,2), dtype=[('x', 'int'), ('y', 'float')])
``````

## 2. Syntax of NumPy zeros()

Following is the syntax to create `numpy.zeros()` function.

``````
# Python numpy.zeros() Syntax
numpy.zeros(shape, dtype=float, order='C')
``````

### 2.1 Parameters of zeros()

• `shape` – It defines the shape of the array which is an int or tuple of ints. The shape of the new array, e.g., (3, 4) or 3.
• `dtype` – It specifies the type of the array elements, e.g., numpy.int8. Default is numpy.float64.
• `order` – To store multi-dimensional data in row-major (C) or column-major (F) order/pattern in the memory location.

### 2.2 Return Value of zeros()

It returns ndarray of zeros with the given shape, order, and datatype.

## 3. Usage of Numpy zeros()

Use NumPy `zeros()` function to create ndarray filled with zero’s, it is one of the most significant functions in the NumPy library. It generates a new array of given shape and type, which is filled with zeros. It returns floating zeros of ndarray by default. Let’s create NumPy array using zeros().

In the following example, I am passing a single integer value ‘9’ to the NumPy zeros() function, without specifying data type and order. This creates a NumPy one-dimensional array of size 9 with zero’s. The default data type is `float`.

``````
import numpy as np

# Get 1-D array filled with zeros
arr = np.zeros(9)
print(arr)

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

This function provides a param to the customized datatype.

``````
# Get 1-D array with specified datatype
arr = np.zeros(7, int)
print(arr)

# Output
# [0 0 0 0 0 0 0]
``````

## 4. Create 2-D NumPy Array with Zeros

To create a two-dimensional array of zeros use the shape of columns and rows as the value to `shape` parameter. The following example creates a NumPy array with `4` rows and `5` columns filled with zero value.

``````
# Create two-dimensional array with zeros
arr = np.zeros((4,5))
print(arr)

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

## 5. Get Three-Dimensional Array with Zeros Filled

To create a three-dimensional array of zeros, pass the shape as a tuple to the `shape` parameter. The following example get a Numpy 3-Dimensional array of shapes `(4, 3, 5)` with zeros.

``````
# Create three-dimensional array with zeros
arr = np.zeros((4, 3, 5))
print(arr)
``````

Yields below 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.]]]
``````

## 6. Get an Array Use zeros() with Heterogeneous Data Types

To create arrays with heterogeneous data types specify the types you wanted to use by `dtype` param.

``````
# Create array with heterogeneous data types
arr = np.zeros((3,2), dtype=[('x', 'int'), ('y', 'float')])
print(arr)

# Output
# [[(0, 0.) (0, 0.)]
# [(0, 0.) (0, 0.)]
# [(0, 0.) (0, 0.)]]
``````

## 7. Conclusion

In this article, I have explained the concept of NumPy `zeros()` function and using this function to create ndarray of specific shapes and datatype filled with zeros.

Happy Learning!!

## References 