NumPy matlib.zeros() Function:
The matlib.zeros() function of the NumPy module returns a matrix of the specified shape and type, filled with zeros.
Syntax:
numpy.matlib.zeros(shape, dtype=None, order='C')
Parameters
shape: This is required. This is a tuple that specifies the shape of the matrix.
dtype: This is optional. The represents the data type of the matrix. float is the default value.
order: This is optional. It Specifies whether or not the result should be stored. C (C-style) and F (Fortran-style) are two possible values. ‘C’ is the default value. That means it is the matrix’s insertion order.
Return Value:
A matrix of zeros with the specified shape, dtype, and order is returned.
NumPy matlib.zeros() Function in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Import matlib function of numpy module using the import keyword
- Pass the shape of the matrix(as a tuple) as an argument to the matlib.zeros() function of numpy module to create a matrix with zeros of the given shape.
- Store it in a variable.
- Print the above-obtained matrix with zeros of the given shape
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the shape of the matrix(as a tuple) as an argument to the matlib.zeros() function
# of numpy module to create a matrix with zeros of the given shape
# Store it in a variable.
gvn_matrx = np.matlib.zeros((3,3))
# Print the above obtained matrix with zeros of the given shape
print("The above obtained matrix with zeros of the given shape:")
print(gvn_matrx)
Output:
The above obtained matrix with zeros of the given shape: [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
Example2
The returned matrix will be a single row matrix of shape(1, N) if the shape has length one, i.e. (N,), or if the shape is a scalar N.
Approach:
- Import numpy module using the import keyword.
- Import matlib function of numpy module using the import keyword
- Pass the shape of the matrix(here with only length one) as an argument to the matlib.zeros () function of numpy module to create a matrix with zeros of the given shape
- Store it in a variable.
- Print the above obtained first matrix.
- Pass the shape of the matrix(here with only length one, ) as an argument to the matlib.zeros () function of numpy module to create another matrix with zeros of the given shape
- Store it in another variable.
- Print the above obtained second matrix.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the shape of the matrix(here with only length one) as an argument to the matlib.ones() function
# of numpy module to create a matrix with zeros of the given shape
# Store it in a variable.
gvn_matrx1 = np.matlib.zeros((3))
# Print the above obtained first matrix
print("The above obtained first matrix = ",gvn_matrx1)
# Pass the shape of the matrix(here length one,) as an argument to the matlib.ones() function
# of numpy module to create a matrix with zeros of the given shape
# Store it in another variable.
gvn_matrx2 = np.matlib.zeros((2,))
# Print the above obtained second matrix
print("The above obtained second matrix = ",gvn_matrx2)
Output:
The above obtained first matrix = [[0. 0. 0.]] The above obtained second matrix = [[0. 0.]]
Example3
The dtype option can be used with the matlib.zeros() function to specify the data type of the matrix’s elements.
Below is the implementation:
# Import numpy module using the import keyword
import numpy as np
# Import matlib function of numpy module using the import keyword
import numpy.matlib
# Pass the shape of the matrix(as a tuple), datatype as complex as the arguments
# to the matlib.zeros() function of numpy module to create a
# matrix with zeros of the given shape with complex values.
# Store it in a variable.
gvn_matrx = np.matlib.zeros((3,3), dtype=complex)
# Print the above obtained matrix with zeros of the given shape with complex values.
print("The above obtained matrix(complex) with zeros of the given shape:")
print(gvn_matrx)
Output:
The above obtained matrix(complex) with zeros of the given shape: [[0.+0.j 0.+0.j 0.+0.j] [0.+0.j 0.+0.j 0.+0.j] [0.+0.j 0.+0.j 0.+0.j]]