Python GAM to fit© Karobben

Python GAM to fit

Python GAM to fit

Source: pygam

import numpy as np
import matplotlib.pyplot as plt
from pygam import LinearGAM, s, te
from pygam.datasets import mcycle

# X = np.array([ [i] for i in Cell_fit.Class_size.to_list()])
# y = np.array(Cell_fit.Size.to_list())

X, y = mcycle(return_X_y=True)

gam = LinearGAM(n_splines=25).gridsearch(X, y)
XX = gam.generate_X_grid(term=0, n=500)

plt.plot(XX, gam.predict(XX), 'r--')
plt.plot(XX, gam.prediction_intervals(XX, width=.95), color='b', ls='--')

plt.scatter(X, y, facecolor='gray', edgecolors='none')
plt.title('95% prediction interval');
© pyGAM

Regression by numpy

Source: W3 school

x = X.flatten()
y = y.to_list()

mymodel = np.poly1d(np.polyfit(x, y, 3))
myline = np.linspace(2, 95, 100)

plt.scatter(x, y)
plt.plot(myline, mymodel(myline))
plt.show()
numpy model regression

Other

X, y = mcycle(return_X_y=True)
gam = LogisticGAM(f(0) + s(1) + s(2)).gridsearch(X, y)

fig, axs = plt.subplots(1, 3)
titles = ['student', 'balance', 'income']

for i, ax in enumerate(axs):
XX = gam.generate_X_grid(term=i)
pdep, confi = gam.partial_dependence(term=i, width=.95)

ax.plot(XX[:, i], pdep)
ax.plot(XX[:, i], confi, c='r', ls='--')
ax.set_title(titles[i]);

Author

Karobben

Posted on

2022-03-31

Updated on

2024-01-15

Licensed under

Comments