Module 'scipy.sparse' has no attribute 'linalg' error in reticulate virtual environment
Asked Answered
C

1

5

I have written a function which works exactly as I would like it to in python, but when I try and run it in R I run into a problem with reticulate. I have troubleshooted this problem and have reduced it down to this problem. I have this function in python:

def get_largest_eigenvalue(inc_mat):
    eigen_val = scipy.sparse.linalg.eigs(inc_mat, k = 1)
    eigen_val = eigen_val[0] / inc_mat.shape[0]
    return eigen_val

Essentially this function takes a csr_matrix and returns its largest eigenvalue. It should work for any csr_matrix. I get an error when I run this code using reticulate and solving this will solve my larger problem.

To run this code in reticulate I run

library(reticulate)
use_virtualenv("default")

I have previously run this code to install scipy to this virtual environment

virtualenv_install("default", c("scipy"))

I have then used

source_python("file_name.py")

To load all of my python functions, including the one above. All of the other functions I have loaded work perfectly except the one above. When I try and run it I receive the following error

Error in py_call_impl(callable, dots$args, dots$keywords) : AttributeError: module 'scipy.sparse' has no attribute 'linalg'

I am a bit out of my depth here and I have tried running code like

virtualenv_install("default", c("scipy.sparse"))

But this causes more errors and does not work. Any help would be appreciated! Thank you!

Corallite answered 26/1, 2021 at 20:46 Comment(0)
C
7

Add this explicit import to your code:

import scipy.sparse.linalg

Importing just scipy.sparse doesn't import the linalg submodule automatically.

Conflagration answered 26/1, 2021 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.