matrix - whats the fastest way to find eigenvalues/vectors in python? -
matrix - whats the fastest way to find eigenvalues/vectors in python? -
currently im using numpy job. but, i'm dealing matrices several thousands of rows/columns , later figure go tens of thousands, wondering if there bundle in existence can perform kind of calculations faster ?
**if matrix sparse, instantiate matrix using constructor scipy.sparse utilize analogous eigenvector/eigenvalue methods in spicy.sparse.linalg. performance point of view, has 2 advantages:
your matrix, built spicy.sparse constructor, smaller in proportion how sparse is.
the eigenvalue/eigenvector methods sparse matrices (eigs, eigsh) take optional argument, k number of eigenvector/eigenvalue pairs want returned. number required business relationship >99% of variance far less number of columns, can verify ex post; in other words, can tell method not calculate , homecoming of eigenvectors/eigenvalue pairs--beyond (usually) little subset required business relationship variance, it's unlikely need rest.
use linear algebra library in scipy, scipy.linalg, instead of numpy library of same name. these 2 libraries have same name , utilize same method names. yet there's difference in performance. difference caused fact numpy.linalg less faithful wrapper on analogous lapack routines sacrifice performance portability , convenience (i.e., comply numpy design goal entire numpy library should built without fortran compiler). linalg in scipy on other hand much more finish wrapper on lapack , uses f2py.
select function appropriate utilize case; in other words, don't utilize function more need. in scipy.linalg there several functions calculate eigenvalues; differences not large, though careful selection of function calculate eigenvalues, should see performance boost. instance:
scipy.linalg.eig returns both eigenvalues , eigenvectors scipy.linalg.eigvals, returns eigenvalues. if need eigenvalues of matrix do not utilize linalg.eig, utilize linalg.eigvals instead. if have real-valued square symmetric matrices (equal transpose) utilize scipy.linalg.eigshoptimize scipy build preparing scipy build environement done largely in scipy's setup.py script. perhaps important alternative performance-wise identifying optimized lapack libraries such atlas or accelerate/veclib framework (os x only?) scipy can observe them , build against them. depending on rig have @ moment, optimizing scipy build re-installing can give substantial performance increase. additional notes scipy core team here.
will these functions work big matrices?
i should think so. these industrial strength matrix decomposition methods, , lean wrappers on analogous fortran lapack routines.
i have used of methods in linalg library decompose matrices in number of columns between 5 , 50, , in number of rows exceeds 500,000. neither svd nor eigenvalue methods seem have problem handling matrices of size.
using scipy library linalg can calculate eigenvectors , eigenvalues, single call, using of several methods library, eig, eigvalsh, , eigh.
>>> import numpy np >>> scipy import linalg la >>> = np.random.randint(0, 10, 25).reshape(5, 5) >>> array([[9, 5, 4, 3, 7], [3, 3, 2, 9, 7], [6, 5, 3, 4, 0], [7, 3, 5, 5, 5], [2, 5, 4, 7, 8]]) >>> e_vals, e_vecs = la.eig(a)
python matrix numpy linear-algebra eigenvalue
Comments
Post a Comment