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 the array_like object.
  • 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 array
myarray = np.array([1, 2, 3, 4, 5, 6])
# implementing the mat() fucntion
mymatrix = np.mat(myarray, dtype = float)
print(mymatrix)
print(type(mymatrix))

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create a one-dimensional array using the array() function. The result is assigned to a variable myarray.
  • Line 7: We implement the mat() function on the variable myarray and use a float data type for the output matrix. The result is assigned to a new variable mymatrix.
  • Line 10: We print the variable mymatrix.
  • Line 11: Using the type() function, we obtain and print the object type of the variable mymatrix we just created.

Note: From the output <class 'numpy.matrix'>, we can see that the object is interpreted as a matrix.

Free Resources