简体   繁体   English

numpy.linalg.svd不按降序返回Sigma

[英]numpy.linalg.svd not returning Sigma in descending order

Im currently computing an SVD on a large matrix (an image, to be exact) using numpy.linalg's svd function. 我目前正在使用numpy.linalg的svd函数在大型矩阵(准确地说是图像)上计算SVD。 The documentation and examples that I've found all seem to indicate that the Sigma values that are returned are ordered in descending order (Implying the correct ordering of U and V^T). 我发现的所有文档和示例似乎都表明,返回的Sigma值按降序排列(表示U和V ^ T的正确顺序)。

However, in my testing the sigma values appear unordered. 但是,在我的测试中,sigma值似乎是无序的。 So my question is whether for some reason something is going wrong in my linalg (highly unlikely I know), or if it simply returns the sigma's as unordered? 所以我的问题是,由于某种原因,我的linalg是否出了问题(我不太可能知道),还是只是简单地将sigma返回为无序?

A follow-up question is then the best way to sort the sigma's so that the order in U and V^T also reflect the change. 因此,后续问题是对sigma进行排序的最佳方法,这样U和V ^ T中的顺序也可以反映出变化。

Since linalg.svd is just an interface to LAPACK dgesdd the singular values should be ordered. 由于linalg.svd只是LAPACK dgesdd的接口,因此dgesdd奇异值进行排序。

>>> import numpy as np
>>> A = np.random.randn(2400,3600)
>>> U, s, V = np.linalg.svd(A, full_matrices=False)
>>> np.allclose(A, np.dot(U*s, V))
True
>>> (s[:-1] >= s[1:]).all()
True

If you get unordered results check if the result is correct, like in the example above. 如果您得到无序的结果,请检查结果是否正确,如上例所示。 If not you may have a lapack bug or (less likely) a numpy bug. 如果不是,则可能是lapack错误或(不太可能)是numpy错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM