dswah / pyGAM

[HELP REQUESTED] Generalized Additive Models in Python
https://pygam.readthedocs.io
Apache License 2.0
857 stars 157 forks source link

LogisticGAM and LinearGAM .fit() throws ValueError: cannot set WRITEABLE flag to True of this array #271

Open tlasko opened 4 years ago

tlasko commented 4 years ago

Trying to use what looks like a promising package, but it chokes on all attempts to fit a model. Minimum example using random data, but it also happens with real data:

>>> import numpy as np
>>> from pygam import LogisticGAM, s
>>> clf=LogisticGAM(s(0)).fit(np.random.rand(10), np.random.randint(2, size=10))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/pygam/pygam.py", line 920, in fit
    self._pirls(X, y, weights)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/pygam/pygam.py", line 705, in _pirls
    E = self._cholesky(S + P, sparse=False, verbose=self.verbose)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/pygam/pygam.py", line 485, in _cholesky
    L = cholesky(A, **kwargs)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/pygam/utils.py", line 59, in cholesky
    P[np.arange(len(p)), p] = 1
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/scipy/sparse/lil.py", line 336, in __setitem__
    IndexMixin.__setitem__(self, key, x)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/scipy/sparse/_index.py", line 99, in __setitem__
    i, j = _broadcast_arrays(row, col)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/scipy/sparse/_index.py", line 26, in _broadcast_arrays
    y.flags.writeable = b.flags.writeable
ValueError: cannot set WRITEABLE flag to True of this array

Also happens using the example from the documentation:

from pygam.datasets import wage
>>> X, y = wage()
from pygam import LinearGAM, s, f
>>> gam = LinearGAM(s(0) + s(1) + f(2)).fit(X, y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/pygam/pygam.py", line 920, in fit
    self._pirls(X, y, weights)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/pygam/pygam.py", line 705, in _pirls
    E = self._cholesky(S + P, sparse=False, verbose=self.verbose)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/pygam/pygam.py", line 485, in _cholesky
    L = cholesky(A, **kwargs)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/pygam/utils.py", line 59, in cholesky
    P[np.arange(len(p)), p] = 1
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/scipy/sparse/lil.py", line 336, in __setitem__
    IndexMixin.__setitem__(self, key, x)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/scipy/sparse/_index.py", line 99, in __setitem__
    i, j = _broadcast_arrays(row, col)
  File "/Users/Tom/opt/anaconda3/envs/ml38/lib/python3.8/site-packages/scipy/sparse/_index.py", line 26, in _broadcast_arrays
    y.flags.writeable = b.flags.writeable
ValueError: cannot set WRITEABLE flag to True of this array

Using Python 3.8.2 pygam 0.8.0 scikit-learn 0.22.2.post1 scikit-sparse 0.4.4 scipy 1.4.1

jancurse commented 4 years ago

I can confirm the same problem using: python 3.7.4 pygam 0.8.0 scikit-learn 0.23.1 scikit-sparse 0.4.4 scipy 1.4.1 numpy 1.18.4

I also tried downgrading numpy to 1.15.4 (after reading a similar issue has been solved like this) but it didn't work.

jerheff commented 3 years ago

I received the same error, but uninstalling scikit-sparse fixed it.

bhishanpdl commented 3 years ago

I had the same problem. I fixed it by uninstalling scikit-sparse as suggested by jerheff.

conda uninstall scikit-sparse

Code to check:

from pygam.datasets import wage
X, y = wage()
from pygam import LinearGAM, s, f
gam = LinearGAM(s(0) + s(1) + f(2)).fit(X, y)

My versions

pygam   0.8.0
seaborn 0.10.1
pandas  1.0.5
sklearn 0.23.1
json    2.0.9
numpy   1.17.5
scipy   1.5.0
craymichael commented 3 years ago

Hi, I believe I have fixed this issue - see https://github.com/dswah/pyGAM/pull/288

In the meantime you can fix manually or use my fork instead

mlldantas commented 1 year ago

I have the same issue. I had to uninstall scikit-sparse to make LinerGAM work. I did understand how this issue was fixed (#288).