{"id":6795,"library":"pygam","title":"Generalized Additive Models in Python (pyGAM)","description":"pyGAM is a Python library for building Generalized Additive Models (GAMs), emphasizing modularity and performance. It extends generalized linear models by allowing non-linear functions of features using penalized B-splines while maintaining additivity, making models both flexible and interpretable. The current stable version is 0.12.0. It is actively maintained with a focus on compatibility and contributions are welcome.","status":"active","version":"0.12.0","language":"en","source_language":"en","source_url":"https://github.com/dswah/pyGAM","tags":["machine-learning","statistical-modeling","generalized-additive-models","GAMs","splines","interpretable-ai"],"install":[{"cmd":"pip install pygam","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core numerical operations","package":"numpy","optional":false},{"reason":"Core numerical operations","package":"scipy","optional":false},{"reason":"Progress bar for model fitting","package":"progressbar2","optional":false},{"reason":"Required by pygam.datasets submodule for data loading","package":"pandas","optional":true},{"reason":"Can significantly speed up optimization on large models with constraints by providing a faster sparse Cholesky factorization","package":"scikit-sparse","optional":true}],"imports":[{"note":"Primary class for fitting Linear Generalized Additive Models","wrong":null,"symbol":"LinearGAM","correct":"from pygam import LinearGAM"},{"note":"Primary class for fitting Logistic Generalized Additive Models","wrong":null,"symbol":"LogisticGAM","correct":"from pygam import LogisticGAM"},{"note":"Primary class for fitting Poisson Generalized Additive Models","wrong":null,"symbol":"PoissonGAM","correct":"from pygam import PoissonGAM"},{"note":"Helper function to define a spline term for a feature","wrong":null,"symbol":"s","correct":"from pygam import s"},{"note":"Helper function to define a factor term for a categorical feature","wrong":null,"symbol":"f","correct":"from pygam import f"},{"note":"Helper function to define a tensor product interaction term","wrong":null,"symbol":"te","correct":"from pygam import te"}],"quickstart":{"code":"import numpy as np\nfrom pygam import LinearGAM, s, f\nfrom pygam.datasets import wage\n\nX, y = wage() # Load example data\n\n# Define a GAM with a spline term for features 0 and 1, and a factor term for feature 2\ngam = LinearGAM(s(0) + s(1) + f(2))\n\n# Fit the model\ngam.fit(X, y)\n\n# Print a summary of the model fit\nprint(gam.summary())\n\n# Example of predicting (using dummy data for simplicity)\ndummy_X = np.array([[10, 20, 1], [15, 25, 0]])\npredictions = gam.predict(dummy_X)\nprint(f\"Predictions for dummy data: {predictions}\")","lang":"python","description":"This quickstart demonstrates how to install pyGAM, load an example dataset, define a LinearGAM with spline and factor terms, fit the model to the data, and then print a statistical summary. It also includes a basic prediction example."},"warnings":[{"fix":"For MKL acceleration, explicitly search for and install NumPy/SciPy with MKL via specific Conda channels (e.g., intel channel) or use third-party builds (e.g., `pip install numpy scipy --extra-index-url https://urob.github.io/numpy-mkl`).","message":"Installing NumPy/SciPy linked to Intel MKL for acceleration can be tricky, especially with Conda due to channel compatibility issues. Pip's NumPy-MKL is often outdated. Consider third-party builds or specific Conda channels if MKL optimization is critical.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Interpret p-values with caution, especially when smoothing parameters are estimated. Consider alternative model evaluation metrics or sensitivity analyses.","message":"P-values derived from models where smoothing parameters have been estimated may be lower than they should be, leading to an increased rate of false positives (rejecting the null hypothesis too readily).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid including both a spline and a linear term for the same feature in a single GAM specification. Use either `s()` for a flexible non-linear effect or `l()` for a purely linear effect.","message":"When combining a spline term (e.g., `s(feature)`) and a linear term for the *same* feature in a GAM, it can introduce a model identifiability problem. This can cause p-values to appear statistically significant when they are not.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For the most current features and bug fixes, prioritize installing `pygam` using `pip`.","message":"The `pyGAM` package available on `conda-forge` is typically less up-to-date than the version available via `pip`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If working with large or constrained models, install `scikit-sparse` and `nose`: `conda install -c conda-forge scikit-sparse nose` (or equivalent pip install if available).","message":"For large models with constraints, installing `scikit-sparse` can significantly improve optimization performance due to its faster sparse Cholesky factorization. It also has a dependency on `nose` for its import.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your Python environment is within the supported range (3.10 to 3.13) for optimal compatibility.","message":"pyGAM is officially tested with Python 3.10+ and is compatible up to Python 3.13. Using it with significantly older or very new, untested Python versions might lead to unexpected compatibility issues.","severity":"gotcha","affected_versions":"Python versions outside 3.10-3.13"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}