A detailed guide to numpy.zeros() function (6 examples)

Updated: February 28, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing in Python. It provides a high-performance multidimensional array object and tools for working with these arrays. A frequently used function in NumPy is numpy.zeros(), which returns a new array of given shape and type, filled with zeros. In this detailed guide, we’ll explore the numpy.zeros() function through six progressive examples, from basic to advanced usage.

Understanding numpy.zeros()

numpy.zeros(shape, dtype=float, order='C') constructs a new array filled with zeros. The shape parameter defines the shape of the array, dtype specifies the data type, and order influences the layout of the array in memory (‘C’ for row-major, ‘F’ for column-major).

Syntax:

numpy.zeros(shape, dtype=float, order='C')

Parameters:

  • shape: int or tuple of ints. Shape of the new array, e.g., (2, 3) or 2.
  • dtype: data-type, optional. The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
  • order: {‘C’, ‘F’}, optional. Whether to store multi-dimensional data in C-style row-major order or Fortran-style column-major order. The default is ‘C’.

Returns:

  • out: ndarray. Array of zeros with the given shape, dtype, and order.

Example 1: Basic Zero Array

import numpy as np
array1 = np.zeros(5)
print(array1)
# Output: [0. 0. 0. 0. 0.]

This example demonstrates the simplest form of creating a zero-filled array with a single dimension.

Example 2: 2D Zero Array

import numpy as np

array2 = np.zeros((2,3))
print(array2)
# Output: [[0. 0. 0.]  [0. 0. 0.]]

Expanding on the first example, here we created a two-dimensional array filled with zeros, specifying the shape as a tuple.

Example 3: Specifying Data Type

import numpy as np

array3 = np.zeros((2,2), dtype=np.int32)
print(array3)
# Output: [[0 0]  [0 0]]

Specifying the dtype parameter allows us to define the desired data type for the array elements, in this case, integers.

Example 4: Multidimensional Zero Array

import numpy as np

array4 = np.zeros((2,2,2))
print(array4)
# Output: [[[0. 0.]   [0. 0.]]   [[0. 0.]  [0. 0.]]]

Moving into more complex territory, this example illustrates creating a three-dimensional array filled with zeros.

Example 5: Using order Parameter

import numpy as np

array5 = np.zeros((2,3), order='F')
print(array5)
# Output: [[0. 0. 0.]  [0. 0. 0.]]

The order parameter impacts how array elements are stored in memory. This example shows an array created with a column-major (Fortran-style) memory layout.

Example 6: Combining numpy.zeros() with numpy operations

Real-world scenarios often involve initializing an array with zeros and then populating it with data through computational operations. Let’s see an immersive example:

import numpy as np

array6 = np.zeros((3,3))
for i in range(3):
    for j in range(3):
        array6[i,j] = i + j
print(array6)
# Output: [[0. 1. 2.]  [1. 2. 3.]  [2. 3. 4.]]

In this advanced example, we demonstrate initializing a 2D array with zeros and then filling it with values representing the sum of its indices.

Conclusion

Understanding the numpy.zeros() function is foundational for efficient numerical computations in Python. We’ve explored various facets of this versatile function through examples—from simple array creations to advanced manipulations, showcasing its broad capabilities. With practice, you can adeptly use numpy.zeros() to initialize arrays for a range of numerical operations, significantly enhancing your data processing workflows.