Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - cannot import `linalg`

Tags:

python

import

I have this code

import scipy.sparse as sparse
import numpy as np

id = np.eye(13)
vals, vecs = sparse.linalg.eigsh(id, k=6)
vals

which is just the example code from the documentation here.

I am running it in a Python 2.7 console and I get the following error message:

AttributeError: 'module' object has no attribute 'linalg'

Does anyone know why this happens?

like image 902
SuperCiocia Avatar asked May 18 '26 17:05

SuperCiocia


1 Answers

Try this code

import scipy.sparse.linalg as sp
import numpy as np

id = np.eye(13)
vals, vecs = sp.eigsh(id, k=6)
vals

This happens because linalg is a directory and not source code i.e it is a sub-package. And I guess this causes the issue because some of the Scipy sub modules do not have __init__.py, Maybe the devs did this to reduce loading times of top-level packages. You can find this information in Scipy Organization section in this link

like image 151
Vineeth Sai Avatar answered May 20 '26 06:05

Vineeth Sai