Python 使用 numpy 求逆矩阵

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21638895/
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-18 23:18:43  来源:igfitidea点击:

Inverse of a matrix using numpy

pythonnumpymatrix

提问by Jake Z

I'd like to use numpy to calculate the inverse. But I'm getting an error:

我想用 numpy 来计算逆。但我收到一个错误:

'numpy.ndarry' object has no attribute I

To calculate inverse of a matrix in numpy, say matrix M, it should be simply: print M.I

要在 numpy 中计算矩阵的逆矩阵,比如矩阵 M,它应该是简单的: print M.I

Here's the code:

这是代码:

x = numpy.empty((3,3), dtype=int)
for comb in combinations_with_replacement(range(10), 9):
   x.flat[:] = comb
   print x.I

I'm presuming, this error occurs because x is now flat, thus 'I' command is not compatible. Is there a work around for this?

我假设,发生此错误是因为 x 现在是平的,因此“ I”命令不兼容。有解决办法吗?

My goal is to print the INVERSE MATRIX of every possible numerical matrix combination.

我的目标是打印每个可能的数值矩阵组合的 INVERSE MATRIX。

采纳答案by user2357112 supports Monica

The Iattribute only exists on matrixobjects, not ndarrays. You can use numpy.linalg.invto invert arrays:

I属性仅存在于matrix对象上,而不存在于ndarrays 上。您可以使用numpy.linalg.inv来反转数组:

inverse = numpy.linalg.inv(x)

Note that the way you're generating matrices, not all of them will be invertible. You will either need to change the way you're generating matrices, or skip the ones that aren't invertible.

请注意,您生成矩阵的方式并非所有矩阵都是可逆的。您要么需要更改生成矩阵的方式,要么跳过不可逆的矩阵。

try:
    inverse = numpy.linalg.inv(x)
except numpy.linalg.LinAlgError:
    # Not invertible. Skip this one.
    pass
else:
    # continue with what you were doing

Also, if you want to go through all 3x3 matrices with elements drawn from [0, 10), you want the following:

此外,如果您想遍历所有具有从 [0, 10) 绘制的元素的 3x3 矩阵,您需要以下内容:

for comb in itertools.product(range(10), repeat=9):

rather than combinations_with_replacement, or you'll skip matrices like

而不是combinations_with_replacement,否则你会跳过像这样的矩阵

numpy.array([[0, 1, 0],
             [0, 0, 0],
             [0, 0, 0]])

回答by user1330052

What about inv?

什么INV

e.g.: my_inverse_array = inv(my_array)

例如:my_inverse_array = inv(my_array)

回答by Eric Leschinski

Inverse of a matrix using python and numpy:

使用 python 和 numpy 的矩阵求逆:

>>> import numpy as np
>>> b = np.array([[2,3],[4,5]])
>>> np.linalg.inv(b)
array([[-2.5,  1.5],
       [ 2. , -1. ]])

Not all matrices can be inverted. For example singular matrices are not Invertable:

并非所有矩阵都可以反转。例如奇异矩阵是不可逆的

>>> import numpy as np
>>> b = np.array([[2,3],[4,6]])
>>> np.linalg.inv(b)

LinAlgError: Singular matrix

Solution to singular matrix problem:

奇异矩阵问题的解:

try-catch the Singular Matrix exception and keep going until you find a transform that meets your prior criteria AND is also invertable.

尝试捕获奇异矩阵异常并继续进行,直到找到满足先前条件并且也是可逆的变换。

Intuition for why matrix inversion can't always be done; like in singular matrices:

为什么不能总是进行矩阵求逆的直觉;就像在奇异矩阵中一样:

Imagine an old overhead film projector that shines a bright light through film onto a white wall. The pixels in the film are projected to the pixels on the wall.

想象一下旧的高架电影放映机,它通过胶片将明亮的光线照射到白墙上。胶片中的像素被投影到墙上的像素上。

If I stop the film projection on a single frame, you will see the pixels of the film on the wall and I ask you to regenerate the film based on what you see. That's easy, you say, just take the inverse of the matrix that performed the projection. An Inverse of a matrix is the reversal of the projection.

如果我停止在单帧上放映电影,您会在墙上看到电影的像素,我要求您根据所看到的内容重新生成电影。你说这很容易,只需取执行投影的矩阵的逆即可。矩阵的逆是投影的反转。

Now imagine if the projector was corrupted, and I put a distorted lens in front of the film. Now multiple pixels are projected to the same spot on the wall. I asked you again to "undo this operation with the matrix inverse". You say: "I can't because you destroyed information with the lens distortion, I can't get back to where we were, because the matrix is either Singular or Degenerate."

现在想象一下,如果放映机损坏了,我在胶卷前面放了一个扭曲的镜头。现在多个像素被投影到墙上的同一个点。我再次要求您“使用矩阵求逆撤消此操作”。你说:“我不能,因为你用镜头畸变破坏了信息,我不能回到我们原来的地方,因为矩阵要么是奇异的,要么是退化的。”

A matrix that can be used to transform some data into other data is invertable only if the process can be reversed with no loss of information. If your matrix can't be inverted, perhaps you are defining your projection using a guess-and-check methodology rather than using a process that guarantees a non-corrupting transform.

仅当该过程可以在不丢失信息的情况下逆转时,可用于将某些数据转换为其他数据的矩阵才是可逆的。如果您的矩阵不能反转,那么您可能正在使用猜测和检查方法而不是使用保证不破坏变换的过程来定义您的投影。

If you're using a heuristic or anything less than perfect mathematical precision, then you'll have to define another process to manage and quarantine distortions so that programming by Brownian motion can resume.

如果您使用的是启发式算法或任何不那么完美的数学精度,那么您必须定义另一个过程来管理和隔离失真,以便可以恢复布朗运动的编程。

Source:

来源:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv.html#numpy.linalg.inv

http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv.html#numpy.linalg.inv

回答by dagrha

Another way to do this is to use the numpy matrixclass(rather than a numpy array) and the Iattribute. For example:

另一种方法是使用numpymatrix(而不是 numpy 数组)和I属性。例如:

>>> m = np.matrix([[2,3],[4,5]])
>>> m.I
matrix([[-2.5,  1.5],
       [ 2. , -1. ]])