zeros() function (numpy matrix operations)
0 244
Introduction
In the realm of numerical computing with Python, NumPy stands as a cornerstone library, offering a plethora of functions to handle arrays and matrices efficiently. One such function is zeros()
, which allows for the creation of an array filled with zeros. This function is particularly useful when you need a placeholder array to initialize variables, store intermediate results, or create masks for image processing.
What is zeros()
?
The zeros()
function in NumPy returns a new array of a specified shape and type, filled with zeros. Unlike the empty()
function, zeros()
initializes the array elements to zero, ensuring predictable behavior. This makes it a safer choice when you require an array with known values.
Syntax
numpy.zeros(shape, dtype=float, order='C', *, like=None)
shape
: Specifies the dimensions of the array (e.g., (2, 3)
for a 2x3 matrix).
dtype
: Desired data type for the array (default is float64
).
order
: Determines whether the multi-dimensional data is stored in row-major (C-style) or column-major (Fortran-style) order in memory (default is 'C').
like
: If an array-like object is passed, it ensures the creation of an array object compatible with the passed object. This parameter is available starting from NumPy version 1.20.0.
Example Usage
import numpy as np
# Create a 2x3 array of zeros
arr = np.zeros((2, 3))
print(arr)
Output:
[[0. 0. 0.]
[0. 0. 0.]]
This code creates a 2x3 matrix filled entirely with zeros, showcasing the ease and efficiency of using NumPy for array initialization.
Specifying Data Type
By default, zeros()
creates an array with the float64
data type. However, you can specify a different data type using the dtype
parameter:
arr_int = np.zeros((2, 3), dtype=int)
print(arr_int)
Output:
[[0 0 0]
[0 0 0]]
In this example, the array is created with the int
data type, resulting in integer zeros.
Creating Higher-Dimensional Arrays
The zeros()
function can also be used to create higher-dimensional arrays by passing a tuple representing the shape:
arr_3d = np.zeros((2, 3, 4))
print(arr_3d)
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.]]
This creates a 3-dimensional array with the specified shape, filled with zeros.
Practical Applications
- Initializing Variables: Use
zeros()
to create placeholder arrays for variables that will be populated later. - Image Processing: Create binary masks or initialize image arrays with zeros before processing.
- Neural Networks: Initialize weight matrices with zeros before training a model.
- Solving Linear Equations: Create coefficient matrices filled with zeros before populating them with data.
Conclusion
The zeros()
function in NumPy is a versatile tool for creating arrays filled with zeros. By understanding its parameters and applications, you can leverage this function to efficiently initialize arrays for various numerical computations. Whether you're working with simple arrays or complex multidimensional data, zeros()
provides a reliable foundation for your computations.

Share:
Comments
Waiting for your comments