module 'scipy' has no attribute 'sparse'

Hey,

I wrote a function to create a random sparse Matrix. 

but when I run the code in Pycham, I get following error:

C = sp.sparse.lil_matrix(B)
AttributeError: module 'scipy' has no attribute 'sparse'

If I run the same code with Spyder there are no errors. I already reinstalled all packages in the venv.

Do you have any idea what's could be the difference between Spyder and Pycham ?  

Maybe the sparse attribute was removed from the newest version of scipy?

Here is the code i worte:

import scipy as sp
import random
import numpy as np


def create_rand_sparse_SLE(N):

B = np.zeros(shape=(N, N))

B[0][0] = random.randint(1, 100)
B[0][1] = random.randint(1, 100)
B[N - 1][N - 1] = random.randint(1, 100)
B[N - 1][N - 2] = random.randint(1, 100)

for x in range(1, N - 1):
for i in range(0, 3):
B[x][i + x - 1] = random.randint(1, 100)
b_x = np.random.rand(N, 1)
C = sp.sparse.lil_matrix(B)

return C, b_x


C, b_x = create_rand_sparse_SLE(10)

 

0
2 comments

Hi!
Did you use the same interpreter in both Spyder and PyCharm?
If not, it worth trying with the same one.
It's also a good idea to test it in the system terminal.

0

In PyCharm, I also got the same error by running the code including the 2 lines given below:

import scipy

sol, exitCode = scipy.sparse.linalg.gmres(A, rhs, tol=1e-11,  callback=counter)
but fixed after adding the following importing code line:

import scipy.sparse.linalg
In your case, try to add the code:

import scipy.sparse

0

Please sign in to leave a comment.