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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17443620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:14:13  来源:igfitidea点击:

convert a 2D numpy array to a 2D numpy matrix

pythonarraysnumpymatrix

提问by abcdxx

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(array) 但它不起作用。谁能告诉如何将二维数组转换为 numpy 矩阵?数组由所有浮点数组成

回答by Tobias Kienzler

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

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

回答by strpeter

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)