在我的代码中,我需要计算矩阵的伪逆,矩阵的某些元素可能是无限的 (np.inf)。有时 pinv() 函数处理得很好并返回一些东西,但有时它只是挂起 100% CPU 使用率,我需要终止进程。请参阅下面的演示:
Python 3.4.3+ (default, Oct 14 2015, 16:03:50)
Type "copyright", "credits" or "license" for more information.
IPython 4.1.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import numpy as np
In [2]: import numpy.linalg as la
In [3]: x = np.array([[550.0, 1], [1, np.inf]])
In [4]: la.pinv(x)
Out[4]:
array([[ 0., 0.],
[ 0., 0.]])
In [5]: x = np.array([[np.inf, np.inf], [np.inf, np.inf]])
In [6]: la.pinv(x)
Out[6]:
array([[ nan, nan],
[ nan, nan]])
In [7]: x = np.array([[550.0, 1], [np.inf, np.inf]])
In [8]: la.pinv(x) # here it just hung and I had to kill it from outside
Killed
为什么会这样?为什么它对 inf 的某些安排很好,但对其他安排却挂起?这可能是一个错误(即应该先检查值)吗?
请您参考如下方法:
我打开了一个 issue在 numpy 的 GitHub 存储库上。
答案是它是 BLAS/LAPACK/ATLAS 中的一个错误,而 numpy (1.10.4) 函数只是对那些库中的过程的薄包装,这些库不会事先检查输入,因为它会损害性能。
