使用 scipy 计算矩阵秩

发布于 2024-08-25 12:41:47 字数 416 浏览 3 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

も让我眼熟你 2024-09-01 12:41:47

Numpy 提供了 numpy.linalg.matrix_rank() :

>>> import numpy
>>> numpy.__version__
'1.5.1'
>>> A = numpy.matrix([[1,3,7],[2,8,3],[7,8,1]])
>>> numpy.linalg.matrix_rank(A)
3

Numpy provides numpy.linalg.matrix_rank():

>>> import numpy
>>> numpy.__version__
'1.5.1'
>>> A = numpy.matrix([[1,3,7],[2,8,3],[7,8,1]])
>>> numpy.linalg.matrix_rank(A)
3
深居我梦 2024-09-01 12:41:47

为需要在实践中完成此操作的人们提供一个粗略的代码片段。请随意改进。

u, s, v = np.linalg.svd(A)
rank = np.sum(s > 1e-10)

To provide a rough code snippet for people who need to get this done in practice. Feel free to improve.

u, s, v = np.linalg.svd(A)
rank = np.sum(s > 1e-10)
梦醒灬来后我 2024-09-01 12:41:47

如果 numpy 不提供排名功能,为什么不编写自己的呢?

计算秩的有效方法是通过奇异值分解 - 矩阵的秩等于非零奇异值的数量。

def rank(A, eps=1e-12):
    u, s, vh = numpy.linalg.svd(A)
    return len([x for x in s if abs(x) > eps])

请注意,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.

def rank(A, eps=1e-12):
    u, s, vh = numpy.linalg.svd(A)
    return len([x for x in s if abs(x) > eps])

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)

热风软妹 2024-09-01 12:41:47

这个答案已经过时了。

答案是否定的——目前 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.

最好是你 2024-09-01 12:41:47

我不太了解 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.

回忆躺在深渊里 2024-09-01 12:41:47

线性代数函数通常分组在numpy.linalg中。 (它们也可以从 scipy.linalg 获得,它具有更多功能。)这允许多态性:函数可以接受 SciPy 处理的任何类型。

所以,是的,numpy.linalg.lstsq函数可以满足您的要求。为什么这还不够呢?

The linear algebra functions are generally grouped in numpy.linalg. (They're also available from scipy.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?

哆兒滾 2024-09-01 12:41:47

scipy 现在包含一个高效的 插值方法,使用随机方法估计矩阵/LinearOperator 的秩,通常足够准确:

>>> from numpy import matrix
>>> A = matrix([[1,3,7],[2,8,3],[7,8,1]], dtype=float)  # doesn't accept int

>>> import scipy.linalg.interpolative as sli
>>> sli.estimate_rank(A, eps=1e-10)
3

scipy now contains an efficient interpolative method for estimating the rank of a matrix/LinearOperator using random methods, which can often be accurate enough:

>>> from numpy import matrix
>>> A = matrix([[1,3,7],[2,8,3],[7,8,1]], dtype=float)  # doesn't accept int

>>> import scipy.linalg.interpolative as sli
>>> sli.estimate_rank(A, eps=1e-10)
3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文