numpy反转不可逆矩阵 [英] Numpy inverts a non-invertible matrix

查看:240
本文介绍了numpy反转不可逆矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的朋友在Python 2Python 3中执行了以下代码行:

My friend and I executed this lines of code in Python 2 and Python 3:

import numpy as np  
mat = np.array([[1,0,0],[-1,3,3],[1,2,2]]) 
np.linalg.inv(mat)

哪个返回:

array([[  1.00000000e+00,   0.00000000e+00,   0.00000000e+00],
   [  1.50119988e+16,   6.00479950e+15,  -9.00719925e+15],
   [ -1.50119988e+16,  -6.00479950e+15,   9.00719925e+15]])

给定哪个是奇怪的:

np.linalg.matrix_rank(mat)

返回2,因此表示矩阵不可不可可逆.

returns 2, thus indicating that the matrix is not invertible.

我从此线程了解到,这可能是由于numpy和python处理浮点数的方式所致,尽管我的矩阵由整数组成.

I understand from this thread that is probably due to the way numpy and python handle floating point numbers, although my matrix consists of whole numbers.

mat中断numpy的逆实现是否有特定原因?

Is there a particular reason why mat breaks numpy's inverse implementation?

推荐答案

正如DYZ指出,矩阵是不可逆的,因为它的秩是2而不是3.

As DYZ pointed out the matrix is not invertible because it's rank is 2 not 3.

之所以得到这样的结果,是因为numpy使用 LU分解来计算逆.即使在矩阵为奇数的情况下,该算法也可以产生结果.如果您对详细信息感兴趣,请阅读链接的维基百科文章.

The reason you are getting such results is because numpy is using LU decomposition to calculate the inverse. This algorithm can yield results even in cases when your matrix is singular. Read linked wikipedia article if you are interested in details.

请注意,产生的逆"是不合时宜的.因此,如果尝试使用它来求解线性方程组,则很可能会给您一堆NaN和Infs.

Note that produced 'inverse' is out of whack. So if you try to use it to solve systems of linear equations it will most likely give you bunch of NaNs and Infs.

我想numpy不会检查结果的质量,这对于高性能库来说很常见.通过将原始矩阵乘以假定的逆,然后检查对角线上的数字是否接近1以及其他数字是否为零,您可以非常便宜地进行自我检查.由于浮点计算的性质,它们不一定完全等于零或一.

I guess numpy does not check the quality of results which is common for high performance libraries. You can do such check yourself very cheaply by multiplying your original matrix by the supposed inverse and checking if numbers on diagonal are close to 1 and other numbers are zeroes. They will not necessarily be exactly equal to zero or one due to nature of floating point computation

正如DSM指出的那样,矩阵的条件编号确实很高.

As DSM pointed out the condition number of your matrix is really high.

>> cond(A)
ans =   2.4956e+16

因此,由于条件不佳的矩阵,您失去了16位精度.最重要的是由浮点不精确引起的错误.

So you are loosing 16 digits of precision due to such ill-conditioned matrix. On top of error caused by floating point imprecision.

顺便说一句,您的示例在Numpy 1.12.0中不起作用

By the way as others pointed out above your example doesn't work in Numpy 1.12.0

>>> import numpy as np
>>> np.version.version
'1.12.0'

>>> import numpy as np
>>> mat = np.array([[1,0,0],[-1,3,3],[1,2,2]])
>>> np.linalg.inv(mat)
Traceback (most recent call last):
  File "/Users/vlad/.pyenv/versions/CourseraDL/lib/python3.4/site-packages/numpy/linalg/linalg.py", line 90, in _raise_linalgerror_singular
    raise LinAlgError("Singular matrix")
numpy.linalg.linalg.LinAlgError: Singular matrix
>>>

这篇关于numpy反转不可逆矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆