Inverting large sparse matrices with scipy

2024/11/13 10:16:00

I have to invert a large sparse matrix. I cannot escape from the matrix inversion, the only shortcut would be to just get an idea of the main diagonal elements, and ignore the off-diagonal elements (I'd rather not, but as a solution it'd be acceptable).

The matrices I need to invert are typically large(40000 *40000), and only have a handful of non-nonzero diagonals. My current approach is to build everything sparse, and then

posterior_covar = np.linalg.inv ( hessian.todense() )

this clearly takes a long time and plenty of memory.

Any hints, or it's just a matter of patience or making the problem smaller?

Answer

I don't think that the sparse module has an explicit inverse method, but it does have sparse solvers. Something like this toy example works:

>>> a = np.random.rand(3, 3)
>>> a
array([[ 0.31837307,  0.11282832,  0.70878689],[ 0.32481098,  0.94713997,  0.5034967 ],[ 0.391264  ,  0.58149983,  0.34353628]])
>>> np.linalg.inv(a)
array([[-0.29964242, -3.43275347,  5.64936743],[-0.78524966,  1.54400931, -0.64281108],[ 1.67045482,  1.29614174, -2.43525829]])>>> a_sps = scipy.sparse.csc_matrix(a)
>>> lu_obj = scipy.sparse.linalg.splu(a_sps)
>>> lu_obj.solve(np.eye(3))
array([[-0.29964242, -0.78524966,  1.67045482],[-3.43275347,  1.54400931,  1.29614174],[ 5.64936743, -0.64281108, -2.43525829]])

Note that the result is transposed!

If you expect your inverse to also be sparse, and the dense return from the last solve won't fit in memory, you can also generate it one row (column) at a time, extract the non-zero values, and build the sparse inverse matrix from those:

>>> for k in xrange(3) :
...     b = np.zeros((3,))
...     b[k] = 1
...     print lu_obj.solve(b)
... 
[-0.29964242 -0.78524966  1.67045482]
[-3.43275347  1.54400931  1.29614174]
[ 5.64936743 -0.64281108 -2.43525829]
https://en.xdnf.cn/q/72333.html

Related Q&A

Error with tweepy OAuthHandler

Im new here and kind of unexperienced with python, so sorry if the question is trivial.I have this simple script, to fetch followers of a given twitter user:import time import tweepyconsumer_key="…

Overriding __or__ operator on python classes

As a contrived example, suppose Im generating a random fruit basket in python. I create the basket:basket = FruitBasket()Now I want to specify specific combinations of fruit that can occur in the baske…

python charmap codec cant decode byte X in position Y character maps to undefined

Im experimenting with python libraries for data analysis,the problem im facing is this exceptionUnicodeDecodeError was unhandled by user code Message: charmap codeccant decode byte 0x81 in position 165…

get icloud web service endpoints to fetch data

My question may look silly but I am asking this after too much search on Google, yet not have any clue.I am using iCloud web services. For that I have converted this Python code to PHP. https://github.…

How to put result of JavaScript function into python variable. PyQt

I want to make a function in PyQt evaluateJavaScript() (or may be similar one) and than display a result of evaluated function. Real function will be much bigger, and it might not be a string.Im only …

Wrap multiple tags with BeautifulSoup

Im writing a python script that allow to convert a html doc into a reveal.js slideshow. To do this, I need to wrap multiple tags inside a <section> tag. Its easy to wrap a single tag inside anoth…

How to permanently delete a file in python 3 and higher?

I want to permanently delete a file i have created with my python code. I know the os.remove() etc but cant find anything specific to delete a file permanently.(Dont want to fill Trash with unused file…

Django. Python social auth. create profiles at the end of pipeline

I want to add a function at the end of the auth pipeline, the function is meant to check if there is a "Profiles" table for that user, if there isnt it will create a table. The Profiles mode…

What is a good audio library for validating files in Python?

Im already checking for content-type, size, and extension (Django (audio) File Validation), but I need a library to read the file and confirm that it is in fact what I hope it is (mp3 and mp4 mostly).I…

Python 3.6+: Nested multiprocessing managers cause FileNotFoundError

So Im trying to use multiprocessing Manager on a dict of dicts, this was my initial try:from multiprocessing import Process, Managerdef task(stat):test[z] += 1test[y][Y0] += 5if __name__ == __main__:te…