NumPy Tutorial NumPy Statistics NumPy References

NumPy - matlib.zeros() function



The NumPy matlib.zeros() function returns a matrix of given shape and type, filled with zeros.

Syntax

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

Parameters

shape Required. Specify shape of the matrix.
dtype Optional. Specify the desired data-type for the matrix. Default: float
order Optional. Specify whether to store the result. Two possible values are: C (C-style) and F (Fortran-style). Default: 'C'

Return Value

Returns a matrix of zeros with the given shape, dtype, and order.

Example: Create a matrix of zeros

In the example below, matlib.zeros() function is used to create a matrix of zeros of specified shape.

import numpy as np
import numpy.matlib

mat = np.matlib.zeros((2,3))
print(mat)

The output of the above code will be:

[[ 0.  0.  0.]
 [ 0.  0.  0.]]

Example: matlib.zeros() with scalar or length one

If shape has length one i.e. (N,), or is a scalar N, the returned matrix will be a single row matrix of shape (1,N). Consider the following example.

import numpy as np
import numpy.matlib

mat1 = np.matlib.zeros(2)
print("mat1 is:", mat1)

mat2 = np.matlib.zeros((3,))
print("mat2 is:", mat2)

The output of the above code will be:

mat1 is: [[0. 0.]]
mat2 is: [[0. 0. 0.]]

Example: matlib.zeros() function with dtype parameter

The matlib.zeros() function can be used with dtype parameter to provide the data type of the elements of the matrix. In the example below, data type of the matrix is complex.

import numpy as np
import numpy.matlib

mat = np.matlib.zeros((2,2), dtype=complex)
print(mat)

The output of the above code will be:

[[ 0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j]]

❮ NumPy - Functions