dswah / pyGAM

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

How to set up n_splines in gridsearch for models with linear and nonlinear terms? #310

Open maju116 opened 2 years ago

maju116 commented 2 years ago

Hi,

I have a simple model that looks like:

model = LogisticGAM(l(0) + s(1, basis="cp") + te(2, 3, basis="ps"))

I want to use gridsearch to find best value for n_splines param in s and te terms. My first idea was to use:

model.gridsearch(X_train, y_train, n_splines=[[], [150], [2, 4]])

which gave me en error:

Traceback (most recent call last):
  File "/home/maju116/anaconda3/envs/travel-parametric-insurance/lib/python3.9/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 2, in <module>
  File "/home/maju116/anaconda3/envs/travel-parametric-insurance/lib/python3.9/site-packages/pygam/pygam.py", line 1845, in gridsearch
    raise ValueError(msg)
ValueError: n_splines grid should have 4 columns, but found grid with 3 columns

I don't know why I have to specify two lists for tensor product, but later on I've tried:

model.gridsearch(X_train, y_train,  n_splines=[[], [150], [2, 4], [2, 4]])

which gave me different error:

- |#                                                  | 0 Elapsed Time: 0:00:00
Traceback (most recent call last):
  File "/home/maju116/anaconda3/envs/travel-parametric-insurance/lib/python3.9/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 3, in <module>
  File "/home/maju116/anaconda3/envs/travel-parametric-insurance/lib/python3.9/site-packages/pygam/pygam.py", line 1584, in summary
    raise AttributeError('GAM has not been fitted. Call fit first.')
AttributeError: GAM has not been fitted. Call fit first.

Fitting the model with same list of n_splines also fails:

model.gridsearch(X_train, y_train, n_splines=[2, 4])
N/A% (0 of 2) |                          | Elapsed Time: 0:00:00 ETA:  --:--:--Traceback (most recent call last):
  File "/home/maju116/anaconda3/envs/travel-parametric-insurance/lib/python3.9/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 2, in <module>
  File "/home/maju116/anaconda3/envs/travel-parametric-insurance/lib/python3.9/site-packages/pygam/pygam.py", line 1888, in gridsearch
    gam.set_params(**param_grid)
  File "/home/maju116/anaconda3/envs/travel-parametric-insurance/lib/python3.9/site-packages/pygam/core.py", line 178, in set_params
    setattr(self, parameter, value)
  File "/home/maju116/anaconda3/envs/travel-parametric-insurance/lib/python3.9/site-packages/pygam/terms.py", line 1002, in __setattr__
    n = np.atleast_1d(getattr(term, name)).size
AttributeError: 'LinearTerm' object has no attribute 'n_splines'

What should I do to run this gridsearch correctly for both s and te ?