NumPy Zeros Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn about the numpy.matlib.zeros() function of the NumPy library. This function is used to create and return a matrix filled with zeros of a given shape and data type using the numpy.matlib matrix library.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/with_statement -.-> lab-86518{{"`NumPy Zeros Function`"}} python/variables_data_types -.-> lab-86518{{"`NumPy Zeros Function`"}} python/numeric_types -.-> lab-86518{{"`NumPy Zeros Function`"}} python/tuples -.-> lab-86518{{"`NumPy Zeros Function`"}} python/importing_modules -.-> lab-86518{{"`NumPy Zeros Function`"}} python/numerical_computing -.-> lab-86518{{"`NumPy Zeros Function`"}} python/build_in_functions -.-> lab-86518{{"`NumPy Zeros Function`"}} end

Installation and Importing of Required Libraries

Before proceeding, you must install and import the required libraries. Run the following code to install the NumPy library:

!pip install numpy

The following code block will import the required libraries:

import numpy as np
import numpy.matlib

Syntax of The matlib.zeros() Function

The required syntax to use this function is as follows:

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

where,

  • shape: input tuple to define the shape of the matrix
  • dtype: data type of the matrix, default is float
  • order: storing of result in C- or Fortran-contiguous order, default is C

Creating a Basic Matrix

In this example, we will create a matrix with shape (4,3) without providing data type and order.

print("The shape is:\n", numpy.matlib.zeros((4,3)) )

Output:

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

Creating a Matrix with Data Type and Order

In this example, we will create a matrix of shape (3,4) with integer data type and C-contiguous ordering:

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

Output:

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

Creating a Single Row Matrix

If shape has a length of one i.e. (N,), or is a scalar N, then there will be a single row matrix of shape (1,N) in the output. The following code creates a matrix with shape (4,):

np.matlib.zeros(4)

Output:

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

Summary

In this lab, you have learned how to use the numpy.matlib.zeros() function to create and return matrices with zeros of a specific shape, data type, and order. You must remember the syntax and parameters used in this function. We also provided multiple examples to understand the usage of the function.

Other Python Tutorials you may like