将2D numpy数组转换为2D numpy矩阵

时间:2021-09-18 13:26:45

I have a python code in which I have to convert a 2D array to a 2D matrix so that I can use it to calculate inverse.For that I am using numpy.matrix(array) but it is not working. Can anyone tell how to convert a 2D array to a numpy matrix? The array consists of all float numbers

我有一个python代码,我必须将2D数组转换为2D矩阵,以便我可以使用它来计算逆。因为我使用numpy.matrix(数组),但它不起作用。任何人都可以告诉如何将2D数组转换为numpy矩阵?该数组由所有浮点数组成

2 个解决方案

#1


19  

If a is your array, np.asmatrix(a) is a matrix.

如果a是你的数组,则np.asmatrix(a)是一个矩阵。

#2


1  

If you have a list of lists (as you mentioned), you need to convert it first to a numpy array; see how to convert 2d list to 2d numpy array?

如果你有一个列表列表(如你所提到的),你需要先将它转换为一个numpy数组;看看如何将2d列表转换为2d numpy数组?

A short example is given here:

这里给出一个简短的例子:

import numpy as np
a = [[  0. +0.j,   1.j,   2. -2.j],
     [  4. -4.j,   5. -5.j,   6. -1.j],
     [  8. -8.j,   9. -9.j,  10.]]
b = np.matrix(np.array(a))
b_inv = np.linalg.inv(b)

#1


19  

If a is your array, np.asmatrix(a) is a matrix.

如果a是你的数组,则np.asmatrix(a)是一个矩阵。

#2


1  

If you have a list of lists (as you mentioned), you need to convert it first to a numpy array; see how to convert 2d list to 2d numpy array?

如果你有一个列表列表(如你所提到的),你需要先将它转换为一个numpy数组;看看如何将2d列表转换为2d numpy数组?

A short example is given here:

这里给出一个简短的例子:

import numpy as np
a = [[  0. +0.j,   1.j,   2. -2.j],
     [  4. -4.j,   5. -5.j,   6. -1.j],
     [  8. -8.j,   9. -9.j,  10.]]
b = np.matrix(np.array(a))
b_inv = np.linalg.inv(b)