3

I’m attempting to make a generalized additive model for some ocean data. My code is the following:

from pygam import LinearGAM
from pygam import LogisticGAM
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns


pdf = pd.read_csv("phytoplankton-ratio-project.csv")
feature =['year','month','Dinoflagellate','Diatom','sea_water_temp_WOA_clim','nitrate_WOA_clim','phosphate_WOA_clim','silicate_WOA_clim']
df= pd.DataFrame()
df= pdf[feature]
df.head(5)

df.loc[:,'total'] = df['Dinoflagellate'] + df['Diatom'] ##
df.loc[:,'percentdia'] = df['Diatom']/df['total']
df.loc[:,'percentdino'] = df['Dinoflagellate']/df['total']
df = df.drop('total', axis=1)
df = df.dropna()
dat2016 = df[df.year == 2016]
dat2016 = dat2016.drop('year', axis=1)

dat2015 = df[df.year == 2015]
dat2015 = dat2015.drop('year', axis=1)

dat16dia = dat2016.drop('percentdino', axis=1)
dat16dino = dat2016.drop('percentdia', axis=1)

X_train = dat2016.drop(['percentdia', 'percentdino'], axis = 1)
#X_train.to_numpy()

y_traindino = dat2016['percentdino']
y_traindia = dat2016['percentdia']

X_test = dat2015.drop(['percentdia', 'percentdino'], axis = 1)
#X_test.to_numpy()

y_testdino = dat2015['percentdino']
y_testdia = dat2015['percentdia']

#5,7
gam = LinearGAM(n_splines=25).gridsearch(X_train.values, y_traindia.values)
gamdino = LinearGAM(n_splines=25).gridsearch(X_train.values, y_traindino.values)

XX = gam.generate_X_grid(term=0)
fig, axs = plt.subplots(1,6, figsize=(20,4))
titles = feature[1:]

for i, ax in enumerate(axs):
    pdep, confi = gam.partial_dependence(XX, feature=i, width=.95)
    ax.plot(XX[:, i], pdep)
    ax.plot(XX[:, i], *confi, c='r', ls='--')
    ax.set_title(titles[i])

However when I run it, I get the following error:

File "C:\Users\A\Documents\PythonRepository\StatsProject\Part2gam.py", line 69, in <module>
    pdep, confi = gam.partial_dependence(XX, feature=i, width=.95)

TypeError: partial_dependence() got an unexpected keyword argument 'feature'

Would anyone know how to fix this?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
April
  • 31
  • 2

1 Answers1

0

I think you are not using the correct parameters, because the Error is telling you that you passed an unexpected argument. I guess that you wanted to use feature=i you should use term=i .

If you see here in the docs you can see the list of params that partial_dependence takes as input.

pygam partial_dependence docs

Here is an example on how they use it Pygam regression docs

Lorenzo Vitali
  • 310
  • 3
  • 9