• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:16 mins read
You are currently viewing Python NumPy zeros() Function

The NumPy zeros() function in Python is used to create an array of specified shapes and types, with all elements initialized to zero. The zeros() function takes three arguments and returns the array filled with zeros of floating values by default. You can customize the specific datatype and order by passing these parameters.

Advertisements

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 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.


# Quick examples of numpy zeros() function

# Example 1: Create a 1D array of zeros 
# With 7 elements and integer data type
arr = np.zeros(9)

# Example 2: Create a 1D array of zeros 
# With 7 elements and integer data type
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 – This is the shape of the array, represented as an integer or a tuple of integers specifying the dimensions of the array, e.g., (3, 4) or 3.
  • dtype (optional) – This parameter specifies the data type of the array. The default is float. You can specify other data types like int, str, etc.
  • 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 zeros, it is one of the most significant functions in the NumPy library. It generates a new array of given shapes and types, which is filled with zeros. It returns floating zeros of ndarray by default. Let’s create NumPy array using zeros().

You can create a 1D array filled with zeros using the numpy.zeros() function, you simply need to specify the desired length of the array as the shape parameter. For example, np.zeros(9) create a 1D array with 9 elements, and all elements are initialized to zero. The default data type is float, but you can specify a different data type using the dtype parameter if needed.


# Import numpy module
import numpy as np

# Get 1-D array filled with zeros
arr = np.zeros(9)
print("After getting a 1D array filled with zeros:\n",arr)

Yields below output.

numpy zeros

Alternatively, the numpy.zeros() function provides a parameter named dtype that allows you to specify a customized data type for the array. By default, the data type is set to float.

In the below example, np.zeros(7, dtype=int) creates a 1D array with 7 elements, and the dtype=int parameter specifies that the data type of the array should be integers. You can replace int with other valid NumPy data types such as float, bool, str, etc., depending on your requirements.


# Create a 1D array of zeros 
# With 7 elements and integer data type
arr = np.zeros(7, int)
print("After getting a 1D array with Integer Data Type:\n",arr)

# Output:
# After getting a 1D array with Integer Data Type:
#  [0 0 0 0 0 0 0]

4. Create 2-D NumPy Array with Zeros

To create a 2D NumPy array filled with zeros, you can use the numpy.zeros() function and specify the shape of the array as a tuple with the desired number of rows and columns.

In the below example, np.zeros((4,5)) creates a 2D array with 4 rows and 5 columns, and all elements are initialized to zero. Adjust the numbers in the tuple (4,5) to create a 2D array of different dimensions as needed.


# Create two-dimensional array with zeros
arr = np.zeros((4,5))
print("2D Array with zeros:\n",arr)

# Output:
# 2D Array with zeros:
# [[0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0.]]

5. Get 3-D Array with Zeros Filled

To create a 3D NumPy array filled with zeros, you can use the numpy.zeros() function and specify the shape as a tuple with the desired dimensions along each axis.

In the below example, np.zeros((4, 3, 5)) creates a 3D array with dimensions 4x3x5, and all elements are initialized to zero. Adjust the numbers in the tuple (4, 3, 5) to create a 3D array of different dimensions as needed.


# Create three-dimensional array with zeros
arr = np.zeros((4, 3, 5))
print("3D array with zeros:\n",arr)

Yields below output.


# Output:
3D array with zeros:
 [[[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 a NumPy array with zeros and heterogeneous data types using a structured dtype. For example, the array has a shape of (3, 2) and is defined to have two fields, ‘x’ with integer type (‘int’) and ‘y’ with float type (‘float’).

In this array, each element has two fields, ‘x’ and ‘y’, with ‘x’ being an integer and ‘y’ being a float, both initialized to zero. You can further customize the data types or field names based on your specific needs.


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

# Output:
# Array with heterogeneous data types:
# [[(0, 0.) (0, 0.)]
# [(0, 0.) (0, 0.)]
# [(0, 0.) (0, 0.)]]

Frequently Asked Questions

What does numpy.zeros() do?

The numpy.zeros() function in Python is a part of the NumPy library, and it is used to create an array filled with zeros. It is particularly useful when you need to initialize an array before filling it with actual data.

How do I use numpy.zeros() to create a 1D array?

To use numpy.zeros() to create a 1D array, you need to specify the length (number of elements) of the array as the shape parameter.

Can I create a 2D array using numpy.zeros()?

You can create a 2D array using the numpy.zeros() function by specifying a tuple representing the shape of the array. The tuple should contain two elements, where the first element is the number of rows, and the second element is the number of columns.

What is the default data type when using numpy.zeros()?

The default data type when using the numpy.zeros() function is float. If you don’t explicitly specify the dtype (data type) parameter, the array will be created with elements of type float.

What is the difference between ‘C’ and ‘F’ order in the order parameter?

The order parameter specifies whether the array should be stored in C-style (row-major) or Fortran-style (column-major) order. ‘C’ is the default and is usually more efficient for row-wise operations, while ‘F’ might be more efficient for column-wise operations.

Can I create an array with elements other than zero using numpy.zeros()?

numpy.zeros() specifically initializes all elements to zero. If you want to create an array with other initial values, you might want to use functions like numpy.ones(), numpy.full(), or create the array and then modify its values.

Conclusion

In this article, I have explained the syntax and usage of numpy.zeros() function and used this function to create ndarray of specific shapes and datatype filled with zeros with examples.

Happy Learning!!

References