Signup/Sign In

NumPy zeros() function

In this tutorial, we will cover numpy.matlib.zeros() function of the Numpy library.

The function numpy.matlib.zeros() is used to return the matrix of given shape and type. This method filled the matrix with zeros.

The numpy.matlib is a matrix library used to configure matrices instead of ndarray objects.

Syntax of matlib.zeros():

The required syntax to use this function is as follows:

numpy.matlib.zeros(shape,dtype,order)  

Parameters:

Let us now cover the parameters used with this function:

  • shape
    This parameter is in the form of a tuple that is used to define the shape of the matrix.

  • dtype
    This parameter is used to indicate the data type of the matrix. The default value of this parameter is float. This is an optional parameter.

  • order
    This is an optional parameter that is used to indicate the insertion order of the matrix. It mainly indicates whether to store the result in C- or Fortran-contiguous order, The default value is 'C'.

Returned Values:

This function mainly returns the zero matrix of a given shape, dtype, and order.

Now it's time to cover a few examples of this function.

Example 1:

Given below is a basic example for the understanding of this function:

import numpy as np    
import numpy.matlib    
    
print("The matrix is:\n",numpy.matlib.zeros((4,3)))    


The matrix is:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

Example 2:

Now we will also use type and order parameter in the code snippet given below:

import numpy as np    
import numpy.matlib    
    
print("The 3x4 matrix with all elements in integer is as follows:\n",numpy.matlib.zeros((3,4), int, 'C'))    


The 3x4 matrix with all elements in integer is as follows:
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]

Example 3:

It is important to note that If the shape has length one i.e. (N,), or is a scalar N, then in the output, there will be a single row matrix of shape (1,N). The code snippet for the understanding of this statement is as follows:

import numpy as np

np.matlib.zeros(4)


matrix([[0., 0., 0., 0.]])

Summary

In this tutorial we learned about numpy.zeros() mathematical function in the Numpy library. We also covered its syntax, parameters as well as the value returned by this function along with a few code examples.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.