pyGAM icon indicating copy to clipboard operation
pyGAM copied to clipboard

Received error 'csr_matrix' object has no attribute 'A'

Open rr1964 opened this issue 1 year ago • 6 comments

Environment is Python 3.11. In order to avoid the issues of https://github.com/dswah/pyGAM/issues/337 I uninstall scikit-sparse. However, this then presents a new error from line 81 of utils.py:

File [~/micromamba/envs/ml/lib/python3.11/site-packages/pygam/utils.py:81]

(http://localhost:8888/~/micromamba/envs/ml/lib/python3.11/site-packages/pygam/utils.py#line=80), in cholesky(A, sparse, verbose)

 78     warnings.warn(msg)
 80 if sp.sparse.issparse(A):
 81     A = A.A
 83 try:
 84     L = sp.linalg.cholesky(A, lower=False)

AttributeError: 'csr_matrix' object has no attribute 'A'

As it stands now, PyGAM is no longer usable for "newer" version of Python Scipy. (As far as I can tell, this fails on versions of Scipy going back several years).

rr1964 avatar Jul 19 '24 16:07 rr1964

To fix this, I manually changed line 81 of utils.py to A = A.toarray(). You will also need to change lines 706 and 783 of pygam.py to use toarray() instead of the .A syntax. There may be more places as well, depending on which models you use. The broad fix is just to replace .A with toarray() everywhere you get that error.

rr1964 avatar Jul 19 '24 16:07 rr1964

You can downgrade to scipy=1.13.1 to resolve the issue. I suspect it requires a new release to fix the dependency to scipy>1.14.0

MMCMA avatar Aug 05 '24 12:08 MMCMA

Another hack-y solution is a monkey patch:

import scipy.sparse

def to_array(self):
    return self.toarray()

scipy.sparse.spmatrix.A = property(to_array)

import pygam

tylerdougan avatar Oct 29 '24 16:10 tylerdougan

To fix this, I manually changed line 81 of utils.py to A = A.toarray(). You will also need to change lines 706 and 783 of pygam.py to use toarray() instead of the .A syntax. There may be more places as well, depending on which models you use. The broad fix is just to replace .A with toarray() everywhere you get that error.

I did the same. Running Python 3.12.5.

gagreene avatar Dec 11 '24 21:12 gagreene

To fix this, I manually changed line 81 of utils.py to A = A.toarray(). You will also need to change lines 706 and 783 of pygam.py to use toarray() instead of the .A syntax. There may be more places as well, depending on which models you use. The broad fix is just to replace .A with toarray() everywhere you get that error.

Worked for me as well on Python 3.12.2. Additionally, I changed line 49 of utils.py to if False:, to avoid having to uninstall scikit-sparse.

SvenZanetti avatar Jan 19 '25 17:01 SvenZanetti

Currently also hitting this issue in Python 3.13 running scipy version 1.15.3 and pyGAM version 0.9.1.

Unlike other suggestions, I didn't want to fix the .A everywhere in pyGAM, so my solution was to change the underlying csr_matrix behaviour (similar to the monkey patch described above).

In scipy/sparse/_csr.py in my venv, the definition for csr_matrix is the last thing defined. I added the following property:

@property
def A(self):
   return self.toarray()

MyKo101 avatar Jun 17 '25 15:06 MyKo101

Hi all, this has been solved in pygam 0.10.0!

Very sorry about the delay.

dswah avatar Nov 20 '25 09:11 dswah