What is the mat() function from NumPy in Python?
Overview
The mat() function in Python is used to interpret an input array as a matrix.
Syntax
numpy.mat(data, dtype=None)
Parameters
The mat() function takes the following parameters:
data: This represents the input data or thearray_likeobject.dtype: This represents the data type of the output matrix.
Return value
The mat() function returns data interpreted as a matrix.
Example
import numpy as np# creating an arraymyarray = np.array([1, 2, 3, 4, 5, 6])# implementing the mat() fucntionmymatrix = np.mat(myarray, dtype = float)print(mymatrix)print(type(mymatrix))
Explanation
- Line 1: We import the
numpymodule. - Line 4: We create a one-dimensional array using the
array()function. The result is assigned to a variablemyarray. - Line 7: We implement the
mat()function on the variablemyarrayand use afloatdata type for the output matrix. The result is assigned to a new variablemymatrix. - Line 10: We print the variable
mymatrix. - Line 11: Using the
type()function, we obtain and print the object type of the variablemymatrixwe just created.
Note: From the output
<class 'numpy.matrix'>, we can see that the object is interpreted as a matrix.