How to Use the numpy.asmatrix() Method

Numpy.asmatrix() method is “used to interpret the input as a matrix”. If the input is already a matrix or a ndarray of 2 dimensions, it returns the input as is. If the input is a different data type, such as a list or a tuple, it converts it into a matrix.


numpy.asmatrix(data, dtype=None)


data: (array_like)

We provide the input as an array to convert that into the matrix.


The data type of the resultant matrix, formed via the asmatrix function, is provided by default; it’s the same as the inputted data.

Return Value

It takes the inputted array and converts that into a matrix. It doesn’t make a copy of a matrix, or ndarray is passed to it. The data type of the output matrix is the same as the inputted array.

Example: How to Use numpy.asmatrix() Method

import numpy as np

x = np.array([[1, 2], [3, 4]])
m = np.asmatrix(x)
print('Manipulating the matrix formed via asmatrix function')

x[0, 0] = 5

a = np.asmatrix(np.zeros((3, 1)))


[[1 2]
 [3 4]]

Manipulating the matrix formed via asmatrix function

[[5 2]
 [3 4]]

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.