def chicago_tensor(): """ chicago tensor """ X, y = chicago() gam = PoissonGAM(s(0, n_splines=200) + te(3, 1) + s(2)).fit(X, y) XX = gam.generate_X_grid(term=1, meshgrid=True) Z = gam.partial_dependence(term=1, meshgrid=True) fig = plt.figure() ax = plt.axes(projection='3d') ax.plot_surface(XX[0], XX[1], Z, cmap='viridis') fig.tight_layout() plt.savefig('imgs/pygam_chicago_tensor.png', dpi=300)
############################################################ # https://pygam.readthedocs.io/en/latest/notebooks/tour_of_pygam.html #Fitting and plotting interactions with te() from pygam import PoissonGAM, s, te from pygam.datasets import chicago X, y = chicago(return_X_y=True) X.shape gam = PoissonGAM(s(0, n_splines=200) + te(3, 1) + s(2)).fit(X, y) import matplotlib.pyplot as plt from mpl_toolkits import mplot3d plt.ion() plt.rcParams['figure.figsize'] = (12, 8) XX = gam.generate_X_grid(term=1, meshgrid=True) Z = gam.partial_dependence(term=1, X=XX, meshgrid=True) ax = plt.axes(projection='3d') ax.plot_surface(XX[0], XX[1], Z, cmap='viridis') #Simple interactions, copare with te() from pygam import LinearGAM, s from pygam.datasets import toy_interaction X, y = toy_interaction(return_X_y=True)
def chicago_X_y(): # y is counts # recommend PoissonGAM return chicago(return_X_y=True)
import missingno as msno import seaborn as sns import matplotlib.pyplot as plt from pygam import datasets df = datasets.chicago(return_X_y=False) df.info() msno.bar(df) plt.show() # 3 features with missing values msno.matrix(df) plt.show() # 2 features have missing values that aren't distributed randomly. 1 feature has too many missing values to keep. msno.heatmap(df) plt.show() # low nullity correlation +1 # plot distribution of deaths sns.distplot(df['death']) # roughly normal distribution plt.show()