使用 scipy 计算矩阵秩
我想使用 scipy 计算矩阵的数学排名。最明显的函数 numpy.rank 计算数组的维度(即标量的维度为 0、向量的维度为 1、矩阵的维度为 2,等等)。我知道 numpy.linalg.lstsq 模块具有此功能,但我想知道这样的基本操作是否内置于矩阵类中的某个位置。
这是一个明确的示例:
from numpy import matrix, rank
A = matrix([[1,3,7],[2,8,3],[7,8,1]])
print rank(A)
这给出了 2
维度,我在其中寻找 3
的答案。
I'd like to calculate the mathematical rank of a matrix using scipy. The most obvious function numpy.rank
calculates the dimension of an array (ie. scalars have dimension 0, vectors 1, matrices 2, etc...). I am aware that the numpy.linalg.lstsq
module has this capability, but I was wondering if such a fundamental operation is built into the matrix class somewhere.
Here is an explicit example:
from numpy import matrix, rank
A = matrix([[1,3,7],[2,8,3],[7,8,1]])
print rank(A)
This gives 2
the dimension, where I'm looking for an answer of 3
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Numpy 提供了 numpy.linalg.matrix_rank() :
Numpy provides
numpy.linalg.matrix_rank()
:为需要在实践中完成此操作的人们提供一个粗略的代码片段。请随意改进。
To provide a rough code snippet for people who need to get this done in practice. Feel free to improve.
如果 numpy 不提供排名功能,为什么不编写自己的呢?
计算秩的有效方法是通过奇异值分解 - 矩阵的秩等于非零奇异值的数量。
请注意,
eps
取决于您的应用程序 - 大多数人都会同意 1e-12 对应于零,但即使 eps=1e-9,您也可能会看到数值不稳定。以你的例子来说,答案是三个。如果将第二行更改为
[2, 6, 14]
(与第一行线性相关),则答案为 2(“零”特征值为 4.9960E-16)If
numpy
does not offer a rank facility, why don't you write your own?An efficient way to compute the rank is via the Singular Value Decomposition - the rank of the matrix is equal to the number of non-zero singular values.
Notice that
eps
depends in your application - most would agree that 1e-12 corresponds to zero, but you may witness numerical instability even for eps=1e-9.Using your example, the answer is three. If you change the second row to
[2, 6, 14]
(linearly dependent with row one) the answer is two (the "zero" eigenvalue is 4.9960E-16)这个答案已经过时了。
答案是否定的——目前 scipy 中没有专门用于计算数组/矩阵的矩阵秩的函数。之前已经讨论过添加一个,但如果它会发生,我不相信它会发生。
This answer is out of date.
The answer is no—there is currently no function dedicated to calculating the matrix rank of an array/matrix in scipy. Adding one has been discussed before, but if it's going to happen, I don't believe it has yet.
我不太了解 Numpy,但这不太可能是矩阵上的内置运算;它涉及相当密集的数值计算(以及有关浮点舍入误差等的相关问题)和阈值选择,这些阈值选择可能适合也可能不适合给定的上下文,并且算法选择对于准确、快速地计算它很重要。
内置于基本类中的事物往往是可以以独特且直接的方式执行的事物,例如最复杂的矩阵乘法。
I don't know about Numpy in particular, but that's unlikely to be a built-in operation on a matrix; it involves fairly intensive numerical computations (and associated concerns about floating-point roundoff error and so forth) and threshold selections that may or may not be appropriate in a given context, and algorithm selection is important to computing it accurately and quickly.
Things that are built into the basic classes tend to be things that can be performed in a unique and straightforward manner, such as matrix multiplications at the most complex.
线性代数函数通常分组在
numpy.linalg
中。 (它们也可以从 scipy.linalg 获得,它具有更多功能。)这允许多态性:函数可以接受 SciPy 处理的任何类型。所以,是的,
numpy.linalg.lstsq
函数可以满足您的要求。为什么这还不够呢?The linear algebra functions are generally grouped in
numpy.linalg
. (They're also available fromscipy.linalg
, which has more functionality.) This allows polymorphism: the functions can accept any of the types that SciPy handles.So, yes, the
numpy.linalg.lstsq
function does what you're asking. Why is that insufficient?scipy
现在包含一个高效的 插值方法,使用随机方法估计矩阵/LinearOperator 的秩,通常足够准确:scipy
now contains an efficient interpolative method for estimating the rank of a matrix/LinearOperator using random methods, which can often be accurate enough: