NumPy - Matrix Library
The NumPy package contains matlib module. This module contains all the functions in the numpy namespace that return matrices instead of ndarray objects. Below mentioned are most commonly used functions of this module:
Function | Description |
---|---|
matlib.empty() | Returns a matrix of given shape and type, without initializing entries. |
matlib.zeros() | Returns a matrix of given shape and type, filled with zeros. |
matlib.ones() | Returns a matrix of given shape and type, filled with ones. |
matlib.eye() | Returns a matrix with ones on the diagonal and zeros elsewhere. |
matlib.identity() | Returns the square identity matrix of given size. |
matlib.rand() | Return a matrix of random values with given shape. |
matlib.randn() | Return a random matrix with data from the “standard normal” distribution. |
Lets discuss these functions in detail:
numpy.matlib.empty() function
The numpy.matlib.empty() function returns a matrix of given shape and type, without initializing entries.
Syntax
numpy.matlib.empty(shape, dtype=None, order='C')
Parameters
shape |
Required. Specify shape of the matrix. |
dtype |
Optional. Specify the desired data-type for the matrix. |
order |
Optional. Specify whether to store the result. Two possible values are: C (C-style) and F (Fortran-style). Default: 'C' |
Example:
The function is used to create a matrix of uninitialized (arbitrary) entries of specified shape.
import numpy as np import numpy.matlib mat = np.matlib.empty((2,2)) print(mat)
The output of the above code will be:
[[1.58262349e-316 0.00000000e+000] [6.21064510e+175 6.78850084e+199]]
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' |
Example:
The 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.]]
numpy.matlib.ones() function
The numpy.matlib.ones() function returns a matrix of given shape and type, filled with ones.
Syntax
numpy.matlib.ones(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' |
Example:
The function is used to create a matrix of ones of specified shape.
import numpy as np import numpy.matlib mat = np.matlib.ones((2,3)) print(mat)
The output of the above code will be:
[[1. 1. 1.] [1. 1. 1.]]
numpy.matlib.eye() function
The numpy.matlib.eye() function returns a matrix with ones on the diagonal and zeros elsewhere.
Syntax
numpy.matlib.eye(n, M=None, k=0, dtype='float', order='C')
Parameters
n |
Required. Specify number of rows in the matrix. |
M |
Optional. Specify number of columns in the matrix. Default is n. |
k |
Optional. Specify index of the diagonal. 0 refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. |
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' |
Example:
In the example below, the function is used to fill the diagonal at index=1 with one and zero elsewhere.
import numpy as np import numpy.matlib mat = np.matlib.eye(n=3, M=4, k=1) print(mat)
The output of the above code will be:
[[0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]]
numpy.matlib.identity() function
The numpy.matlib.identity() function returns a square identity matrix of given size.
Syntax
numpy.matlib.identity(n, dtype=None)
Parameters
n |
Required. Specify the size of the returned identity matrix. |
dtype |
Optional. Specify the desired data-type for the matrix. Default: float |
Example:
The example below shows the usage of matlib.identity() function.
import numpy as np import numpy.matlib mat = np.matlib.identity(3, dtype=int) print(mat)
The output of the above code will be:
[[1 0 0] [0 1 0] [0 0 1]]
numpy.matlib.rand() function
The numpy.matlib.rand() function returns a matrix of random values with given shape. The function creates a matrix of the given shape and propagate it with random samples from a uniform distribution over [0, 1).
Syntax
numpy.matlib.rand(*args)
Parameters
*args |
Required. Specify shape of the output. If given as N integers, each integer specifies the size of one dimension. If given as a tuple, this tuple gives the complete shape. |
Example:
In the example below, the function is used to create a matrix of given shape containing random values from a uniform distribution over [0, 1).
import numpy as np import numpy.matlib mat = np.matlib.rand(3,2) print(mat)
The possible output of the above code could be:
[[0.76220569 0.45832152] [0.2573741 0.16884502] [0.67076371 0.94206513]]
numpy.matlib.randn() function
The numpy.matlib.randn() function returns a matrix filled with random floats sampled from a univariate normal (Gaussian) distribution of mean 0 and variance 1.
Syntax
numpy.matlib.randn(*args)
Parameters
*args |
Required. Specify shape of the output. If given as N integers, each integer specifies the size of one dimension. If given as a tuple, this tuple gives the complete shape. |
Example:
In the example below, matlib.randn() function is used to create a matrix of given shape containing random values from the standard normal distribution, N(0, 1).
import numpy as np import numpy.matlib mat = np.matlib.randn(3,3) print(mat)
The possible output of the above code could be:
[[-0.48017485 1.19876658 1.05405775] [ 2.03861756 0.06356518 -0.40892882] [ 1.25324351 0.50041813 0.73766593]]