用Python计算稀疏矩阵的N个最小特征值 [英] Computing N smallest eigenvalues of Sparse Matrix in Python

查看:653
本文介绍了用Python计算稀疏矩阵的N个最小特征值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python中找到稀疏矩阵的N个最小特征值.我尝试使用scipy.sparse.linalg.eigen.arpack包,但是在计算最小特征值时非常慢.我在某处读到有移位反转模式,但是当我尝试使用它时,收到一条错误消息,告诉我还不支持移位反转模式.关于我应该如何进行的任何想法?

I'd like to find the N smallest eigenvalues of a sparse matrix in Python. I've tried using the scipy.sparse.linalg.eigen.arpack package, but it is very slow at computing the smallest eigenvalues. I read somewhere that there is a shift-invert mode, but when I try using it, I receive an error message telling me that the shift-invert mode is not yet supported. Any ideas as to how I should proceed?

推荐答案

SciPy版本

比较的.eigs.html#scipy.sparse.linalg.eigs"rel =" noreferrer> scipy.sparse.linalg.eigs看来,移位反转模式自v0.10起便已实现并正常工作.具体来说,v0.9文档中对sigma参数的解释指出该参数尚未实现,但v0.10文档并未表明是这种情况.

SciPy Versions

Comparing the documentation of scipy.sparse.linalg.eigs from SciPy v0.9 with the documentation of scipy.sparse.linalg.eigs from SciPy v0.10 it appears that the shift-invert mode is implemented and working since v0.10. Specifically, the explanation of the sigma parameter in the v0.9 documentation states it is not implemented, but the v0.10 documentation does not indicate that is the case.

如果您没有SciPy v0.10或更高版本,则安装最新版本应该可以使您使用稀疏特征求解器的移位反转模式.

If you do not have SciPy v0.10, or later, installing the latest should enable you to utilize shift-invert mode with the sparse eigensolver.

如问题中所述,可以使用ARPACK接口查找小幅度特征值.这是通过在调用scipy.sparse.linalg.eigs时传递which='SM'来完成的.但是,正如问题中所述,它很慢.在 ARPACK的稀疏特征值问题,其中指出:

As mentioned in the question, it is possible to use the ARPACK interface to find small-magnitude eigenvalues. This is done by passing which='SM' when calling scipy.sparse.linalg.eigs. It is, however, as stated in the question, slow. This is confirmed in the SciPy Tutorial's section on Sparse Eigenvalue Problems with ARPACK, where it states:

请注意,ARPACK通常更适合查找极值特征值:即,具有较大幅度的特征值.特别是,使用which = 'SM'可能会导致执行时间缓慢和/或结果异常.更好的方法是使用 shift-invert模式.

Note that ARPACK is generally better at finding extremal eigenvalues: that is, eigenvalues with large magnitudes. In particular, using which = 'SM' may lead to slow execution time and/or anomalous results. A better approach is to use shift-invert mode.

实验

让我们看一些代码,尝试将shift-invert与SciPy的v0.9和v0.10一起使用.在这两种情况下,我们都将使用以下代码.

Experiments

Let's look at some code trying to use shift-invert with both v0.9 and v0.10 of SciPy. In both cases, we will use the following code.

from scipy.sparse import identity
from scipy.sparse.linalg import eigs

A = identity(10, format='csc')
A.setdiag(range(1, 11))
eigs(A, 3, sigma=0) # find three eigenvalues near zero using shift-invert mode

SciPy v0.9

在SciPy v0.9中运行代码会引发异常.

SciPy v0.9

Running the code in SciPy v0.9 results in an exception being raised.

NotImplementedError: shifted eigenproblem not supported yet

SciPy v0.10

在SciPy 0.10中运行代码会产生预期的结果.

SciPy v0.10

Running the code in SciPy 0.10 produces expected results.

(array([ 1.+0.j,  2.+0.j,  3.+0.j]),
 array([[ -1.00000000e+00+0.j,   5.96300068e-17+0.j,   9.95488924e-17+0.j],
       [  3.55591776e-17+0.j,   1.00000000e+00+0.j,  -4.88997616e-16+0.j],
       [ -3.79110898e-17+0.j,   1.16635626e-16+0.j,   1.00000000e+00+0.j],
       [ -1.08397454e-17+0.j,   1.23544164e-17+0.j,   1.78854096e-15+0.j],
       [  1.68486368e-17+0.j,  -9.37965967e-18+0.j,   2.05571432e-16+0.j],
       [ -2.97859557e-19+0.j,  -3.43100887e-18+0.j,   3.35947574e-17+0.j],
       [  1.89565432e-17+0.j,  -3.61479402e-17+0.j,  -1.33021453e-17+0.j],
       [ -1.40925577e-18+0.j,   3.16953070e-18+0.j,   7.91193025e-17+0.j],
       [  6.76947854e-19+0.j,  -3.75674631e-19+0.j,   3.61821551e-17+0.j],
       [ -3.07505146e-17+0.j,  -6.52050102e-17+0.j,  -8.57423599e-16+0.j]]))

这篇关于用Python计算稀疏矩阵的N个最小特征值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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